fix: hand HTTPBasicAuth a real PreparedRequest - #53
Open
blaipr wants to merge 1 commit into
Open
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
`Base.get_oauth2_token` builds a fake request so it can borrow `HTTPBasicAuth` to compute an `Authorization` header:
```python
req = collections.namedtuple('req', 'headers')({})
HTTPBasicAuth(client_id, client_secret)(req)
```
`HTTPBasicAuth.__call__` takes a `PreparedRequest`, sets `r.headers['Authorization']` and returns `r`. A namedtuple with a `headers` attribute happens to survive that, because `__call__` touches nothing else, but it is not what the interface asks for and it breaks the moment requests reads anything more from the object it is handed.
It is a real `PreparedRequest` now, with its headers initialised the way `prepare_headers` does:
```python
req = PreparedRequest()
req.headers = CaseInsensitiveDict()
HTTPBasicAuth(client_id, client_secret)(req)
```
Same cost, no reimplementation of the base64 and encoding handling that `requests` already does correctly, and `collections` is no longer imported at all since this was its only use.
Checked that the header is byte-identical between the two:
```
old header: Basic dXNlcjpwYTU1
new header: Basic dXNlcjpwYTU1
identical: True
```
`headers` also becomes a `CaseInsensitiveDict` rather than a plain one, which is what the other two branches of this method already hand to `connection.post`, so the three paths now agree.
Two diagnostics retired, and `ty` reports nothing further about `HTTPBasicAuth`.
Note for whoever merges: ctrliq#22 removes three methods from this file, immediately above the one changed here, so whichever lands second needs a rebase.
Verified with `black --check`, `flake8` and the unit suite, 355 passing.
blaipr
force-pushed
the
fix/httpbasicauth-prepared-request
branch
from
September 13, 2026 09:03
1214e3e to
da48f57
Compare
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.
Base.get_oauth2_tokenbuilds a fake request so it can borrowHTTPBasicAuthto compute anAuthorizationheader:HTTPBasicAuth.__call__takes aPreparedRequest, setsr.headers['Authorization']and returnsr. A namedtuple with aheadersattribute happens to survive that, because__call__touches nothing else, but it is not what the interface asks for and it breaks the moment requests reads anything more from the object it is handed.It is a real
PreparedRequestnow, with its headers initialised the wayprepare_headersdoes:Same cost, no reimplementation of the base64 and encoding handling that
requestsalready does correctly, andcollectionsis no longer imported at all since this was its only use.Checked that the header is byte-identical between the two:
headersalso becomes aCaseInsensitiveDictrather than a plain one, which is what the other two branches of this method already hand toconnection.post, so the three paths now agree.Two diagnostics retired, and
tyreports nothing further aboutHTTPBasicAuth.Note for whoever merges: #22 removes three methods from this file, immediately above the one changed here, so whichever lands second needs a rebase.
Verified with
black --check,flake8and the unit suite, 355 passing.