Now reach out to the wider internet: build a client for a real public API (weather, GitHub, or similar) that fetches data, parses the JSON, and presents a clean summary. It applies requests, robust error handling, and modelling a messy response into just what you need.
What to build
- Call a public API with
requestsand atimeout. - Handle network errors and non-200 responses gracefully.
- Extract only the fields you care about from the JSON.
- Present a clean, readable summary to the user.
A robust client function
import requestsdef get_weather(city: str) -> dict | None:try:r = requests.get("https://api.example.com/weather",params={"q": city}, timeout=10)r.raise_for_status()except requests.RequestException as e:print(f"Request failed: {e}")return Nonedata = r.json()return { # model just what you need"city": data["name"],"temp": data["main"]["temp"],"desc": data["weather"][0]["description"],}
Stretch goals
Cache responses to avoid repeat calls, add a retry with backoff, load the API key from an environment variable, or add a small CLI with argparse.
Key takeaways
- 01Applies requests, JSON parsing and robust error handling end to end.
- 02Always use timeouts and check status codes (raise_for_status).
- 03Catch requests.RequestException so network failures do not crash the app.
- 04Model the response into just the fields you need.
- 05Stretch it with caching, retries, env-var config, or a CLI.
