From: Tom Christie Date: Thu, 10 Sep 2020 10:44:36 +0000 (+0100) Subject: Switch auth/redirect methods to follow flow of execution better (#1273) X-Git-Tag: 0.15.0~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=930f3773e28c9cb7dc496650f54a9c467fb53b5a;p=thirdparty%2Fhttpx.git Switch auth/redirect methods to follow flow of execution better (#1273) --- diff --git a/httpx/_client.py b/httpx/_client.py index afe4f9d9..7a264318 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -741,6 +741,37 @@ class Client(BaseClient): return response + def _send_handling_auth( + self, + request: Request, + auth: Auth, + timeout: Timeout, + allow_redirects: bool, + history: typing.List[Response], + ) -> Response: + auth_flow = auth.sync_auth_flow(request) + request = next(auth_flow) + + while True: + response = self._send_handling_redirects( + request, + timeout=timeout, + allow_redirects=allow_redirects, + history=history, + ) + try: + next_request = auth_flow.send(response) + except StopIteration: + return response + except BaseException as exc: + response.close() + raise exc from None + else: + response.history = list(history) + response.read() + request = next_request + history.append(response) + def _send_handling_redirects( self, request: Request, @@ -775,37 +806,6 @@ class Client(BaseClient): ) return response - def _send_handling_auth( - self, - request: Request, - auth: Auth, - timeout: Timeout, - allow_redirects: bool, - history: typing.List[Response], - ) -> Response: - auth_flow = auth.sync_auth_flow(request) - request = next(auth_flow) - - while True: - response = self._send_handling_redirects( - request, - timeout=timeout, - allow_redirects=allow_redirects, - history=history, - ) - try: - next_request = auth_flow.send(response) - except StopIteration: - return response - except BaseException as exc: - response.close() - raise exc from None - else: - response.history = list(history) - response.read() - request = next_request - history.append(response) - def _send_single_request(self, request: Request, timeout: Timeout) -> Response: """ Sends a single request, without handling any redirections. @@ -1364,6 +1364,37 @@ class AsyncClient(BaseClient): return response + async def _send_handling_auth( + self, + request: Request, + auth: Auth, + timeout: Timeout, + allow_redirects: bool, + history: typing.List[Response], + ) -> Response: + auth_flow = auth.async_auth_flow(request) + request = await auth_flow.__anext__() + + while True: + response = await self._send_handling_redirects( + request, + timeout=timeout, + allow_redirects=allow_redirects, + history=history, + ) + try: + next_request = await auth_flow.asend(response) + except StopAsyncIteration: + return response + except BaseException as exc: + await response.aclose() + raise exc from None + else: + response.history = list(history) + await response.aread() + request = next_request + history.append(response) + async def _send_handling_redirects( self, request: Request, @@ -1398,37 +1429,6 @@ class AsyncClient(BaseClient): ) return response - async def _send_handling_auth( - self, - request: Request, - auth: Auth, - timeout: Timeout, - allow_redirects: bool, - history: typing.List[Response], - ) -> Response: - auth_flow = auth.async_auth_flow(request) - request = await auth_flow.__anext__() - - while True: - response = await self._send_handling_redirects( - request, - timeout=timeout, - allow_redirects=allow_redirects, - history=history, - ) - try: - next_request = await auth_flow.asend(response) - except StopAsyncIteration: - return response - except BaseException as exc: - await response.aclose() - raise exc from None - else: - response.history = list(history) - await response.aread() - request = next_request - history.append(response) - async def _send_single_request( self, request: Request, timeout: Timeout ) -> Response: