Skip to content

Cache statsbombpy's requests in its own session instead of patching requests globally - #90

Merged
willnewton merged 1 commit into
hudl:masterfrom
codenarok:scoped-request-cache
Oct 19, 2026
Merged

willnewton merged 1 commit into
hudl:masterfrom
codenarok:scoped-request-cache

Conversation

@codenarok

Copy link
Copy Markdown
Contributor

Fixes #85

api_client.py called requests_cache.install_cache() at import time, which patches requests for the whole process. Importing statsbombpy therefore changed caching for every requests call in the host application.

This follows the fix suggested in the issue: a requests_cache.CachedSession that only statsbombpy uses.

Change

New statsbombpy/cache.py with one CachedSession (same sqlite backend in a temp directory, same CACHED_CALLS_SECS expiry as before).
api_client.get_resource() and public.get_response() use that session. The open-data client was relying on the global patch for its caching, so it keeps its cache this way.
Two tests: importing statsbombpy leaves requests untouched (requests_cache.is_installed() is False), and both clients go through the statsbombpy session.
Checked

Both new tests fail on master and pass with the change.
The rest of the suite: 9 tests need StatsBomb API credentials, which I do not have, and fail the same way before and after the change ("please provide credentials"). All other tests pass.
AI-assisted: I used an AI assistant to make the change and write the tests; I reviewed it and ran the tests before and after.

@orca-security-us orca-security-us Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@willnewton willnewton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the contribution!

@willnewton
willnewton merged commit 475f16f into hudl:master Oct 19, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

install_cache() at import time monkeypatches the global requests session — unexpected side effect on the whole process

2 participants