APIs · Course lab · about 150 minutes · 6 tasks · marked out of 100, pass at 60
A robust API client that survives the real internet
The situation
Write a script that fetches from a real public API and keeps working when the internet does what it does: slow answers, missing fields, wrong URLs, 500 errors, rate limits, and an accidental double run. Every failure mode is provoked on purpose and handled on purpose, and the key never appears in the file.
What you'll be able to show
- Read API documentation and name base URL, endpoint, method, parameters and auth
- Check status codes and handle missing fields without crashing
- Add timeouts, retries with backoff and logging
- Respect rate limits, cache repeats, and make a double run safe
What you need
- Python 3 with requests
- One free public API that needs a key, and one that does not
- An environment variable for the key
Tasks
-
1Read the documentationFor a public API (weather, currency, anything), write down without code: base URL, one endpoint, the method, one required parameter, whether it needs a key, and its rate limit if stated.A correct result: Six facts from the documentation, with the page URL.
-
2The first fetch, checkedFetch one endpoint, check the status code, print three specific fields. Then break the URL deliberately and confirm your check catches it instead of crashing.A correct result: Three fields printed; the broken URL produces a handled message, not a traceback.
-
3The key, outside the fileSign up for a free API key. Store it as an environment variable and read it with os.environ. Search the script file for the key and find nothing.A correct result: A working authenticated call; grep of the file for the key returns nothing.
-
4RobustAdd a timeout, .get() for every field, handling for non-200 codes, three retries with increasing waits on 5xx, and a log line per failure. Point it at a URL that does not exist and at a field that does not exist; watch it fail gracefully both times.A correct result: Two provoked failures, each logged and survived.
-
5Many items, politelyAdapt the script to fetch for a list of twenty items. Add a pause between calls, cache anything repeated so it is fetched once, and write down what would happen if the script ran twice by accident. Make that safe (idempotent output).A correct result: Twenty items fetched with pauses, repeats served from cache, and a double run that changes nothing.
-
6The failure catalogueWrite a short table: each failure mode you provoked, how you provoked it, what the script did, and the line of code that handles it.A correct result: A table with at least six rows.
What to hand in
The script, the log from the provoked failures, the cache evidence, and the failure catalogue.
How it is marked
| Criterion | Points |
|---|---|
| Documentation facts recorded correctly | 10 |
| Status checked and the broken URL handled | 15 |
| Key kept out of the file, verified | 15 |
| Timeouts, retries and logging work under provoked failures | 25 |
| Rate limit respected, cache used, double run safe | 20 |
| Failure catalogue complete | 15 |
| Total · pass at 60 | 100 |
صورتحال
ایک script لکھیں جو کسی حقیقی public API سے data لائے اور تب بھی کام کرتی رہے جب internet وہ کرے جو وہ کرتا ہے: سست جواب، غائب fields، غلط URLs، 500 errors، rate limits، اور غلطی سے دو بار چلنا۔ ہر ناکامی کی صورت جان بوجھ کر پیدا کی جاتی ہے اور جان بوجھ کر سنبھالی جاتی ہے، اور key کبھی file میں نہیں آتی۔
آپ کیا دکھا سکیں گے
- API documentation پڑھنا اور base URL، endpoint، method، parameters اور auth کا نام لینا
- status codes چیک کرنا اور غائب fields کو crash ہوئے بغیر سنبھالنا
- timeouts، backoff کے ساتھ retries اور logging شامل کرنا
- rate limits کا احترام کرنا، دہرائی ہوئی چیزیں cache کرنا، اور دو بار چلنے کو محفوظ بنانا
آپ کو کیا چاہیے
- requests کے ساتھ Python 3
- ایک مفت public API جسے key چاہیے، اور ایک جسے نہیں
- key کے لیے ایک environment variable
کام
-
1documentation پڑھیںکسی public API (موسم، کرنسی، کچھ بھی) کے لیے بغیر code کے لکھیں: base URL، ایک endpoint، method، ایک لازمی parameter، کیا اسے key چاہیے، اور اس کی rate limit اگر بتائی گئی ہو۔درست نتیجہ: documentation سے چھ حقائق، صفحے کے URL کے ساتھ۔
-
2پہلا fetch، چیک شدہایک endpoint سے data لائیں، status code چیک کریں، تین مخصوص fields print کریں۔ پھر جان بوجھ کر URL توڑیں اور تصدیق کریں کہ آپ کا check اسے crash کے بجائے پکڑتا ہے۔درست نتیجہ: تین fields print ہوں؛ ٹوٹا ہوا URL traceback نہیں، سنبھالا ہوا پیغام دے۔
-
3key، file سے باہرایک مفت API key حاصل کریں۔ اسے environment variable کے طور پر رکھیں اور os.environ سے پڑھیں۔ script file میں key تلاش کریں اور کچھ نہ ملے۔درست نتیجہ: ایک کام کرتی authenticated call؛ file میں key کی grep کچھ واپس نہ کرے۔
-
4مضبوطtimeout شامل کریں، ہر field کے لیے .get()، غیر-200 codes کی handling، 5xx پر بڑھتے وقفوں کے ساتھ تین retries، اور ہر ناکامی پر ایک log line۔ اسے ایسے URL پر چلائیں جو موجود نہیں اور ایسے field پر جو موجود نہیں؛ دونوں بار اسے سلیقے سے ناکام ہوتے دیکھیں۔درست نتیجہ: دو پیدا کی گئی ناکامیاں، ہر ایک log شدہ اور بچ گئی۔
-
5بہت سی چیزیں، شائستگی سےscript کو بیس چیزوں کی list کے لیے ڈھالیں۔ calls کے درمیان وقفہ ڈالیں، دہرائی ہوئی چیزیں cache کریں تاکہ ایک بار fetch ہوں، اور لکھیں کہ اگر script غلطی سے دو بار چلے تو کیا ہو گا۔ اسے محفوظ بنائیں (idempotent output)۔درست نتیجہ: وقفوں کے ساتھ بیس چیزیں fetch ہوئیں، دہرائی ہوئی cache سے آئیں، اور دو بار چلنا کچھ نہیں بدلتا۔
-
6ناکامیوں کا catalogueایک چھوٹی table لکھیں: ہر ناکامی کی صورت جو آپ نے پیدا کی، کیسے پیدا کی، script نے کیا کیا، اور code کی وہ line جو اسے سنبھالتی ہے۔درست نتیجہ: کم از کم چھ قطاروں والی table۔
کیا جمع کروانا ہے
script، پیدا کی گئی ناکامیوں کا log، cache کا ثبوت، اور ناکامیوں کا catalogue۔
نمبر کیسے ملیں گے
| معیار | نمبر |
|---|---|
| documentation کے حقائق درست درج | 10 |
| status چیک ہوا اور ٹوٹا ہوا URL سنبھالا گیا | 15 |
| key file سے باہر رکھی گئی، تصدیق شدہ | 15 |
| پیدا کی گئی ناکامیوں میں timeouts، retries اور logging کام کرتے ہیں | 25 |
| rate limit کا احترام، cache استعمال، دو بار چلنا محفوظ | 20 |
| ناکامیوں کا catalogue مکمل | 15 |
| کل · پاس 60 | 100 |
Hand in your lab
Create a free BvLogic ID to hand in your lab, get it marked, and have it on your certificate.
Create your BvLogic IDAlready have one? Sign in and this course will be added to it.