Add a versioned User-Agent - #279
Merged
Merged
Conversation
Co-authored-by: Matthew Spah <spahmatthew@gmail.com>
Mattsface
marked this pull request as ready for review
August 3, 2026 23:24
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.
Closes #271
Refs #265
Summary
Why
Requests sends its generic
python-requests/<version>User-Agent by default, so MLB Stats API traffic from this package is indistinguishable from any other Requests-based client. A package-specific, versioned User-Agent makes package traffic identifiable while debugging and makes the installed release visible without any extra request data.What changed
mlbstatsapi/mlb_dataadapter.pygains one internal source of truth for library-owned Session setup:PACKAGE_DISTRIBUTION_NAME = "python-mlb-statsapi"is the only place the distribution name is stored._build_user_agent()reads the version throughimportlib.metadata.version()and falls back tounknownonPackageNotFoundError, so it cannot raise during Session construction._configure_library_session()setssession.headers["User-Agent"]and then calls the existing_configure_retry_adapters().MlbDataAdapter.__init__andMlb.__init__now call_configure_library_session()in the branch that already handledsession is None. Injected Sessions take exactly the same path as before and are never passed to the helper.The package version is not duplicated in source.
pyproject.tomlis unchanged, and the header will automatically reflect the version bump made by the release-preparation issue. Nothing new is exported frommlbstatsapi/__init__.py.Behavior
python-mlb-statsapi/<installed-version>Only the
User-Agentkey is assigned; the header mapping is never replaced, soAccept-Encoding,Accept, andConnectionkeep their Requests default values.Tests
New coverage in
tests/test_mlb_session.py, all offline:9.8.7→python-mlb-statsapi/9.8.7), which proves no release string is hardcodedMlb()and standaloneMlbDataAdapter()Sessions both carry the versioned headersession.prepare_request()carries the header, with no request sentUser-AgentRequests default header matches a baselinerequests.Session(), plus explicit checks forAccept-Encoding,Accept, andConnectionmy-baseball-project/1.0User-Agent and anX-Application: scoreboardheader stay unchanged through construction andclose(), for bothMlbandMlbDataAdapterPackageNotFoundErroryieldspython-mlb-statsapi/unknownand does not raiseassert_library_retry_policy()after the header is setThe existing
test_injected_session_is_not_replaced_with_library_sessionalready asserts the Session identity contract across both adapters, so it was left as-is rather than duplicated.Documentation
docs/http-transport.mdgains a## User-Agentsection covering the header format, the metadata source, preserved default headers, the untouched injected-Session rule, theunknownfallback, an injected-Session example, and an explicit statement that this is not telemetry and carries no machine or user identifiers. The compatibility-warning documentation was not modified.Validation
poetry run pytest tests/test_mlb_session.py -v— 45 passedpoetry run pytest tests/test_mlb_retries.py -vpoetry run pytest tests/test_http_contract.py -vpoetry run pytest tests/test_mlb_exceptions.py -vpoetry run pytest tests/ --ignore=tests/external_tests -v— 255 passedgit diff --checktests/test_http_warnings.pydoes not exist onrelease/0.9.0yet, so the issue #270 warning suite was not run here.Risk
Low. The only behavior change is one header value on Sessions the library creates itself. No public method, argument, return type, exception, or endpoint behavior changed, and no new runtime dependency was added (
importlib.metadatais standard library).Possible impact: any consumer asserting on the outgoing
User-Agentof a library-created Session will now seepython-mlb-statsapi/<version>instead ofpython-requests/<version>.Intentionally left out
MlbHttpCompatibilityWarningwork from Add warnings for future strict HTTP behavior #270, which this branch does not depend onpyproject.tomlversion bump, which belongs to release preparationBase branch
release/0.9.0