|
1 | 1 | import json |
2 | 2 | import copy |
3 | 3 | import math |
| 4 | +from collections import defaultdict |
4 | 5 | from jsonpath_ng.ext import parse |
5 | 6 | from safe_logger import SafeLogger |
6 | 7 |
|
|
9 | 10 |
|
10 | 11 |
|
11 | 12 | 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) |
13 | 18 |
|
14 | 19 |
|
15 | 20 | def get_endpoint_parameters(configuration): |
@@ -98,15 +103,30 @@ def template_dict(dictionnary, **kwargs): |
98 | 103 | return ret |
99 | 104 |
|
100 | 105 |
|
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 |
103 | 117 | if template is None: |
104 | 118 | 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 |
110 | 130 |
|
111 | 131 |
|
112 | 132 | def is_string(data): |
|
0 commit comments