@@ -745,7 +745,7 @@ def _internal_request(self, request_obj, url, method, **kwargs):
745
745
"""
746
746
method = method .lower ()
747
747
if method not in self ._allowed_methods :
748
- raise ValueError ('Method must be one of: {}' . format ( self ._allowed_methods ) )
748
+ raise ValueError (f 'Method must be one of: { self ._allowed_methods } ' )
749
749
750
750
if 'headers' not in kwargs :
751
751
kwargs ['headers' ] = {** self .default_headers }
@@ -769,95 +769,60 @@ def _internal_request(self, request_obj, url, method, **kwargs):
769
769
if self .timeout is not None :
770
770
kwargs ['timeout' ] = self .timeout
771
771
772
- request_done = False
773
- token_refreshed = False
774
-
775
- while not request_done :
776
- self ._check_delay () # sleeps if needed
772
+ self ._check_delay () # sleeps if needed
773
+ try :
774
+ log .debug (f'Requesting ({ method .upper ()} ) URL: { url } ' )
775
+ log .debug (f'Request parameters: { kwargs } ' )
776
+ # auto_retry will occur inside this function call if enabled
777
+ response = request_obj .request (method , url , ** kwargs )
778
+
779
+ response .raise_for_status () # raise 4XX and 5XX error codes.
780
+ log .debug (f'Received response ({ response .status_code } ) from URL { response .url } ' )
781
+ return response
782
+ except (ConnectionError , ProxyError , SSLError , Timeout ) as e :
783
+ # We couldn't connect to the target url, raise error
784
+ log .debug (f'Connection Error calling: { url } .{ f"Using proxy { self .proxy } " if self .proxy else "" } ' )
785
+ raise e # re-raise exception
786
+ except HTTPError as e :
787
+ # Server response with 4XX or 5XX error status codes
788
+ if e .response .status_code == 401 and self ._token_expired_flag is False :
789
+ # This could be a token expired error.
790
+ if self .token_backend .token_is_expired ():
791
+ log .debug ('Oauth Token is expired' )
792
+ # Token has expired, try to refresh the token and try again on the next loop
793
+ self ._token_expired_flag = True
794
+ raise TokenExpiredError ('Oauth Token is expired' )
795
+
796
+ # try to extract the error message:
777
797
try :
778
- log .debug ('Requesting ({}) URL: {}' .format (method .upper (), url ))
779
- log .debug ('Request parameters: {}' .format (kwargs ))
780
- # auto_retry will occur inside this function call if enabled
781
- response = request_obj .request (method , url , ** kwargs )
782
- response .raise_for_status () # raise 4XX and 5XX error codes.
783
- log .debug ('Received response ({}) from URL {}' .format (
784
- response .status_code , response .url ))
785
- request_done = True
786
- return response
787
- except (ConnectionError , ProxyError , SSLError , Timeout ) as e :
788
- # We couldn't connect to the target url, raise error
789
- log .debug ('Connection Error calling: {}.{}'
790
- '' .format (url , ('Using proxy: {}' .format (self .proxy )
791
- if self .proxy else '' )))
792
- raise e # re-raise exception
793
- except TokenExpiredError as e :
794
- # Token has expired, try to refresh the token and try again on the next loop
795
- log .debug ('Oauth Token is expired' )
796
- if self .token_backend .token .is_long_lived is False and self .auth_flow_type == 'authorization' :
797
- raise e
798
- if token_refreshed :
799
- # Refresh token done but still TokenExpiredError raise
800
- raise RuntimeError ('Token Refresh Operation not working' )
801
- should_rt = self .token_backend .should_refresh_token (self )
802
- if should_rt is True :
803
- # The backend has checked that we can refresh the token
804
- if self .refresh_token () is False :
805
- raise RuntimeError ('Token Refresh Operation not working' )
806
- token_refreshed = True
807
- elif should_rt is False :
808
- # the token was refreshed by another instance and updated into
809
- # this instance, so: update the session token and
810
- # go back to the loop and try the request again.
811
- request_obj .token = self .token_backend .token
812
- else :
813
- # the refresh was performed by the tokend backend.
814
- token_refreshed = True
815
- except HTTPError as e :
816
- # Server response with 4XX or 5XX error status codes
817
-
818
- if e .response .status_code == 401 and self ._token_expired_flag is False :
819
- # This could be a token expired error.
820
- if self .token_backend .token_is_expired ():
821
- log .debug ('Oauth Token is expired' )
822
- # Token has expired, try to refresh the token and try again on the next loop
823
- if self .token_backend .token_is_long_lived is False and self .auth_flow_type == 'authorization' :
824
- raise e
825
-
826
- # try to extract the error message:
827
- try :
828
- error = response .json ()
829
- error_message = error .get ('error' , {}).get ('message' , '' )
830
- error_code = (
831
- error .get ("error" , {}).get ("innerError" , {}).get ("code" , "" )
832
- )
833
- except ValueError :
834
- error_message = ''
835
- error_code = ''
836
-
837
- status_code = int (e .response .status_code / 100 )
838
- if status_code == 4 :
839
- # Client Error
840
- # Logged as error. Could be a library error or Api changes
841
- log .error (
842
- "Client Error: {} | Error Message: {} | Error Code: {}" .format (
843
- str (e ), error_message , error_code
844
- )
845
- )
846
- else :
847
- # Server Error
848
- log .debug ('Server Error: {}' .format (str (e )))
849
- if self .raise_http_errors :
850
- if error_message :
851
- raise HTTPError ('{} | Error Message: {}' .format (e .args [0 ], error_message ),
852
- response = response ) from None
853
- else :
854
- raise e
798
+ error = e .response .json ()
799
+ error_message = error .get ('error' , {}).get ('message' , '' )
800
+ error_code = (
801
+ error .get ("error" , {}).get ("innerError" , {}).get ("code" , "" )
802
+ )
803
+ except ValueError :
804
+ error_message = ''
805
+ error_code = ''
806
+
807
+ status_code = int (e .response .status_code / 100 )
808
+ if status_code == 4 :
809
+ # Client Error
810
+ # Logged as error. Could be a library error or Api changes
811
+ log .error (f'Client Error: { e } | Error Message: { error_message } | Error Code: { error_code } ' )
812
+ else :
813
+ # Server Error
814
+ log .debug (f'Server Error: { e } ' )
815
+ if self .raise_http_errors :
816
+ if error_message :
817
+ raise HTTPError (f'{ e .args [0 ]} | Error Message: { error_message } ' , response = e .response ) from None
855
818
else :
856
- return e .response
857
- except RequestException as e :
858
- # catch any other exception raised by requests
859
- log .debug ('Request Exception: {}' .format (str (e )))
860
- raise e
819
+ raise e
820
+ else :
821
+ return e .response
822
+ except RequestException as e :
823
+ # catch any other exception raised by requests
824
+ log .debug (f'Request Exception: { e } ' )
825
+ raise e
861
826
862
827
def naive_request (self , url , method , ** kwargs ):
863
828
""" Makes a request to url using an without oauth authorization
@@ -872,6 +837,7 @@ def naive_request(self, url, method, **kwargs):
872
837
if self .naive_session is None :
873
838
# lazy creation of a naive session
874
839
self .naive_session = self .get_naive_session ()
840
+
875
841
return self ._internal_request (self .naive_session , url , method , ** kwargs )
876
842
877
843
def oauth_request (self , url , method , ** kwargs ):
@@ -889,10 +855,15 @@ def oauth_request(self, url, method, **kwargs):
889
855
890
856
try :
891
857
return self ._internal_request (self .session , url , method , ** kwargs )
892
- except TokenExpiredError :
858
+ except TokenExpiredError as e :
893
859
# refresh and try again the request!
894
- self .refresh_token ()
895
- return self ._internal_request (self .session , url , method , ** kwargs )
860
+ try :
861
+ if self .refresh_token ():
862
+ return self ._internal_request (self .session , url , method , ** kwargs )
863
+ else :
864
+ raise e
865
+ finally :
866
+ self ._token_expired_flag = False
896
867
897
868
def get (self , url , params = None , ** kwargs ):
898
869
""" Shorthand for self.oauth_request(url, 'get')
0 commit comments