-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.handlebars
589 lines (532 loc) · 14.6 KB
/
endpoint.handlebars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# coding: utf-8
{{>partial_header}}
from dataclasses import dataclass
import typing_extensions
import urllib3
{{#with operation}}
{{#or headerParams bodyParam produces}}
from urllib3._collections import HTTPHeaderDict
{{/or}}
{{/with}}
from {{packageName}} import api_client, exceptions
{{> model_templates/imports_schema_types }}
{{> model_templates/imports_schemas }}
{{#unless isStub}}
from . import path
{{/unless}}
{{#with operation}}
{{#if queryParams}}
# query params
{{#each queryParams}}
{{#with schema}}
{{> model_templates/schema }}
{{/with}}
{{/each}}
{{#unless isStub}}
RequestRequiredQueryParams = typing_extensions.TypedDict(
'RequestRequiredQueryParams',
{
{{#each queryParams}}
{{#if required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/if}}
{{/each}}
}
)
RequestOptionalQueryParams = typing_extensions.TypedDict(
'RequestOptionalQueryParams',
{
{{#each queryParams}}
{{#unless required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/unless}}
{{/each}}
},
total=False
)
class RequestQueryParams(RequestRequiredQueryParams, RequestOptionalQueryParams):
pass
{{#each queryParams}}
{{> endpoint_parameter }}
{{/each}}
{{/unless}}
{{/if}}
{{#if headerParams}}
# header params
{{#each headerParams}}
{{#with schema}}
{{> model_templates/schema }}
{{/with}}
{{/each}}
{{#unless isStub}}
RequestRequiredHeaderParams = typing_extensions.TypedDict(
'RequestRequiredHeaderParams',
{
{{#each headerParams}}
{{#if required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/if}}
{{/each}}
}
)
RequestOptionalHeaderParams = typing_extensions.TypedDict(
'RequestOptionalHeaderParams',
{
{{#each headerParams}}
{{#unless required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/unless}}
{{/each}}
},
total=False
)
class RequestHeaderParams(RequestRequiredHeaderParams, RequestOptionalHeaderParams):
pass
{{#each headerParams}}
{{> endpoint_parameter }}
{{/each}}
{{/unless}}
{{/if}}
{{#if pathParams}}
# path params
{{#each pathParams}}
{{#with schema}}
{{> model_templates/schema }}
{{/with}}
{{/each}}
{{#unless isStub}}
RequestRequiredPathParams = typing_extensions.TypedDict(
'RequestRequiredPathParams',
{
{{#each pathParams}}
{{#if required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/if}}
{{/each}}
}
)
RequestOptionalPathParams = typing_extensions.TypedDict(
'RequestOptionalPathParams',
{
{{#each pathParams}}
{{#unless required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/unless}}
{{/each}}
},
total=False
)
class RequestPathParams(RequestRequiredPathParams, RequestOptionalPathParams):
pass
{{#each pathParams}}
{{> endpoint_parameter }}
{{/each}}
{{/unless}}
{{/if}}
{{#if cookieParams}}
# cookie params
{{#each cookieParams}}
{{#with schema}}
{{> model_templates/schema }}
{{/with}}
{{/each}}
{{#unless isStub}}
RequestRequiredCookieParams = typing_extensions.TypedDict(
'RequestRequiredCookieParams',
{
{{#each cookieParams}}
{{#if required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/if}}
{{/each}}
}
)
RequestOptionalCookieParams = typing_extensions.TypedDict(
'RequestOptionalCookieParams',
{
{{#each cookieParams}}
{{#unless required}}
'{{baseName}}': {{#with schema}}typing.Union[{{baseName}}, {{> model_templates/schema_python_types }}],{{/with}}
{{/unless}}
{{/each}}
},
total=False
)
class RequestCookieParams(RequestRequiredCookieParams, RequestOptionalCookieParams):
pass
{{#each cookieParams}}
{{> endpoint_parameter }}
{{/each}}
{{/unless}}
{{/if}}
{{#with bodyParam}}
# body param
{{#each content}}
{{#with this.schema}}
{{> model_templates/schema }}
{{/with}}
{{/each}}
{{#unless isStub}}
request_body_{{paramName}} = api_client.RequestBody(
content={
{{#each content}}
'{{{@key}}}': api_client.MediaType({{#if this.schema}}
schema={{this.schema.baseName}}{{/if}}),
{{/each}}
},
{{#if required}}
required=True,
{{/if}}
)
{{/unless}}
{{/with}}
{{#unless isStub}}
{{#each authMethods}}
{{#if @first}}
_auth = [
{{/if}}
'{{name}}',
{{#if @last}}
]
{{/if}}
{{/each}}
{{#each servers}}
{{#if @first}}
_servers = (
{{/if}}
{
'url': "{{{url}}}",
'description': "{{#unless description}}No description provided{{else}}{{{description}}}{{/unless}}",
{{#each variables}}
{{#if @first}}
'variables': {
{{/if}}
'{{{name}}}': {
'description': "{{#unless description}}No description provided{{else}}{{{description}}}{{/unless}}",
'default_value': "{{{defaultValue}}}",
{{#each enumValues}}
{{#if @first}}
'enum_values': [
{{/if}}
"{{{.}}}"{{#unless @last}},{{/unless}}
{{#if @last}}
]
{{/if}}
{{/each}}
}{{#unless @last}},{{/unless}}
{{#if @last}}
}
{{/if}}
{{/each}}
},
{{#if @last}}
)
{{/if}}
{{/each}}
{{/unless}}
{{#each responses}}
{{#each responseHeaders}}
{{#with schema}}
{{> model_templates/schema }}
{{/with}}
{{#unless isStub}}
{{paramName}}_parameter = api_client.HeaderParameter(
name="{{baseName}}",
{{#if style}}
style=api_client.ParameterStyle.{{style}},
{{/if}}
{{#if schema}}
{{#with schema}}
schema={{baseName}},
{{/with}}
{{/if}}
{{#if required}}
required=True,
{{/if}}
{{#if isExplode}}
explode=True,
{{/if}}
)
{{/unless}}
{{/each}}
{{#each content}}
{{#with this.schema}}
{{> model_templates/schema }}
{{/with}}
{{/each}}
{{#unless isStub}}
{{#if responseHeaders}}
ResponseHeadersFor{{code}} = typing_extensions.TypedDict(
'ResponseHeadersFor{{code}}',
{
{{#each responseHeaders}}
'{{baseName}}': {{#with schema}}{{baseName}},{{/with}}
{{/each}}
}
)
{{/if}}
@dataclass
{{#if isDefault}}
class ApiResponseForDefault(api_client.ApiResponse):
{{else}}
class ApiResponseFor{{code}}(api_client.ApiResponse):
{{/if}}
response: urllib3.HTTPResponse
{{#and responseHeaders content}}
body: typing.Union[
{{#each content}}
{{#if this.schema}}
{{this.schema.baseName}},
{{else}}
schemas.Unset,
{{/if}}
{{/each}}
]
headers: ResponseHeadersFor{{code}}
{{else}}
{{#or responseHeaders content}}
{{#if responseHeaders}}
headers: ResponseHeadersFor{{code}}
body: schemas.Unset = schemas.unset
{{else}}
body: typing.Union[
{{#each content}}
{{#if this.schema}}
{{this.schema.baseName}},
{{else}}
schemas.Unset,
{{/if}}
{{/each}}
]
headers: schemas.Unset = schemas.unset
{{/if}}
{{/or}}
{{/and}}
{{#unless responseHeaders}}
{{#unless content}}
body: schemas.Unset = schemas.unset
headers: schemas.Unset = schemas.unset
{{/unless}}
{{/unless}}
{{#if isDefault}}
_response_for_default = api_client.OpenApiResponse(
response_cls=ApiResponseForDefault,
{{else}}
_response_for_{{code}} = api_client.OpenApiResponse(
response_cls=ApiResponseFor{{code}},
{{/if}}
{{#each content}}
{{#if @first}}
content={
{{/if}}
'{{{@key}}}': api_client.MediaType({{#if this.schema}}
schema={{this.schema.baseName}}{{/if}}),
{{#if @last}}
},
{{/if}}
{{/each}}
{{#if responseHeaders}}
headers=[
{{#each responseHeaders}}
{{paramName}}_parameter,
{{/each}}
]
{{/if}}
)
{{/unless}}
{{/each}}
{{#unless isStub}}
_status_code_to_response = {
{{#each responses}}
{{#if isDefault}}
'default': _response_for_default,
{{else}}
'{{code}}': _response_for_{{code}},
{{/if}}
{{/each}}
}
{{/unless}}
{{#each produces}}
{{#if @first}}
_all_accept_content_types = (
{{/if}}
'{{{this.mediaType}}}',
{{#if @last}}
)
{{/if}}
{{/each}}
class BaseApi(api_client.Api):
@typing.overload
def _{{operationId}}_oapg(
{{> endpoint_args isOverload=true skipDeserialization="False"}}
@typing.overload
def _{{operationId}}_oapg(
{{> endpoint_args isOverload=true skipDeserialization="True"}}
@typing.overload
def _{{operationId}}_oapg(
{{> endpoint_args isOverload=true skipDeserialization="null"}}
def _{{operationId}}_oapg(
{{> endpoint_args isOverload=false skipDeserialization="null"}}
"""
{{#if summary}}
{{summary}}
{{/if}}
:param skip_deserialization: If true then api_response.response will be set but
api_response.body and api_response.headers will not be deserialized into schema
class instances
"""
{{#if queryParams}}
self._verify_typed_dict_inputs_oapg(RequestQueryParams, query_params)
{{/if}}
{{#if headerParams}}
self._verify_typed_dict_inputs_oapg(RequestHeaderParams, header_params)
{{/if}}
{{#if pathParams}}
self._verify_typed_dict_inputs_oapg(RequestPathParams, path_params)
{{/if}}
{{#if cookieParams}}
self._verify_typed_dict_inputs_oapg(RequestCookieParams, cookie_params)
{{/if}}
used_path = path.value
{{#if pathParams}}
_path_params = {}
for parameter in (
{{#each pathParams}}
request_path_{{paramName}},
{{/each}}
):
parameter_data = path_params.get(parameter.name, schemas.unset)
if parameter_data is schemas.unset:
continue
serialized_data = parameter.serialize(parameter_data)
_path_params.update(serialized_data)
for k, v in _path_params.items():
used_path = used_path.replace('{%s}' % k, v)
{{/if}}
{{#if queryParams}}
prefix_separator_iterator = None
for parameter in (
{{#each queryParams}}
request_query_{{paramName}},
{{/each}}
):
parameter_data = query_params.get(parameter.name, schemas.unset)
if parameter_data is schemas.unset:
continue
if prefix_separator_iterator is None:
prefix_separator_iterator = parameter.get_prefix_separator_iterator()
serialized_data = parameter.serialize(parameter_data, prefix_separator_iterator)
for serialized_value in serialized_data.values():
used_path += serialized_value
{{/if}}
{{#or headerParams bodyParam produces}}
_headers = HTTPHeaderDict()
{{else}}
{{/or}}
{{#if headerParams}}
for parameter in (
{{#each headerParams}}
request_header_{{paramName}},
{{/each}}
):
parameter_data = header_params.get(parameter.name, schemas.unset)
if parameter_data is schemas.unset:
continue
serialized_data = parameter.serialize(parameter_data)
_headers.extend(serialized_data)
{{/if}}
# TODO add cookie handling
{{#if produces}}
if accept_content_types:
for accept_content_type in accept_content_types:
_headers.add('Accept', accept_content_type)
{{/if}}
{{#with bodyParam}}
{{#if required}}
if body is schemas.unset:
raise exceptions.ApiValueError(
'The required body parameter has an invalid value of: unset. Set a valid value instead')
{{/if}}
_fields = None
_body = None
{{#if required}}
{{> endpoint_body_serialization }}
{{else}}
if body is not schemas.unset:
{{> endpoint_body_serialization }}
{{/if}}
{{/with}}
{{#if servers}}
host = self._get_host_oapg('{{operationId}}', _servers, host_index)
{{/if}}
response = self.api_client.call_api(
resource_path=used_path,
method='{{httpMethod}}'.upper(),
{{#or headerParams bodyParam produces}}
headers=_headers,
{{/or}}
{{#if bodyParam}}
fields=_fields,
body=_body,
{{/if}}
{{#if hasAuthMethods}}
auth_settings=_auth,
{{/if}}
{{#if servers}}
host=host,
{{/if}}
stream=stream,
timeout=timeout,
)
if skip_deserialization:
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
else:
response_for_status = _status_code_to_response.get(str(response.status))
if response_for_status:
api_response = response_for_status.deserialize(response, self.api_client.configuration)
else:
{{#if hasDefaultResponse}}
default_response = _status_code_to_response.get('default')
if default_response:
api_response = default_response.deserialize(response, self.api_client.configuration)
else:
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
{{else}}
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
{{/if}}
if not 200 <= response.status <= 299:
raise exceptions.ApiException(api_response=api_response)
return api_response
class {{operationIdCamelCase}}(BaseApi):
# this class is used by api classes that refer to endpoints with operationId fn names
@typing.overload
def {{operationId}}(
{{> endpoint_args isOverload=true skipDeserialization="False"}}
@typing.overload
def {{operationId}}(
{{> endpoint_args isOverload=true skipDeserialization="True"}}
@typing.overload
def {{operationId}}(
{{> endpoint_args isOverload=true skipDeserialization="null"}}
def {{operationId}}(
{{> endpoint_args isOverload=false skipDeserialization="null"}}
return self._{{operationId}}_oapg(
{{> endpoint_args_passed }}
)
class ApiFor{{httpMethod}}(BaseApi):
# this class is used by api classes that refer to endpoints by path and http method names
@typing.overload
def {{httpMethod}}(
{{> endpoint_args isOverload=true skipDeserialization="False"}}
@typing.overload
def {{httpMethod}}(
{{> endpoint_args isOverload=true skipDeserialization="True"}}
@typing.overload
def {{httpMethod}}(
{{> endpoint_args isOverload=true skipDeserialization="null"}}
def {{httpMethod}}(
{{> endpoint_args isOverload=false skipDeserialization="null"}}
return self._{{operationId}}_oapg(
{{> endpoint_args_passed }}
)
{{/with}}