Skip to content

Commit efef9ff

Browse files
Merge pull request #56 from dataiku/feature/sc-248258api-connect-support-query-param-list
Support query param list for multivalued keys
2 parents d4cb6c3 + a634bbb commit efef9ff

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

python-lib/dku_utils.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import copy
33
import math
4+
from collections import defaultdict
45
from jsonpath_ng.ext import parse
56
from safe_logger import SafeLogger
67

@@ -9,7 +10,11 @@
910

1011

1112
def get_dku_key_values(endpoint_query_string):
12-
return {key_value.get("from"): key_value.get("to") for key_value in endpoint_query_string if key_value.get("from")}
13+
result = defaultdict(list)
14+
for kv in endpoint_query_string:
15+
if kv.get('from') and kv.get('to'):
16+
result[kv['from'].strip()].append(kv['to'].strip())
17+
return dict(result)
1318

1419

1520
def get_endpoint_parameters(configuration):
@@ -98,15 +103,30 @@ def template_dict(dictionnary, **kwargs):
98103
return ret
99104

100105

101-
def format_template(template, **kwargs):
102-
""" Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
106+
def format_template(template, allow_list=False, **kwargs):
107+
"""
108+
Replace {{key}} in template with the value(s) in the kwargs dictionnary.
109+
If allow_list is False, list inputs will be joined into a comma-separated string (for headers).
110+
If allow_list is True, lists will be returned as lists (for query params).
111+
"""
112+
def replace_in(template):
113+
formated = template
114+
for key, value in kwargs.items():
115+
formated = formated.replace(f"{{{{{key}}}}}", str(value))
116+
return formated
103117
if template is None:
104118
return None
105-
formated = template
106-
for key in kwargs:
107-
replacement = kwargs.get(key, "")
108-
formated = formated.replace("{{{{{}}}}}".format(key), str(replacement))
109-
return formated
119+
elif isinstance(template, list):
120+
replaced_list = [replace_in(item) for item in template]
121+
if allow_list:
122+
return replaced_list
123+
else:
124+
# To handle headers
125+
return ", ".join(replaced_list)
126+
elif isinstance(template, str):
127+
return replace_in(template)
128+
else:
129+
return template
110130

111131

112132
def is_string(data):

python-lib/rest_api_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, credential, secure_credentials, endpoint, custom_key_values={
4646
endpoint_headers = endpoint.get("endpoint_headers", "")
4747
self.endpoint_headers = self.get_params(endpoint_headers, self.presets_variables)
4848

49-
self.params = self.get_params(self.endpoint_query_string, self.presets_variables)
49+
self.params = self.get_params(self.endpoint_query_string, self.presets_variables, True)
5050

5151
self.extraction_key = endpoint.get("extraction_key", None)
5252

@@ -195,11 +195,11 @@ def set_metadata(self, metadata_name, value):
195195
self.metadata["dku_{}".format(metadata_name)] = value
196196

197197
@staticmethod
198-
def get_params(endpoint_query_string, keywords):
198+
def get_params(endpoint_query_string, keywords, allow_list=False):
199199
templated_query_string = get_dku_key_values(endpoint_query_string)
200200
ret = {}
201201
for key in templated_query_string:
202-
ret.update({key: format_template(templated_query_string.get(key, ""), **keywords) or ""})
202+
ret.update({key: format_template(templated_query_string.get(key, ""), allow_list=allow_list, **keywords) or ""})
203203
return ret
204204

205205
def has_more_data(self):

0 commit comments

Comments
 (0)