Skip to content

Support query param list for multivalued keys #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions python-lib/dku_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import copy
import math
from collections import defaultdict
from jsonpath_ng.ext import parse
from safe_logger import SafeLogger

Expand All @@ -9,7 +10,11 @@


def get_dku_key_values(endpoint_query_string):
return {key_value.get("from"): key_value.get("to") for key_value in endpoint_query_string if key_value.get("from")}
result = defaultdict(list)
for kv in endpoint_query_string:
if kv.get('from') and kv.get('to'):
result[kv['from'].strip()].append(kv['to'].strip())
return dict(result)


def get_endpoint_parameters(configuration):
Expand Down Expand Up @@ -98,15 +103,30 @@ def template_dict(dictionnary, **kwargs):
return ret


def format_template(template, **kwargs):
""" Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
def format_template(template, allow_list=False, **kwargs):
"""
Replace {{key}} in template with the value(s) in the kwargs dictionnary.
If allow_list is False, list inputs will be joined into a comma-separated string (for headers).
If allow_list is True, lists will be returned as lists (for query params).
"""
def replace_in(template):
formated = template
for key, value in kwargs.items():
formated = formated.replace(f"{{{{{key}}}}}", str(value))
return formated
if template is None:
return None
formated = template
for key in kwargs:
replacement = kwargs.get(key, "")
formated = formated.replace("{{{{{}}}}}".format(key), str(replacement))
return formated
elif isinstance(template, list):
replaced_list = [replace_in(item) for item in template]
if allow_list:
return replaced_list
else:
# To handle headers
return ", ".join(replaced_list)
elif isinstance(template, str):
return replace_in(template)
else:
return template


def is_string(data):
Expand Down
6 changes: 3 additions & 3 deletions python-lib/rest_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, credential, secure_credentials, endpoint, custom_key_values={
endpoint_headers = endpoint.get("endpoint_headers", "")
self.endpoint_headers = self.get_params(endpoint_headers, self.presets_variables)

self.params = self.get_params(self.endpoint_query_string, self.presets_variables)
self.params = self.get_params(self.endpoint_query_string, self.presets_variables, True)

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

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

@staticmethod
def get_params(endpoint_query_string, keywords):
def get_params(endpoint_query_string, keywords, allow_list=False):
templated_query_string = get_dku_key_values(endpoint_query_string)
ret = {}
for key in templated_query_string:
ret.update({key: format_template(templated_query_string.get(key, ""), **keywords) or ""})
ret.update({key: format_template(templated_query_string.get(key, ""), allow_list=allow_list, **keywords) or ""})
return ret

def has_more_data(self):
Expand Down