Skip to content

Better error handling and other cleanup#629

Merged
ianepreston merged 17 commits intomasterfrom
speedtest
Apr 10, 2026
Merged

Better error handling and other cleanup#629
ianepreston merged 17 commits intomasterfrom
speedtest

Conversation

@ianepreston
Copy link
Copy Markdown
Owner

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

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
@ianepreston ianepreston merged commit 801e154 into master Apr 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant