Skip to content

fix(odds): identify the odds requests to ESPN - #451

Merged
ChuckBuilds merged 1 commit into
mainfrom
fix/odds-user-agent
Aug 11, 2026
Merged

fix(odds): identify the odds requests to ESPN#451
ChuckBuilds merged 1 commit into
mainfrom
fix/odds-user-agent

Conversation

@ChuckBuilds

@ChuckBuilds ChuckBuilds commented Aug 11, 2026

Copy link
Copy Markdown
Owner

What

src/base_odds_manager.py fetched odds with a bare requests.get, so it identified itself as python-requests/x.y — the one agent ESPN is known to reject. It now goes through a session that sends the same header every other ESPN caller in the tree already sends.

-response = requests.get(url, timeout=self.request_timeout)
+response = self.session.get(url, timeout=self.request_timeout)

Why this path in particular

Around 2026-08-04 ESPN began 403ing browser strings and bare custom tokens alike; what it accepts is a token carrying a URL that says who is calling. src/common/api_helper.py and src/base_classes/data_sources.py were both updated then. The odds manager was missed.

It is the worst one to miss. Odds are fetched per live game, from inside the live update loop, with show_odds defaulting on — so this is the path whose failures cost the caller its entire update budget. It's the same path #449 added the 5s timeout and the 60s cooldown to protect.

To be clear about scope: this is not what is failing on the dev rig today. Those failures are NameResolutionError — DNS, which happens before a request is ever sent, so headers are irrelevant to them; there are zero real 403s in two days of logs. This closes a latent exposure on the path least able to absorb one.

Notes

  • Sent via a session rather than per-call, which also reuses the connection across a slate.
  • Deliberately no retry adapter, unlike api_helper — retries multiply request_timeout, which is 5s precisely to stay inside the 30s operation budget. A test pins this.
  • A 403 raises HTTPError, a RequestException, so it opens the existing breaker rather than being paid once per game. Also pinned.

Tests

The existing tests patched the module's requests.get, which this change bypasses — test_base_odds_manager.py was consequently reaching the real ESPN and taking 404s. Both files now patch the session.

New coverage in TestIdentifiesItselfToEspn: the agent names the project and links to it, it is byte-identical to APIHelper's live value (compared against the object, not a copied literal, so the two can't drift), the header reaches the request, and no retrying adapter is mounted.

46 passed across both odds files; 2751 passed, 60 skipped across the suite. The one failure, test_install_lowmem.py::TestDiskBackedTmpdir, is pre-existing on main and environment-dependent.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5

Summary by CodeRabbit

  • Bug Fixes

    • Improved the reliability of odds requests by using a persistent session with appropriate request headers.
    • Preserved existing timeout, caching, failure cooldown, and stale-data fallback behavior.
    • Added stronger handling and coverage for timeouts, access restrictions, recovery, and request-budget behavior.
  • Tests

    • Expanded automated coverage for session-based requests and identifying request headers.

The odds fetch used a bare requests.get, so it went out as
python-requests/x.y -- the one agent ESPN is known to reject. Around
2026-08-04 it began 403ing browser strings and bare custom tokens alike;
what it accepts is a token carrying a URL that says who is calling.
Every other ESPN caller in the tree already sends that header
(src/common/api_helper.py, src/base_classes/data_sources.py); this path
was simply missed.

It is the worst one to miss. Odds are fetched per live game from inside
the live update loop, so its failures are the ones that cost the caller
its whole update budget -- the same path the 5s timeout and the cooldown
were added to protect.

Sent via a session rather than per-call, which also reuses the
connection across a slate. Deliberately no retry adapter, unlike
api_helper: retries multiply request_timeout, which is 5s precisely to
stay inside the 30s operation budget.

The existing tests patched the module's requests.get, which this change
bypasses -- test_base_odds_manager was consequently reaching the real
ESPN and taking 404s. Both files now patch the session, and the new
tests pin the agent against api_helper's live value so the two cannot
drift apart the next time ESPN moves the goalposts.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

ESPN odds session migration

Layer / File(s) Summary
Configure and use the odds session
src/base_odds_manager.py
BaseOddsManager creates a persistent requests.Session with identifying headers and uses it for odds requests. Existing timeout handling remains unchanged.
Validate session request behavior
test/test_base_odds_manager.py, test/test_odds_request_budget.py
Tests mock session requests and cover headers, timeouts, disabled retries, breaker recovery, 403 responses, and stale-cache fallback.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: identifying odds requests sent to ESPN.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/odds-user-agent

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/test_odds_request_budget.py`:
- Around line 80-88: Update test_the_header_reaches_the_request to mock
m.session.send instead of the session-level get path, then inspect the
PreparedRequest passed to send and assert its User-Agent exactly matches
m.session.headers["User-Agent"]. Preserve the existing single-request assertion
and get_odds invocation.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 570882b1-22f2-4674-b1a9-f4d01adb0851

📥 Commits

Reviewing files that changed from the base of the PR and between bb1a167 and 0e0b893.

📒 Files selected for processing (3)
  • src/base_odds_manager.py
  • test/test_base_odds_manager.py
  • test/test_odds_request_budget.py

Comment thread test/test_odds_request_budget.py
@ChuckBuilds
ChuckBuilds merged commit 2add759 into main Aug 11, 2026
11 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