API Client
You'll build a command line tool that talks to a public API, survives a bad network, and doesn't hammer the server it's talking to.
About
Most real programs spend their time talking to other programs over HTTP. The protocol is simple and the failures are not: timeouts, rate limits, malformed JSON, and a server that's down at 3 a.m. You'll build a small tool against a free public API and handle every one of those on purpose. Weather, public transit, a dictionary, or open data from your city all work, as long as it speaks JSON and doesn't need a credit card.
At a glance
- Per requirement
- 35 XP
- Completion bonus
- 300 XP
- Requirements
- 6
- Takes
- Two to four weeks
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Fetch JSON from a public API with your language's HTTP library and print one field. Skip any wrapper library for the API and use raw HTTP, so you can see the request and the response. Print the status code and one value from the body. If the API wants a key, read it from an environment variable, never from the code and never from git. Take a screenshot of the request, the status and the value. Photo
-
2
Turn the response into typed objects or structs rather than raw dictionaries. Write a class or struct for the thing you're fetching, with only the fields you actually use. Nested data gets nested types. When the API changes shape one day, the error should happen here and not three functions later. Take a screenshot of the type and a parsed object printed out. Photo
-
3
Handle a timeout, a 404 and a 500 without crashing, with a different message for each. Set a timeout of 5 seconds. Point the tool at a wrong URL for the 404, and use httpbin.org/status/500 for the server error. Each case gets a one-line human message and a non-zero exit code. Take a screenshot of all three messages. Photo
-
4
Add a cache so repeated calls within ten minutes don't hit the network. A JSON file on disk keyed by URL with a timestamp is plenty. Make the second run visibly faster and have it print whether the result was cached or fresh. Most public APIs have rate limits, and a cache is good manners. Take a screenshot of two runs showing the time difference. Photo
-
5
Add a retry with backoff for failed requests, capped at three tries. Wait one second, then two, then four. Never retry a 404, because it won't get better. Log each attempt. Take a screenshot of the log showing a retry against a flaky endpoint, which httpbin can fake for you. Photo
-
6
Write tests that run without the network. Record one real response to a file and have your tests read from that instead of the internet. Your suite should pass on a plane. Take a screenshot of the tests passing with the wifi icon showing it's off. Photo