Python, properly · Chapter 37 of 42
HTTP with httpx
By the end of this chapter you can call a web API from Python with httpx: read its answers and status codes, send it JSON with a key, walk through its pages, and survive a slow or busy server with timeouts and retries. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world Python
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
DabbaDrop has grown an API. The new website places orders through it, Bhavna's kitchen screen reads the menu from it, and Farah wants your scripts to use it too, instead of a CSV export that is a day old by the time you read it.
Everything so far has read files on your own disk, which answer at once and never say no. An API lives in another program, often on another machine. It answers over the network, it can be slow, it can refuse you, and sometimes it is simply busy. Every AI lab after this one talks to a model the same way: over HTTP, through an API that can be slow or rate-limited. This chapter is where you learn to do that properly.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
menu_api.py: the menu, from the API
Bhavna's kitchen screen should read the menu from DabbaDrop's API at http://localhost:8000, not from a file. Write two functions that call it with httpx:
available_dishes(category) returns the names of the available dishes in that category (veg, jain or non-veg), in the order the API sends them. GET /menu takes category and available=true as query parameters.
dish_price(dish_id) returns one dish's price from GET /menu/<dish_id>, or None when the API answers 404. Any other error status raises.
The check tries all three categories, roti, the unavailable fish-fry (it still has a price) and pav-bhaji, which is not on the menu.
- Task 2
place_order.py: place an order
Farah wants scripts to place orders through the API. place_order(customer_id, items) sends POST /orders with the JSON body {"customer_id": ..., "items": {dish_id: quantity}} and the API key dabba-test-key in the header X-API-Key.
It returns the new order exactly as the API sends it back (status 201). When the API answers 422, raise OrderRejected (already in the file) with the list of errors from the answer's body. Any other error status raises httpx's own error.
The check places a real order for C033, looks it up in the API, then sends one with an unknown dish and a quantity of 0.
- Task 3
all_orders.py: every page of orders
Rafiq wants every delivered order, but GET /orders sends at most 100 at a time. Each answer is {"orders": [...], "page": 1, "per_page": 100, "total": ..., "next": 2}, and next is None on the last page.
fetch_all(status) returns every order with that status, from every page, as one list of order dicts, each order once. The check tries delivered and cancelled and compares the order ids with the September data.
- Task 4 · interview question
Interview: retry.py, retry with backoff
The kitchen system is sometimes busy. get_with_retry(url, attempts=4, delay=0.2, timeout=2.0) GETs the url with that timeout and returns its JSON.
On a 5xx answer or a network error (a timeout, a refused connection), wait delay seconds and try again, doubling the wait after each failure, for at most attempts tries in all. After the last try, raise its error without waiting: an httpx.HTTPStatusError for a 5xx, the timeout itself for a timeout. A 4xx is raised at once, without retrying.
The check counts the calls made to /flaky (503 three times, then 200, with a unique key) and times them, then tries a URL that always fails, a 404 and a slow URL with a short timeout.
What you will learn
- HTTP: a request (method, URL, headers, body) and a response (status code, headers, body).
- httpx: get, post, query parameters, JSON in and out, a Client.
- Status codes: why a 404 is not an exception, and raise_for_status.
- Timeouts and retries: never wait forever, try again with a growing wait, and only for errors that can go away.
More in Real-world Python
Build "HTTP with httpx" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.