Better error handling and other cleanup#629
Merged
ianepreston merged 17 commits intomasterfrom Apr 10, 2026
Merged
Conversation
Without timeouts, any request to the StatsCan API could hang indefinitely if the server stops responding. Add a 30s default timeout for API calls and 120s for file downloads. Co-authored-by: Isaac
Without this check, a 403 or 500 response would have its error page HTML written to disk as a .zip file, causing an opaque BadZipFile error downstream. Co-authored-by: Isaac
Reuse TCP connections via HTTP keep-alive instead of creating a
new connection per request. Set User-Agent to stats_can/{version}
so the API provider can identify this client.
Co-authored-by: Isaac
Retry up to 3 times with exponential backoff on 429/5xx errors and connection failures. Uses urllib3 Retry mounted on the shared session so all requests benefit automatically. Co-authored-by: Isaac
When large vector lists are split into chunks, fire requests with a small delay between them to avoid burst traffic against the public StatsCan API. Co-authored-by: Isaac
Use logging.warning instead of print for JSON parse failures. Also fixes a bug where string concatenation with a Path object would have raised TypeError. Co-authored-by: Isaac
…data _fetch_and_validate returns T or list[T] depending on the API response shape -- annotate it honestly. get_cube_metadata always receives a list from parse_tables so it always returns a list. Co-authored-by: Isaac
Code sets (scales, frequencies, symbols, etc.) rarely change but were fetched fresh on every call. Cache after the first request to reduce unnecessary API traffic. Co-authored-by: Isaac
Register an 'integration' marker and apply it to all tests that make real HTTP requests to Statistics Canada. Run fast local checks with: pytest -m "not integration" Co-authored-by: Isaac
Test HTTP errors, API failure statuses, timeouts, connection errors, bad download responses, and empty vector data -- all without hitting the live API. Co-authored-by: Isaac
Dropped from nixpkg
409 error code means the list isn't updated for today
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Trying to add some things to make this more resilient and not hammer the StatsCan API as much.
Plus some cleanup to pre-commit and enabling Darwin in my flake config