Skip to content
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

Middleware API #1133

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions httpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
WriteError,
WriteTimeout,
)
from ._middleware import AsyncMiddleware, Middleware
from ._models import URL, Cookies, Headers, QueryParams, Request, Response
from ._status_codes import StatusCode, codes
from ._transports.asgi import ASGITransport
Expand All @@ -55,6 +56,8 @@
"codes",
"ASGITransport",
"AsyncClient",
"AsyncMiddleware",
"Middleware",
"Auth",
"BasicAuth",
"Client",
Expand Down
29 changes: 25 additions & 4 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
TooManyRedirects,
map_exceptions,
)
from ._middleware import AsyncMiddleware, Middleware
from ._models import URL, Cookies, Headers, QueryParams, Request, Response
from ._status_codes import codes
from ._transports.asgi import ASGITransport
Expand Down Expand Up @@ -452,6 +453,7 @@ def __init__(
max_redirects: int = DEFAULT_MAX_REDIRECTS,
base_url: URLTypes = None,
transport: httpcore.SyncHTTPTransport = None,
middleware: typing.Sequence[Middleware] = (),
app: typing.Callable = None,
trust_env: bool = True,
):
Expand Down Expand Up @@ -502,6 +504,8 @@ def __init__(
}
self._proxies = dict(sorted(self._proxies.items()))

self._middleware = middleware

def _init_transport(
self,
verify: VerifyTypes = True,
Expand Down Expand Up @@ -610,9 +614,16 @@ def send(

auth = self._build_auth(request, auth)

response = self._send_handling_redirects(
request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,
call_next = functools.partial(
self._send_handling_redirects,
auth=auth,
allow_redirects=allow_redirects,
timeout=timeout,
)
for middleware in self._middleware:
call_next = functools.partial(middleware.send, call_next=call_next)

response = call_next(request)

if not stream:
try:
Expand Down Expand Up @@ -983,6 +994,7 @@ def __init__(
max_redirects: int = DEFAULT_MAX_REDIRECTS,
base_url: URLTypes = None,
transport: httpcore.AsyncHTTPTransport = None,
middleware: typing.Sequence[AsyncMiddleware] = (),
app: typing.Callable = None,
trust_env: bool = True,
):
Expand Down Expand Up @@ -1033,6 +1045,8 @@ def __init__(
}
self._proxies = dict(sorted(self._proxies.items()))

self._middleware = middleware

def _init_transport(
self,
verify: VerifyTypes = True,
Expand Down Expand Up @@ -1138,9 +1152,16 @@ async def send(

auth = self._build_auth(request, auth)

response = await self._send_handling_redirects(
request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,
call_next = functools.partial(
self._send_handling_redirects,
auth=auth,
allow_redirects=allow_redirects,
timeout=timeout,
)
for middleware in self._middleware:
call_next = functools.partial(middleware.asend, call_next=call_next)

response = await call_next(request)

if not stream:
try:
Expand Down
17 changes: 17 additions & 0 deletions httpx/_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Awaitable, Callable

from ._models import Request, Response


class Middleware:
def send(
self, request: Request, call_next: Callable[[Request], Response]
) -> Response:
raise NotImplementedError # pragma: no cover


class AsyncMiddleware:
async def asend(
self, request: Request, call_next: Callable[[Request], Awaitable[Response]]
) -> Response:
raise NotImplementedError # pragma: no cover