diff --git a/python-lib/dku_utils.py b/python-lib/dku_utils.py index 834b2af..eb170c4 100644 --- a/python-lib/dku_utils.py +++ b/python-lib/dku_utils.py @@ -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 @@ -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): @@ -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): diff --git a/python-lib/rest_api_client.py b/python-lib/rest_api_client.py index 0b467ce..efaa2eb 100644 --- a/python-lib/rest_api_client.py +++ b/python-lib/rest_api_client.py @@ -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) @@ -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):