fix: correct HTTP adapter response handling - #254
Merged
Conversation
Co-authored-by: Matthew Spah <spahmatthew@gmail.com>
Mattsface
marked this pull request as ready for review
August 1, 2026 05:30
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.
Summary
Correct HTTP adapter response-handling defects before sessions, timeouts, retries, or structured exception subclasses are introduced.
Why status classification now occurs before JSON decoding
Error responses from MLB (and intermediate proxies) may return HTML or other non-JSON bodies. Decoding before checking status incorrectly raised
Bad JSON in responsefor 4xx/5xx failures. The adapter now classifies by HTTP status first, so a JSON or HTML 404 still returns empty data, and a JSON or HTML 5xx raises the HTTP-status exception.How the full 2xx range is handled
Successful responses are now detected with
200 <= status_code <= 299, so statuses such as 201, 204, and 299 are accepted instead of falling through to the non-2xx fallback.How empty successful responses are handled
After a successful status check, an empty body uses
response_data = {}. Non-empty successful bodies are decoded as JSON once; invalid JSON still raisesTheMlbStatsApiException("Bad JSON in response")with exception chaining.How
MlbResultnow protects caller-owned dataMlbResultno longer uses a mutable default dictionary. Each instance gets a new outer dictionary (dict(data)or{}), andcopyrightis removed only from that copy. Caller-owned dictionaries are left unchanged.Compatibility
MlbResult(status_code=404, message=response.reason, data={})TheMlbStatsApiExceptionwith"{status}: {reason}"get()signatures are unchanged, including the unuseddataargumentTest plan
Focused adapter/result tests:
Result: 26 passed, 0 failed, 0 xfailed, 0 xpassed
Complete offline suite:
Result: 95 passed
Live adapter tests:
Result: 3 passed (live MLB API was available)
Risk and impact
MlbResultownership change, but public signatures and 404/5xx semantics are preserved