Skip to content

Commit 3a3d4f8

Browse files
committed
Generator support for URL parameters
Some endpoint families require additional parameters to be passed as part of the URL. Example: ```https://webexapis.com/v1/meetings/{meetingId}/registrants``` This commit adds support for URL parameter(s). For endpoints requiring URL parameters, YAML template: * must include the parameter in the endpoint definition: ```endpoint: meetings/{meetingId}/registrants``` * must have URL parameters described under ```url_parameters``` key, including name, description, and type. * if an endpoint uses URL parameter, it is required for all calls within this endpoint
1 parent d76ab94 commit 3a3d4f8

File tree

3 files changed

+107
-15
lines changed

3 files changed

+107
-15
lines changed

generator/generate.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def render_api_class(env: Environment,
7979
out = tpl.render(name=descr['name'],
8080
endpoint=descr['endpoint'],
8181
object_type=descr['object_type'],
82+
url_parameters=descr['url_parameters'],
8283
query_parameters=descr['query_parameters'],
8384
create_parameters=create_parameters,
8485
update_parameters=update_parameters,
@@ -137,6 +138,10 @@ def main():
137138
raise MissingKeyError(f"Missing required key '{key}'")
138139
else:
139140
d = d.get(sub_key)
141+
142+
# Add empty url_parameters key if there are no URL parameters defined
143+
if 'url_parameters' not in descr.keys():
144+
descr['url_parameters'] = []
140145

141146
mixin_path = render_prop_mixin(env=env,
142147
name=descr['name'],

generator/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Jinja2==3.1.3
2+
MarkupSafe==2.1.5
3+
PyYAML==6.0.1

generator/templates/api.py

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def __init__(self, session, object_factory):
7373
self._object_factory = object_factory
7474
{% if "list" in methods %}
7575
@generator_container
76-
def list(self, {% for qp in query_parameters -%}
76+
def list(self, {% for up in url_parameters -%}
77+
{{ up['name'] }},
78+
{% endfor -%}
79+
{% for qp in query_parameters -%}
7780
{{ qp['name'] }}{% if qp['optional'] %}=None{% endif %},
7881
{% endfor -%}
7982
headers={},
@@ -94,6 +97,9 @@ def list(self, {% for qp in query_parameters -%}
9497
container.
9598
9699
Args:
100+
{% for up in url_parameters -%}
101+
{{ up['name'] }} ({{ up['type']}}): {{ up['description']}}
102+
{% endfor -%}
97103
{% for qp in query_parameters -%}
98104
{{ qp['name'] }} ({{ qp['type']}}): {{ qp['description']}}
99105
{% endfor -%}
@@ -110,6 +116,9 @@ def list(self, {% for qp in query_parameters -%}
110116
ApiError: If the Webex Teams cloud returns an error.
111117
112118
"""
119+
{% for up in url_parameters -%}
120+
check_type({{ up['name'] }}, {{ up['type'] }})
121+
{% endfor -%}
113122
{% for qp in query_parameters -%}
114123
check_type({{ qp['name'] }}, {{ qp['type'] }}{% if qp['optional'] %}, optional=True{% endif %})
115124
{% endfor %}
@@ -126,12 +135,22 @@ def list(self, {% for qp in query_parameters -%}
126135
params['{{qp['requestName']}}'] = params.pop("{{ qp['name'] }}")
127136
{% endif %}
128137
{%- endfor %}
138+
{%- if url_parameters %}
139+
{%- set ups = [] -%}
140+
{% for up in url_parameters|map(attribute='name') -%}
141+
{% set ups = ups.append( up+"="+up ) %}
142+
{%- endfor %}
143+
# Add URL parameters to the API endpoint
144+
request_url = API_ENDPOINT.format({{ ups|join(", ") }})
145+
{% else %}
146+
request_url = API_ENDPOINT
147+
{% endif %}
129148
# API request - get items
130149

131150
# Update headers
132151
for k, v in headers.items():
133152
self._session.headers[k] = v
134-
items = self._session.get_items(API_ENDPOINT, params=params)
153+
items = self._session.get_items(request_url, params=params)
135154

136155
# Remove headers
137156
for k, v in headers.items():
@@ -142,14 +161,20 @@ def list(self, {% for qp in query_parameters -%}
142161
yield self._object_factory(OBJECT_TYPE, item)
143162
{% endif %}
144163
{% if "create" in methods %}
145-
def create(self, {% for cp in create_parameters -%}
146-
{{ cp['name'] }}{% if cp['optional'] %}=None{% endif %},
164+
def create(self, {% for up in url_parameters -%}
165+
{{ up['name'] }},
166+
{% endfor -%}
167+
{% for cp in create_parameters -%}
168+
{{ cp['name'] }}{% if cp['optional'] %}=None{% endif %},
147169
{% endfor -%}
148170

149171
**request_parameters):
150172
"""Create a {{ object_type }}.
151173
152174
Args:
175+
{% for up in url_parameters -%}
176+
{{ up['name'] }} ({{ up['type']}}): {{ up['description']}}
177+
{% endfor -%}
153178
{% for cp in create_parameters -%}
154179
{{ cp['name'] }} ({{ cp['type']}}): {{ cp['description']}}
155180
{% endfor -%}
@@ -165,6 +190,9 @@ def create(self, {% for cp in create_parameters -%}
165190
ApiError: If the Webex Teams cloud returns an error.
166191
167192
"""
193+
{% for up in url_parameters -%}
194+
check_type({{ up['name'] }}, {{ up['type'] }})
195+
{% endfor -%}
168196
{% for cp in create_parameters -%}
169197
check_type({{ cp['name'] }}, {{ cp['type'] }}{% if cp['optional'] %}, optional=True{% endif %})
170198
{% endfor %}
@@ -181,18 +209,31 @@ def create(self, {% for cp in create_parameters -%}
181209
post_data['{{cp['requestName']}}'] = post_data.pop("{{ cp['name'] }}")
182210
{% endif %}
183211
{%- endfor %}
212+
{%- if url_parameters %}
213+
{%- set ups = [] -%}
214+
{% for up in url_parameters|map(attribute='name') -%}
215+
{% set ups = ups.append( up+"="+up ) %}
216+
{%- endfor %}
217+
# Add URL parameters to the API endpoint
218+
request_url = API_ENDPOINT.format({{ ups|join(", ") }})
219+
{% else %}
220+
request_url = API_ENDPOINT
221+
{% endif %}
184222
# API request
185-
json_data = self._session.post(API_ENDPOINT, json=post_data)
223+
json_data = self._session.post(request_url, json=post_data)
186224

187225
# Return a membership object created from the response JSON data
188226
return self._object_factory(OBJECT_TYPE, json_data)
189227
{% endif %}
190228

191229
{% if "get" in methods %}
192-
def get(self, {{ object_type }}Id):
230+
def get(self, {% for up in url_parameters %}{{up['name']}}, {% endfor %}{{ object_type }}Id):
193231
"""Get details for a {{ object_type }}, by ID.
194232
195233
Args:
234+
{% for up in url_parameters -%}
235+
{{up['name']}} ({{ up['type']}}): {{ up['description']}}
236+
{% endfor -%}
196237
{{ object_type }}Id(basestring): The {{ object_type }} ID.
197238
198239
Returns:
@@ -204,35 +245,64 @@ def get(self, {{ object_type }}Id):
204245
ApiError: If the Webex Teams cloud returns an error.
205246
206247
"""
248+
{% for up in url_parameters -%}
249+
check_type({{ up['name'] }}, {{ up['type'] }})
250+
{% endfor -%}
207251
check_type({{ object_type }}Id, basestring)
208-
252+
{%- if url_parameters %}
253+
{%- set ups = [] -%}
254+
{% for up in url_parameters|map(attribute='name') -%}
255+
{% set ups = ups.append( up+"="+up ) %}
256+
{%- endfor %}
257+
258+
# Add URL parameters to the API endpoint
259+
request_url = API_ENDPOINT.format({{ ups|join(", ") }})
260+
{% else %}
261+
request_url = API_ENDPOINT
262+
{% endif %}
209263
# API request
210-
json_data = self._session.get(API_ENDPOINT + '/' + {{ object_type }}Id)
264+
json_data = self._session.get(request_url + '/' + {{ object_type }}Id)
211265

212266
# Return a membership object created from the response JSON data
213267
return self._object_factory(OBJECT_TYPE, json_data)
214268
{% endif %}
215269

216270
{% if "delete" in methods %}
217-
def delete(self, {{ object_type }}Id):
271+
def delete(self, {% for up in url_parameters %}{{up['name']}}, {% endfor %}{{ object_type }}Id):
218272
"""Delete a {{ object_type }}, by ID.
219273
220274
Args:
275+
{% for up in url_parameters -%}
276+
{{up['name']}} ({{ up['type']}}): {{ up['description']}}
277+
{% endfor -%}
221278
{{ object_type }}Id(basestring): The {{ object_type }} ID.
222279
223280
Raises:
224281
TypeError: If the parameter types are incorrect.
225282
ApiError: If the Webex Teams cloud returns an error.
226283
227284
"""
285+
{% for up in url_parameters -%}
286+
check_type({{ up['name'] }}, {{ up['type'] }})
287+
{% endfor -%}
228288
check_type({{ object_type }}Id, basestring)
229-
289+
{%- if url_parameters %}
290+
{%- set ups = [] -%}
291+
{% for up in url_parameters|map(attribute='name') -%}
292+
{% set ups = ups.append( up+"="+up ) %}
293+
{%- endfor %}
294+
295+
# Add URL parameters to the API endpoint
296+
request_url = API_ENDPOINT.format({{ ups|join(", ") }})
297+
{% else %}
298+
request_url = API_ENDPOINT
299+
{% endif %}
230300
# API request
231-
self._session.delete(API_ENDPOINT + '/' + {{ object_type }}Id)
301+
self._session.delete(request_url + '/' + {{ object_type }}Id)
232302
{% endif %}
233303

234304
{% if "update" in methods %}
235-
def update(self, {{ object_type }}Id,
305+
def update(self, {% for up in url_parameters %}{{up['name']}}, {% endfor %}{object_type }}Id,
236306
{% for up in update_parameters -%}
237307
{{ up['name'] }}{% if up['optional'] %}=None{% endif %},
238308
{% endfor -%}
@@ -241,6 +311,9 @@ def update(self, {{ object_type }}Id,
241311
"""Update properties for a {{ object_type }}, by ID.
242312
243313
Args:
314+
{% for up in url_parameters -%}
315+
{{up['name']}} ({{ up['type']}}): {{ up['description']}}
316+
{% endfor -%}
244317
{{ object_type }}Id(basestring): The {{ object_type }} ID.
245318
{% for up in update_parameters -%}
246319
{{ up['name'] }} ({{ up['type']}}): {{ up['description']}}
@@ -258,6 +331,9 @@ def update(self, {{ object_type }}Id,
258331
259332
"""
260333
check_type({{ object_type }}Id, basestring)
334+
{% for up in url_parameters -%}
335+
check_type({{ up['name'] }}, {{ up['type'] }})
336+
{% endfor -%}
261337
{% for up in update_parameters -%}
262338
check_type({{ up['name'] }}, {{ up['type'] }}{% if up['optional'] %}, optional=True{% endif %})
263339
{% endfor %}
@@ -267,16 +343,24 @@ def update(self, {{ object_type }}Id,
267343
{{ up['name'] }}={{ up['name'] }},
268344
{% endfor %}
269345
)
270-
271346
{% for up in update_parameters -%}
272347
{% if up['requestName'] %}
273348
if {{ up['name'] }}:
274349
put_data['{{up['requestName']}}'] = put_data.pop("{{ up['name'] }}")
275350
{% endif %}
276351
{%- endfor %}
277-
352+
{%- if url_parameters %}
353+
{%- set ups = [] -%}
354+
{% for up in url_parameters|map(attribute='name') -%}
355+
{% set ups = ups.append( up+"="+up ) %}
356+
{%- endfor %}
357+
# Add URL parameters to the API endpoint
358+
request_url = API_ENDPOINT.format({{ ups|join(", ") }})
359+
{% else %}
360+
request_url = API_ENDPOINT
361+
{% endif %}
278362
# API request
279-
json_data = self._session.put(API_ENDPOINT + '/' + {{ object_type }}Id,
363+
json_data = self._session.put(request_url + '/' + {{ object_type }}Id,
280364
json=put_data)
281365

282366
# Return a membership object created from the response JSON data

0 commit comments

Comments
 (0)