Theory
API પર વિશ્વાસ કરતા પહેલાં test કરો
તમે હવે events APIને call કરતું Android code લખવાના છો. પરંતુ API તમારી ધારણા કરતાં અલગ behave કરે તો? Field name અલગ હોઈ શકે, response structure બદલાયેલું હોઈ શકે અથવા error આવી શકે. જો APIને માત્ર appની અંદરથી જ call કરો, તો failure APIનું છે કે appનું તે સમજવું મુશ્કેલ બને.
Professional habit છે કે પહેલાં APIને અલગથી test કરો, જેમ કે Postman toolથી. આ lesson API testing tools શું કરે છે અને endpointને independently test કરવાથી debuggingના કલાકો કેવી રીતે બચે છે તે સમજાવે છે. APIનું behaviour સમજી અને confirm કર્યા પછી જ તેની સામે app build કરો.
Theory
API testing tool શું કરે છે
Postman જેવી tool તમને app code લખ્યા વગર APIના endpoints પર HTTP requests મોકલવા દે છે. તમે method (GET, POST), URL (endpoint), જરૂરી headers (જેમ કે auth token) અને POST માટે body set કરો, પછી Send દબાવો.
Tool તમને exact response બતાવે છે: bodyમાં મળેલું JSON, status code જેમ કે 200, 404 અથવા 401, response headers અને requestને લાગેલો સમય. તેથી Android codeની એક line લખતા પહેલાં API શું expect કરે છે અને શું return કરે છે તે ચોક્કસ જોઈ શકાય છે. Command-line tool curl પણ terminalમાંથી આવું જ કરી શકે છે.
Formula
Fault APIમાં છે કે appમાં તે અલગ કરો
સૌથી મોટો લાભ isolation છે. App data મેળવી ન શકે તો કારણ API down હોવું, API બદલાઈ હોવું અથવા અલગ request માંગતી હોવું હોઈ શકે છે; અથવા appમાં wrong URL, bad parsing અથવા missing header હોઈ શકે છે.
Postmanમાં endpoint test કરો. જો Postmanને સાચો response મળે, તો API અને request કામ કરે છે અને bug તમારા appમાં છે. જો Postman પણ fail થાય, તો problem APIમાં અથવા તમારા requestમાં છે. એક checkથી guessing બંધ થાય છે અને ક્યાં debug કરવું તે તરત સમજાય છે.
Quiz
Android app સામે API build કરતા પહેલાં અને દરમિયાન Postman જેવી toolથી API શા માટે test કરવી?
- Postman તમારા માટે Android code લખે છે
- APIની exact requests અને responses independently જોવા, API કામ કરે છે તેની ખાતરી કરવા અને problem APIમાં છે કે appમાં તે અલગ કરવા
- Postman વગર apps APIs call કરી શકતી નથી
- App data permanently store કરવા
Show the answer
APIની exact requests અને responses independently જોવા, API કામ કરે છે તેની ખાતરી કરવા અને problem APIમાં છે કે appમાં તે અલગ કરવા
Postman જેવી tools app code વગર requests મોકલીને exact response અને status code બતાવે છે. તેથી API સામે app build કરતા પહેલાં તેનું behaviour સમજાય છે અને પછી failure APIનું છે કે appનું તે isolate કરી શકાય છે. Option A ખોટું છે: Postman API test કરે છે, Android code generate કરતું નથી. Option C ખોટું છે: app Volley જેવી libraryથી APIને direct call કરી શકે છે; Postman testing aid છે, requirement નહીં. Option D ખોટું છે: Postman requests test કરવા માટે છે, app data store કરવા માટે નહીં.
Think first
APIને અલગથી test કરવાથી debugging time કેમ બચે છે?
બધું appની અંદર જ debug કેમ ન કરવું? APIને independently test કરવાથી શું વધારાનું મળે છે? પછી tap.
Show the answer
કારણ કે આખી app-plus-API systemને એકસાથે debug કરવાથી ઘણા possible causes એકસાથે ધ્યાનમાં લેવા પડે છે. APIને અલગથી test કરવાથી problem બે નાના independent partsમાં વહેંચાય છે અને એક સમયે એક part check કરી શકાય છે.
Appમાં data ન દેખાય ત્યારે wrong endpoint URL, missing અથવા malformed header, API down અથવા changed હોવી, non-200 status, parsing માટે અલગ response shape, threading/callback error અથવા UI bug, કોઈ પણ કારણ હોઈ શકે છે. Postmanમાં endpoint test કરીને chainને બે ભાગમાં વહેંચો. જો Postman correct response આપે, તો API અને request fine છે; bug તમારા URL, headers, parsing અથવા display codeમાં જ હશે. Server okay છે કે નહીં તે અંગેની અડધી possibilities તમે તરત દૂર કરી શકો છો.
જો Postman પણ fail થાય અથવા unexpected response આપે, તો problem API અથવા requestમાં છે અને app codeમાં શોધવાનું બંધ કરી શકો છો. Status codes પણ clue આપે છે: 401 authentication problem, 404 wrong URL, અને differently-shaped body parsing સુધારવાની જરૂર બતાવી શકે છે.
આ divide-and-conquer debugging strategy છે: components isolate કરો અને independently verify કરો. API testing એ આ strategyને app-plus-service integration પર લાગુ કરે છે. પહેલાં APIને અલગથી test કરો, પછી appમાં integration debug કરો.
Summary
Key takeaways
- App સામે build કરતા પહેલાં અને development દરમિયાન APIને independently test કરો.
- Postman અથવા curl app code વગર HTTP requests મોકલે છે અને response body, status code, headers અને timing બતાવે છે.
- Method, URL, headers અને body set કરીને API શું expect અને return કરે છે તે ચોક્કસ જુઓ.
- આથી Android code લખતા પહેલાં API કામ કરે છે અને તેનો request/response shape સમજાય છે.
- Debugging વખતે Postman works તો bug appમાં છે; Postman fail થાય તો problem API અથવા requestમાં છે.
- Components isolate કરવાથી divide-and-conquer debugging ઝડપી બને છે.
- Memory hook: પહેલાં APIને એકલા test કરો, પછી દરેક failure APIનું છે કે appનું તે જાણી શકશો.