Skip to content

Commit 4e2de29

Browse files
authored
Merge pull request #18 from dataiku/feature/sc-76763-add-follow-authorization-header-option
Add "Follow authorization header" option
2 parents 819c556 + 4129748 commit 4e2de29

5 files changed

Lines changed: 31 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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
import time
3+
import copy
34
from pagination import Pagination
45
from safe_logger import SafeLogger
56
from 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")

tests/python/integration/test_scenario.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ def test_run_api_connect_search_path(user_dss_clients):
2727
dss_scenario.run(user_dss_clients, project_key=TEST_PROJECT_KEY, scenario_id="SearchPath")
2828

2929

30+
def test_run_api_connect_redirection(user_dss_clients):
31+
dss_scenario.run(user_dss_clients, project_key=TEST_PROJECT_KEY, scenario_id="REDIRECTION")
32+
33+
3034
def test_run_api_connect_check_sc_84465(user_dss_clients):
3135
dss_scenario.run(user_dss_clients, project_key=TEST_PROJECT_KEY, scenario_id="CHECKSC84465")

0 commit comments

Comments
 (0)