]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Switch auth/redirect methods to follow flow of execution better (#1273)
authorTom Christie <tom@tomchristie.com>
Thu, 10 Sep 2020 10:44:36 +0000 (11:44 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Sep 2020 10:44:36 +0000 (12:44 +0200)
httpx/_client.py

index afe4f9d9c5db5ada33f996f9129bc8e3249f609e..7a264318df340e67f068119ac4c7f09b577985c1 100644 (file)
@@ -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: