-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add Client Middleware Support #9732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
CodSpeed Performance ReportMerging #9732 will not alter performanceComparing Summary
|
Also #6915. There are some examples in there of different use cases which should work when this is complete. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9732 +/- ##
==========================================
+ Coverage 98.71% 98.74% +0.02%
==========================================
Files 125 127 +2
Lines 37894 38520 +626
Branches 2094 2123 +29
==========================================
+ Hits 37407 38036 +629
+ Misses 338 336 -2
+ Partials 149 148 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Since we're generally happy with the design now (as discussed on Matrix), I'll go ahead and merge this to unblock the follow-up work. Any further adjustments can be handled in a separate PR. |
Backport to 3.12: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply 1e911ea on top of patchback/backports/3.12/1e911eaf23091b42cc374547ff9bf692debf9720/pr-9732 Backporting merged PR #9732 into master
🤖 @patchback |
Didn't think I would be that lucky that it wouldn't conflict 😄 |
Co-authored-by: Sam Bull <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> (cherry picked from commit 1e911ea)
Co-authored-by: Sam Bull <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Thanks for the review. I'll clean this up between sprint work. |
Summary
This PR introduces middleware support for the aiohttp client, allowing users to intercept and modify requests and responses. This brings the client API closer to parity with the server API and enables powerful use cases like authentication, logging, retries, and request/response modification.
Key Changes
Core Implementation
Added
aiohttp.client_middlewares
module with:ClientMiddlewareType
type alias for middleware functionsClientHandlerType
type alias for request handlersbuild_client_middlewares()
function to chain middlewares efficientlyModified
ClientSession
to acceptmiddlewares
parameter:_middlewares
attribute to store session middlewaresAdded
session
attribute toClientRequest
:Documentation
client_advanced.rst
:Tests
test_client_middleware.py
(~1100 lines):Features Enabled
Example Usage
Technical Details
partial
orupdate_wrapper
to reduce overheadBreaking Changes
None. This is a backwards-compatible addition.
Related Issues
aiohttp.client
#434 - Add middleware for client requests