Many analyst scripts start by pulling data from an API. With the requests library, that looks like resp = requests.get(url, params={"page": 2}, timeout=10). The response has a status_code (200 means OK, 404 not found, 429 too many requests, 500 a server error) and resp.json() gives the parsed body.
These exercises have no network, so the setup cell defines api_get(path, params), which returns the same kind of response from canned data. Everything else is the real pattern:
- Check the status first. An error body is JSON too, so reading it as data gives strange results.
- Page through results. APIs return results in pages; keep requesting until there are no more.
- Retry politely. On 429, wait and try again a limited number of times.
- Flatten nested records into rows before analysis, with
.get() for fields that may be missing.
class Response:
def __init__(self, status_code, data):
self.status_code, self._data = status_code, data
def json(self):
return self._data
resp = Response(404, {"error": "not found"})
resp.status_code == 200, resp.json()
Out: (False, {'error': 'not found'}). The error body is valid JSON, so always check the status before using it.