Skip to content

Commit e2871a5

Browse files
committed
support query with multivalued keys
1 parent d6340a8 commit e2871a5

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

python-lib/dku_utils.py

Lines changed: 29 additions & 9 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,12 @@
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)
18+
1319

1420

1521
def get_endpoint_parameters(configuration):
@@ -98,16 +104,30 @@ def template_dict(dictionnary, **kwargs):
98104
return ret
99105

100106

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

112132

113133
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)