Skip to content

Commit 475a2bc

Browse files
committed
Add retry with auth header following redirect
1 parent 805e14d commit 475a2bc

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

custom-recipes/api-connect/recipe.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@
227227
"type": "BOOLEAN",
228228
"defaultValue": false
229229
},
230+
{
231+
"name": "redirect_auth_header",
232+
"label": "Redirect authorization header",
233+
"type": "BOOLEAN",
234+
"defaultValue": false
235+
},
230236
{
231237
"name": "display_metadata",
232238
"label": "Display metadata",

python-connectors/api-connect_dataset/connector.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@
186186
"type": "BOOLEAN",
187187
"defaultValue": false
188188
},
189+
{
190+
"name": "redirect_auth_header",
191+
"label": "Redirect authorization header",
192+
"type": "BOOLEAN",
193+
"defaultValue": false
194+
},
189195
{
190196
"name": "display_metadata",
191197
"label": "Display metadata",

python-lib/dku_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def get_endpoint_parameters(configuration):
1919
"extraction_key",
2020
"raw_output",
2121
"ignore_ssl_check",
22+
"redirect_auth_header",
2223
"timeout",
2324
"requests_per_minute",
2425
"pagination_type",

python-lib/rest_api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(self, credential, endpoint, custom_key_values={}):
7575
self.requests_kwargs.update({"verify": False})
7676
else:
7777
self.requests_kwargs.update({"verify": True})
78+
self.redirect_auth_header = endpoint.get("redirect_auth_header", False)
7879
self.timeout = endpoint.get("timeout", -1)
7980
if self.timeout > 0:
8081
self.requests_kwargs.update({"timeout": self.timeout})
@@ -142,7 +143,7 @@ def request(self, method, url, can_raise_exeption=True, **kwargs):
142143
raise RestAPIClientError("The api-connect plugin is stuck in a loop. Please check the pagination parameters.")
143144
try:
144145
request_start_time = time.time()
145-
response = requests.request(method, url, **kwargs)
146+
response = self.request_with_redirect_retry(method, url, **kwargs)
146147
request_finish_time = time.time()
147148
except Exception as err:
148149
self.pagination.is_last_batch_empty = True
@@ -169,6 +170,14 @@ def request(self, method, url, can_raise_exeption=True, **kwargs):
169170
self.pagination.update_next_page(json_response, response.links)
170171
return json_response
171172

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+
response = requests.request(method, response.url, **kwargs)
179+
return response
180+
172181
def paginated_api_call(self, can_raise_exeption=True):
173182
pagination_params = self.pagination.get_params()
174183
params = self.requests_kwargs.get("params")

0 commit comments

Comments
 (0)