Use requests.get with a timeout, check HTTP status, and validate JSON structure before analysis. Build API calls as small functions so retries, headers, and auth behavior stay consistent across scripts.
- Set explicit timeouts to avoid hanging scripts.
- Validate response status and JSON shape before processing.
- Separate fetch logic from transformation logic for testability.
Call APIs defensively
Network behavior is uncertain, so scripts should expect transient failures. Handle non-200 status codes and malformed payloads with clear error messages that include endpoint and parameters.
Use a Session when making repeated calls to reuse connections and shared headers.
Convert JSON payloads into tabular records
After validation, flatten nested JSON into dictionaries with stable keys and then load into pandas. Keep raw API responses for troubleshooting when transformations fail.
- Use params dictionaries instead of string concatenation for query values.
- Log response metadata such as status code and record count.
- Implement retry behavior only for retry-safe status patterns.
Fetch daily exchange rates and store selected fields
A script reads a daily rates endpoint and keeps base currency, date, and selected symbols for pricing reports.
- Send a GET request with params and timeout, then call raise_for_status.
- Parse JSON and verify expected keys like date and rates exist.
- Write normalized records to rates_daily.json for downstream joins.
Common mistakes
- Omitting timeouts and letting scripts stall indefinitely.
- Assuming every successful response has the expected JSON keys.
- Parsing response text manually instead of using response.json.
- Mixing request, transform, and output logic in one large block.
Try one
Why should a Requests call include a timeout in production scripts?
It prevents indefinite hangs and allows the script to fail fast or retry based on policy.
Sources
- Python documentationOfficial reference for Python syntax, standard library modules, and tutorials.
- Requests documentationOfficial user guide and API reference for Python Requests.
- pandas documentationOfficial reference for pandas DataFrame operations, indexing, and analysis APIs.