@@ -873,66 +873,61 @@ def _request(
873
873
request = self ._build_request (options )
874
874
self ._prepare_request (request )
875
875
876
- response = None
877
-
878
876
try :
879
877
response = self ._client .send (
880
878
request ,
881
879
auth = self .custom_auth ,
882
880
stream = stream or self ._should_stream_response_body (request = request ),
883
881
)
884
- log .debug (
885
- 'HTTP Request: %s %s "%i %s"' , request .method , request .url , response .status_code , response .reason_phrase
886
- )
887
- response .raise_for_status ()
888
- except httpx .HTTPStatusError as err : # thrown on 4xx and 5xx status code
889
- if retries > 0 and self ._should_retry (err .response ):
890
- err .response .close ()
882
+ except httpx .TimeoutException as err :
883
+ if retries > 0 :
891
884
return self ._retry_request (
892
885
options ,
893
886
cast_to ,
894
887
retries ,
895
- err .response .headers ,
896
888
stream = stream ,
897
889
stream_cls = stream_cls ,
890
+ response_headers = None ,
898
891
)
899
892
900
- # If the response is streamed then we need to explicitly read the response
901
- # to completion before attempting to access the response text.
902
- if not err .response .is_closed :
903
- err .response .read ()
904
-
905
- raise self ._make_status_error_from_response (err .response ) from None
906
- except httpx .TimeoutException as err :
907
- if response is not None :
908
- response .close ()
909
-
893
+ raise APITimeoutError (request = request ) from err
894
+ except Exception as err :
910
895
if retries > 0 :
911
896
return self ._retry_request (
912
897
options ,
913
898
cast_to ,
914
899
retries ,
915
900
stream = stream ,
916
901
stream_cls = stream_cls ,
917
- response_headers = response . headers if response is not None else None ,
902
+ response_headers = None ,
918
903
)
919
904
920
- raise APITimeoutError (request = request ) from err
921
- except Exception as err :
922
- if response is not None :
923
- response .close ()
905
+ raise APIConnectionError (request = request ) from err
924
906
925
- if retries > 0 :
907
+ log .debug (
908
+ 'HTTP Request: %s %s "%i %s"' , request .method , request .url , response .status_code , response .reason_phrase
909
+ )
910
+
911
+ try :
912
+ response .raise_for_status ()
913
+ except httpx .HTTPStatusError as err : # thrown on 4xx and 5xx status code
914
+ if retries > 0 and self ._should_retry (err .response ):
915
+ err .response .close ()
926
916
return self ._retry_request (
927
917
options ,
928
918
cast_to ,
929
919
retries ,
920
+ err .response .headers ,
930
921
stream = stream ,
931
922
stream_cls = stream_cls ,
932
- response_headers = response .headers if response is not None else None ,
933
923
)
934
924
935
- raise APIConnectionError (request = request ) from err
925
+ # If the response is streamed then we need to explicitly read the response
926
+ # to completion before attempting to access the response text.
927
+ if not err .response .is_closed :
928
+ err .response .read ()
929
+
930
+ raise self ._make_status_error_from_response (err .response ) from None
936
931
937
932
return self ._process_response (
938
933
cast_to = cast_to ,
@@ -1340,66 +1335,61 @@ async def _request(
1340
1335
request = self ._build_request (options )
1341
1336
await self ._prepare_request (request )
1342
1337
1343
- response = None
1344
-
1345
1338
try :
1346
1339
response = await self ._client .send (
1347
1340
request ,
1348
1341
auth = self .custom_auth ,
1349
1342
stream = stream or self ._should_stream_response_body (request = request ),
1350
1343
)
1351
- log .debug (
1352
- 'HTTP Request: %s %s "%i %s"' , request .method , request .url , response .status_code , response .reason_phrase
1353
- )
1354
- response .raise_for_status ()
1355
- except httpx .HTTPStatusError as err : # thrown on 4xx and 5xx status code
1356
- if retries > 0 and self ._should_retry (err .response ):
1357
- await err .response .aclose ()
1344
+ except httpx .TimeoutException as err :
1345
+ if retries > 0 :
1358
1346
return await self ._retry_request (
1359
1347
options ,
1360
1348
cast_to ,
1361
1349
retries ,
1362
- err .response .headers ,
1363
1350
stream = stream ,
1364
1351
stream_cls = stream_cls ,
1352
+ response_headers = None ,
1365
1353
)
1366
1354
1367
- # If the response is streamed then we need to explicitly read the response
1368
- # to completion before attempting to access the response text.
1369
- if not err .response .is_closed :
1370
- await err .response .aread ()
1371
-
1372
- raise self ._make_status_error_from_response (err .response ) from None
1373
- except httpx .TimeoutException as err :
1374
- if response is not None :
1375
- await response .aclose ()
1376
-
1355
+ raise APITimeoutError (request = request ) from err
1356
+ except Exception as err :
1377
1357
if retries > 0 :
1378
1358
return await self ._retry_request (
1379
1359
options ,
1380
1360
cast_to ,
1381
1361
retries ,
1382
1362
stream = stream ,
1383
1363
stream_cls = stream_cls ,
1384
- response_headers = response . headers if response is not None else None ,
1364
+ response_headers = None ,
1385
1365
)
1386
1366
1387
- raise APITimeoutError (request = request ) from err
1388
- except Exception as err :
1389
- if response is not None :
1390
- await response .aclose ()
1367
+ raise APIConnectionError (request = request ) from err
1391
1368
1392
- if retries > 0 :
1369
+ log .debug (
1370
+ 'HTTP Request: %s %s "%i %s"' , request .method , request .url , response .status_code , response .reason_phrase
1371
+ )
1372
+
1373
+ try :
1374
+ response .raise_for_status ()
1375
+ except httpx .HTTPStatusError as err : # thrown on 4xx and 5xx status code
1376
+ if retries > 0 and self ._should_retry (err .response ):
1377
+ await err .response .aclose ()
1393
1378
return await self ._retry_request (
1394
1379
options ,
1395
1380
cast_to ,
1396
1381
retries ,
1382
+ err .response .headers ,
1397
1383
stream = stream ,
1398
1384
stream_cls = stream_cls ,
1399
- response_headers = response .headers if response is not None else None ,
1400
1385
)
1401
1386
1402
- raise APIConnectionError (request = request ) from err
1387
+ # If the response is streamed then we need to explicitly read the response
1388
+ # to completion before attempting to access the response text.
1389
+ if not err .response .is_closed :
1390
+ await err .response .aread ()
1391
+
1392
+ raise self ._make_status_error_from_response (err .response ) from None
1403
1393
1404
1394
return self ._process_response (
1405
1395
cast_to = cast_to ,
0 commit comments