-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
82 commits
Select commit
Hold shift + click to select a range
2603139
Client middleware PoC
bdraco 5ea6a04
Merge branch 'master' into client_middleware
bdraco 91c83df
trim
bdraco 163ac5b
trim
bdraco 578c761
trim
bdraco de85ea1
cleanup
bdraco 8857b9e
cleanup
bdraco 64a9597
cleanup
bdraco cd4a07c
cleanup
bdraco 9f9b518
cleanup
bdraco 86aa049
cleanup
bdraco 374799b
cleanup
bdraco 6d600f0
cleanup
bdraco cd02de8
cleanup
bdraco 03607c9
preen
bdraco 22cc25b
fix loop var
bdraco deafe32
fix error
bdraco cf808f6
fix refactorig error
bdraco 34f24ac
fix refactorig error
bdraco 7c5658f
cleanup
bdraco a1cab41
cleanup
bdraco 42266de
cleanup
bdraco f79f5ef
cleanup
bdraco d0a4704
cover
bdraco 61efd10
fixes
bdraco 6d887ed
fix type
bdraco d4ec087
fix
bdraco 92d93b7
changelog
bdraco 0211e49
preen
bdraco 7152b20
preen
bdraco f90e6b1
preen
bdraco b84e4d8
preen
bdraco 9c52522
Update aiohttp/client_middlewares.py
bdraco 392f3e7
Update docs/client_advanced.rst
bdraco 2db4321
cleanup
bdraco 85c33c6
cleanup
bdraco 71ac750
fix
bdraco 56c750c
noreturn
bdraco e48f570
noreturn
bdraco 139052d
handle exception in middleware
bdraco fc3b57b
one more
bdraco 9f6a840
docs
bdraco 6c222d6
make sure we can block connections
bdraco 818cd82
fixes
bdraco f4b4608
typing
bdraco ff2be43
cleanup
bdraco 6bf3b31
more cover
bdraco fd13fa8
ssrf example
bdraco 3cfa627
bad port bind is now caught
bdraco 384e991
mypy
bdraco df43381
document
bdraco f86281f
lint
bdraco e034e28
cleanup
bdraco 6ef8233
fix test
bdraco db30bd7
fix test
bdraco ebfae72
format
bdraco b4525e6
format
bdraco ae575e1
format
bdraco 6c633b5
reduce scope of retry
bdraco 323572e
adjust client middleware retry pattern to avoid exception and use loo…
bdraco 555169d
lint
bdraco 384a942
lint
bdraco 8217840
Merge branch 'master' into client_middleware
bdraco 4417d9c
Use request.session
Dreamsorcerer 9cfba39
Remove _url/_method from handler.
Dreamsorcerer eeac225
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] de1f5fc
Fixup
Dreamsorcerer 16237eb
Fix mock
Dreamsorcerer b49fba3
recursion tests
bdraco 351d767
add warning about infinite recursion
bdraco 84ad646
add test
bdraco dc2f947
Merge branch 'master' into client_middleware
bdraco bd07cf1
add missing coverage for moved code
bdraco 733c672
Merge remote-tracking branch 'upstream/client_middleware' into client…
bdraco 6617c34
remove unreachable or assert
bdraco bcef340
unreachable
bdraco aa6c8b0
remove unreachable or assert
bdraco 8bc1e40
delete examples as they were only for development and demo purposes
bdraco 19dbc37
polish
bdraco c90cce8
adjust changes
bdraco cafc876
adjust changes
bdraco ab1aac2
docs
bdraco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Added client middleware support -- by :user:`bdraco` and :user:`Dreamsorcerer`. | ||
|
||
This change allows users to add middleware to the client session and requests, enabling features like | ||
authentication, logging, and request/response modification without modifying the core | ||
request logic. Additionally, the ``session`` attribute was added to ``ClientRequest``, | ||
allowing middleware to access the session for making additional requests. |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Client middleware support.""" | ||
|
||
from collections.abc import Awaitable, Callable | ||
|
||
from .client_reqrep import ClientRequest, ClientResponse | ||
|
||
__all__ = ("ClientMiddlewareType", "ClientHandlerType", "build_client_middlewares") | ||
|
||
# Type alias for client request handlers - functions that process requests and return responses | ||
ClientHandlerType = Callable[[ClientRequest], Awaitable[ClientResponse]] | ||
|
||
# Type for client middleware - similar to server but uses ClientRequest/ClientResponse | ||
ClientMiddlewareType = Callable[ | ||
[ClientRequest, ClientHandlerType], Awaitable[ClientResponse] | ||
] | ||
|
||
|
||
def build_client_middlewares( | ||
handler: ClientHandlerType, | ||
middlewares: tuple[ClientMiddlewareType, ...], | ||
bdraco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> ClientHandlerType: | ||
""" | ||
Apply middlewares to request handler. | ||
The middlewares are applied in reverse order, so the first middleware | ||
in the list wraps all subsequent middlewares and the handler. | ||
This implementation avoids using partial/update_wrapper to minimize overhead | ||
and doesn't cache to avoid holding references to stateful middleware. | ||
""" | ||
if not middlewares: | ||
return handler | ||
bdraco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Optimize for single middleware case | ||
Dreamsorcerer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(middlewares) == 1: | ||
middleware = middlewares[0] | ||
|
||
async def single_middleware_handler(req: ClientRequest) -> ClientResponse: | ||
return await middleware(req, handler) | ||
|
||
return single_middleware_handler | ||
|
||
# Build the chain for multiple middlewares | ||
current_handler = handler | ||
|
||
for middleware in reversed(middlewares): | ||
# Create a new closure that captures the current state | ||
def make_wrapper( | ||
mw: ClientMiddlewareType, next_h: ClientHandlerType | ||
) -> ClientHandlerType: | ||
async def wrapped(req: ClientRequest) -> ClientResponse: | ||
return await mw(req, next_h) | ||
|
||
return wrapped | ||
|
||
current_handler = make_wrapper(middleware, current_handler) | ||
|
||
return current_handler |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.