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

  1. 1Read the documentation
    For 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.
  2. 2The first fetch, checked
    Fetch 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.
  3. 3The key, outside the file
    Sign 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.
  4. 4Robust
    Add 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.
  5. 5Many items, politely
    Adapt 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.
  6. 6The failure catalogue
    Write 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

CriterionPoints
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

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 ID

Already have one? Sign in and this course will be added to it.