@@ -336,7 +336,6 @@ def _rebuild_redirect_url(self, request: Request, response: Response) -> URL:
336
336
def _send (
337
337
self , request : Request , * , history : list = None , start : float = None
338
338
) -> Response :
339
- history = history if isinstance (history , list ) else []
340
339
start = start or time .perf_counter ()
341
340
config = self .prepare_config (request )
342
341
response = Response .from_tls_response (
@@ -483,6 +482,7 @@ def send(
483
482
response = self ._send (
484
483
request ,
485
484
start = time .perf_counter (),
485
+ history = []
486
486
)
487
487
488
488
if self .hooks .get ("response" ):
@@ -755,39 +755,6 @@ async def request(
755
755
)
756
756
return await self .send (request , auth = auth , follow_redirects = follow_redirects )
757
757
758
- async def send (
759
- self ,
760
- request : Request ,
761
- * ,
762
- stream : bool = False ,
763
- auth : AuthTypes = None ,
764
- follow_redirects : bool = DEFAULT_FOLLOW_REDIRECTS ,
765
- ) -> Response :
766
- if self ._state == ClientState .CLOSED :
767
- raise RuntimeError ("Cannot send a request, as the client has been closed." )
768
-
769
- self ._state = ClientState .OPENED
770
- for fn in [self .prepare_auth , self .build_hook_request ]:
771
- request_ = fn (request , auth or self .auth , follow_redirects )
772
- if isinstance (request_ , Request ):
773
- request = request_
774
-
775
- self .follow_redirects = follow_redirects
776
- response = self ._send (
777
- request ,
778
- start = time .perf_counter (),
779
- )
780
-
781
- if self .hooks .get ("response" ):
782
- response_ = self .build_hook_response (response )
783
- if isinstance (response_ , Response ):
784
- response = response_
785
- else :
786
- await response .aread ()
787
-
788
- await response .aclose ()
789
- return response
790
-
791
758
async def get (
792
759
self ,
793
760
url : URLTypes ,
@@ -995,6 +962,65 @@ async def delete(
995
962
timeout = timeout ,
996
963
)
997
964
965
+ async def send (
966
+ self ,
967
+ request : Request ,
968
+ * ,
969
+ stream : bool = False ,
970
+ auth : AuthTypes = None ,
971
+ follow_redirects : bool = DEFAULT_FOLLOW_REDIRECTS ,
972
+ ) -> Response :
973
+ if self ._state == ClientState .CLOSED :
974
+ raise RuntimeError ("Cannot send a request, as the client has been closed." )
975
+
976
+ self ._state = ClientState .OPENED
977
+ for fn in [self .prepare_auth , self .build_hook_request ]:
978
+ request_ = fn (request , auth or self .auth , follow_redirects )
979
+ if isinstance (request_ , Request ):
980
+ request = request_
981
+
982
+ self .follow_redirects = follow_redirects
983
+ response = await self ._send (
984
+ request ,
985
+ start = time .perf_counter (),
986
+ history = []
987
+ )
988
+
989
+ if self .hooks .get ("response" ):
990
+ response_ = self .build_hook_response (response )
991
+ if isinstance (response_ , Response ):
992
+ response = response_
993
+ else :
994
+ await response .aread ()
995
+
996
+ await response .aclose ()
997
+ return response
998
+
999
+ async def _send (
1000
+ self , request : Request , * , history : list = None , start : float = None
1001
+ ) -> Response :
1002
+ start = start or time .perf_counter ()
1003
+ config = self .prepare_config (request )
1004
+ response = Response .from_tls_response (
1005
+ await self .session .arequest (config .to_dict ()), is_byte_response = config .isByteResponse ,
1006
+ )
1007
+ response .request = request
1008
+ response .default_encoding = self .encoding
1009
+ response .elapsed = datetime .timedelta (seconds = time .perf_counter () - start )
1010
+ if response .is_redirect :
1011
+ response .next = self ._rebuild_redirect_request (response .request , response )
1012
+ if self .follow_redirects :
1013
+ is_break = bool (len (history ) < self .max_redirects )
1014
+ if not is_break :
1015
+ raise TooManyRedirects ("Too many redirects." )
1016
+
1017
+ while is_break :
1018
+ history .append (response )
1019
+ return await self ._send (response .next , history = history , start = start )
1020
+
1021
+ response .history = history
1022
+ return response
1023
+
998
1024
async def aclose (self ) -> None :
999
1025
return self .close ()
1000
1026
0 commit comments