Skip to content

Commit ee571ce

Browse files
adminJaneBellaiche
authored andcommitted
Edited file 'python-lib/dku_utils.py'
1 parent 47e189d commit ee571ce

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

python-lib/dku_utils.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,30 @@ def template_dict(dictionnary, **kwargs):
9898
return ret
9999

100100

101-
def format_template(template, **kwargs):
102-
""" Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
101+
def format_template(template, allow_list=False, **kwargs):
102+
"""
103+
Replace {{key}} in template with the value(s) in the kwargs dictionnary.
104+
If allow_list is False, list inputs will be joined into a comma-separated string (for headers).
105+
If allow_list is True, lists will be returned as lists (for query params).
106+
"""
107+
def replace_in(template):
108+
formated = template
109+
for key, value in kwargs.items():
110+
formated = formated.replace(f"{{{{{key}}}}}", str(value))
111+
return formated
103112
if template is None:
104113
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
114+
elif isinstance(template, list):
115+
replaced_list = [replace_in(item) for item in template]
116+
if allow_list:
117+
return replaced_list
118+
else:
119+
# To handle headers
120+
return ", ".join(replaced_list)
121+
elif isinstance(template, str):
122+
return replace_in(template)
123+
else:
124+
return template
110125

111126

112127
def is_string(data):

0 commit comments

Comments
 (0)