MCPcopy
hub / github.com/openai/openai-python / path_template

Function path_template

src/openai/_utils/_path.py:78–127  ·  src/openai/_utils/_path.py::path_template

Interpolate {name} placeholders in `template` from keyword arguments. Args: template: The template string containing {name} placeholders. **kwargs: Keyword arguments to interpolate into the template. Returns: The template with placeholders interpolated and percent-e

(template: str, /, **kwargs: Any)

Source from the content-addressed store, hash-verified

76
77
78def path_template(template: str, /, **kwargs: Any) -> str:
79 class="st">"""Interpolate {name} placeholders in `template` from keyword arguments.
80
81 Args:
82 template: The template string containing {name} placeholders.
83 **kwargs: Keyword arguments to interpolate into the template.
84
85 Returns:
86 The template with placeholders interpolated and percent-encoded.
87
88 Safe characters for percent-encoding are dependent on the URI component.
89 Placeholders in path and fragment portions are percent-encoded where the `segment`
90 and `fragment` sets from RFC 3986 respectively are considered safe.
91 Placeholders in the query portion are percent-encoded where the `query` set from
92 RFC 3986 §3.3 is considered safe except for = and & characters.
93
94 Raises:
95 KeyError: If a placeholder is not found in `kwargs`.
96 ValueError: If resulting path contains /./ or /../ segments (including percent-encoded dot-segments).
97 class="st">"""
98 class="cm"># Split the template into path, query, and fragment portions.
99 fragment_template: str | None = None
100 query_template: str | None = None
101
102 rest = template
103 if class="st">"class="cm">#" in rest:
104 rest, fragment_template = rest.split(class="st">"class="cm">#", 1)
105 if class="st">"?" in rest:
106 rest, query_template = rest.split(class="st">"?", 1)
107 path_template = rest
108
109 class="cm"># Interpolate each portion with the appropriate quoting rules.
110 path_result = _interpolate(path_template, kwargs, _quote_path_segment_part)
111
112 class="cm"># Reject dot-segments (. and ..) in the final assembled path. The check
113 class="cm"># runs after interpolation so that adjacent placeholders or a mix of static
114 class="cm"># text and placeholders that together form a dot-segment are caught.
115 class="cm"># Also reject percent-encoded dot-segments to protect against incorrectly
116 class="cm"># implemented normalization in servers/proxies.
117 for segment in path_result.split(class="st">"/"):
118 if _DOT_SEGMENT_RE.match(segment):
119 raise ValueError(fclass="st">"Constructed path {path_result!r} contains dot-segment {segment!r} which is not allowed")
120
121 result = path_result
122 if query_template is not None:
123 result += class="st">"?" + _interpolate(query_template, kwargs, _quote_query_part)
124 if fragment_template is not None:
125 result += class="st">"class="cm">#" + _interpolate(fragment_template, kwargs, _quote_fragment_part)
126
127 return result

Callers 15

test_interpolationFunction · 0.90
retrieveMethod · 0.85
deleteMethod · 0.85
download_contentMethod · 0.85
get_characterMethod · 0.85
remixMethod · 0.85
retrieveMethod · 0.85
deleteMethod · 0.85
download_contentMethod · 0.85
get_characterMethod · 0.85

Calls 1

_interpolateFunction · 0.85

Tested by 3

test_interpolationFunction · 0.72