11import requests
22import time
3+ import copy
34from pagination import Pagination
45from safe_logger import SafeLogger
56from loop_detector import LoopDetector
@@ -74,6 +75,7 @@ def __init__(self, credential, endpoint, custom_key_values={}):
7475 self .requests_kwargs .update ({"verify" : False })
7576 else :
7677 self .requests_kwargs .update ({"verify" : True })
78+ self .redirect_auth_header = endpoint .get ("redirect_auth_header" , False )
7779 self .timeout = endpoint .get ("timeout" , - 1 )
7880 if self .timeout > 0 :
7981 self .requests_kwargs .update ({"timeout" : self .timeout })
@@ -141,7 +143,7 @@ def request(self, method, url, can_raise_exeption=True, **kwargs):
141143 raise RestAPIClientError ("The api-connect plugin is stuck in a loop. Please check the pagination parameters." )
142144 try :
143145 request_start_time = time .time ()
144- response = requests . request (method , url , ** kwargs )
146+ response = self . request_with_redirect_retry (method , url , ** kwargs )
145147 request_finish_time = time .time ()
146148 except Exception as err :
147149 self .pagination .is_last_batch_empty = True
@@ -168,6 +170,17 @@ def request(self, method, url, can_raise_exeption=True, **kwargs):
168170 self .pagination .update_next_page (json_response , response .links )
169171 return json_response
170172
173+ def request_with_redirect_retry (self , method , url , ** kwargs ):
174+ # In case of redirection to another domain, the authorization header is not kept
175+ # If redirect_auth_header is true, another attempt is made with initial headers to the redirected url
176+ response = requests .request (method , url , ** kwargs )
177+ if self .redirect_auth_header and not response .url .startswith (url ):
178+ redirection_kwargs = copy .deepcopy (kwargs )
179+ redirection_kwargs .pop ("params" , None ) # params are contained in the redirected url
180+ logger .warning ("Redirection ! Accessing endpoint {} with initial authorization headers" .format (response .url ))
181+ response = requests .request (method , response .url , ** redirection_kwargs )
182+ return response
183+
171184 def paginated_api_call (self , can_raise_exeption = True ):
172185 pagination_params = self .pagination .get_params ()
173186 params = self .requests_kwargs .get ("params" )
0 commit comments