@@ -104,30 +104,16 @@ def template_dict(dictionnary, **kwargs):
104104 return ret
105105
106106
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
107+ def format_template (template , ** kwargs ):
108+ """ Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
118109 if template is None :
119110 return None
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
111+ placeholders = re .findall (r'{{([a-zA-Z\-\_]*)}}' , template )
112+ formated = template
113+ for placeholder in placeholders :
114+ replacement = kwargs .get (placeholder , "" )
115+ formated = formated .replace ("{{{{{}}}}}" .format (placeholder ), str (replacement ))
116+ return formated
131117
132118
133119def is_string (data ):
0 commit comments