fix: Propagate API token to custom HTTP clients#956
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #956 +/- ##
==========================================
+ Coverage 94.61% 94.65% +0.03%
==========================================
Files 58 58
Lines 5239 5257 +18
==========================================
+ Hits 4957 4976 +19
+ Misses 282 281 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| raise ValueError('Cannot pass both "json" and "data" parameters at the same time!') | ||
|
|
||
| headers = dict(headers) if headers else {} | ||
| headers = {**self._headers, **(headers or {})} |
There was a problem hiding this comment.
Impit client was working without this, so I guess this is going to duplicate header handling at this level for the default Impit client.
https://github.com/apify/impit/blob/accab5669aa3c3d8e5dc3adc75c847294eabd104/impit/src/impit.rs#L413
There was a problem hiding this comment.
It won't duplicate the headers because we use _prepare_request_call, where we do deduplication. But yeah, it would make sense to update the Impit init, stop passing the default headers, and rely on the upstream behavior.
|
I'll also update it to handle the fact, that HTTP headers should be case-insensitive (according to RFC 9110). And also add more tests regarding the headers. |
ApifyClient.with_custom_http_client(token=...)(and the async twin) stored the token on theApifyClientinstance but never passed it to the injected HTTP client, so no request carried anAuthorizationheader and every call failed with 401. The documented custom HTTP client example inherited the bug.with_custom_http_clientnow setsAuthorization: Bearer <token>on the injected client's default headers, unless the client already has an auth header configured (checked case-insensitively).HttpClientBase._prepare_request_callnow merges the client's default headers under the per-request headers, so any custom client using the helper (including a pre-builtImpitHttpClientpassed as the custom client) actually sends them. For the default client the wire behavior is unchanged, since impit request-level headers replace the identical client-level ones.self._headersbefore delegating, and theHttpClientABC docstring states that implementations must send the default headers with every request.Regression tests cover the token reaching the wire (custom sync/async clients and a pre-built tokenless
ImpitHttpClient) and the no-clobber semantics for client-configured auth headers.🤖 Generated with Claude Code