Repository navigation
Expand file tree
/
Copy pathapi.mustache
More file actions
530 lines (482 loc) · 18.3 KB
/
Copy pathapi.mustache
File metadata and controls
530 lines (482 loc) · 18.3 KB
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
{{>partial_header}}
{{#x-python-has-json-header}}
import json
{{/x-python-has-json-header}}
import warnings
from pydantic import validate_call, Field, StrictFloat, StrictStr, StrictInt
from typing import Any, Dict, List, Optional, Tuple, Union
from typing_extensions import Annotated
{{#imports}}
{{import}}
{{/imports}}
from {{packageName}}.api_client import ApiClient, RequestSerialized
from {{packageName}}.api_response import ApiResponse
from {{packageName}}.rest import RESTResponseType
{{#useCustomTemplateCode}}
import io
{{/useCustomTemplateCode}}
{{#supportHttpxSync}}
from {{packageName}}.sync_helper import run_sync
{{/supportHttpxSync}}
{{#operations}}
class {{classname}}:
"""NOTE: This class is auto generated by OpenAPI Generator
Ref: https://openapi-generator.tech
Do not edit the class manually.
"""
def __init__(self, api_client=None) -> None:
{{#useIndependentImplicitClients}}
# api_client remains publicly assignable. Retain the client acquired at
# construction so reassignment cannot transfer or discard ownership.
if api_client is None:
api_client, owns_api_client = ApiClient._get_default_or_new()
else:
owns_api_client = False
{{/useIndependentImplicitClients}}
{{^useIndependentImplicitClients}}
if api_client is None:
api_client = ApiClient.get_default()
{{/useIndependentImplicitClients}}
self.api_client = api_client
{{#useIndependentImplicitClients}}
self._owned_api_client: Optional[ApiClient] = (
api_client if owns_api_client else None
)
{{#async}}
async def close(self) -> None:
owned_api_client = self._owned_api_client
self._owned_api_client = None
if owned_api_client is not None:
await owned_api_client.close()
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_value, traceback):
await self.close()
{{#supportHttpxSync}}
def close_sync(self) -> None:
run_sync(self.close())
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close_sync()
{{/supportHttpxSync}}
{{/async}}
{{^async}}
def close(self) -> None:
owned_api_client = self._owned_api_client
self._owned_api_client = None
if owned_api_client is not None:
owned_api_client.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
{{/async}}
{{/useIndependentImplicitClients}}
{{#operation}}
{{#compatibleWithPythonLegacyApi}}
{{^async}}
{{!
Synchronous urllib3 legacy flags can return a future or raw response. Keep the ordinary data
annotation precise because Kubernetes Watch inspects it:
https://github.com/kubernetes-client/python/blob/6cc5456d79dd8c0ccc9e275a0811aeb025714bea/kubernetes/watch/watch.py#L36-L48
}}
@validate_call
def {{operationId}}{{>partial_api_args_legacy}} -> {{{returnType}}}{{^returnType}}None{{/returnType}}:
{{/async}}
{{#async}}
@validate_call
async def {{operationId}}{{>partial_api_args}} -> {{{returnType}}}{{^returnType}}None{{/returnType}}:
{{/async}}
{{/compatibleWithPythonLegacyApi}}
{{^compatibleWithPythonLegacyApi}}
@validate_call
{{^useCustomTemplateCode}}
{{#async}}async {{/async}}def {{operationId}}{{>partial_api_args}} -> {{{returnType}}}{{^returnType}}None{{/returnType}}:
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
{{#async}}async {{/async}}def {{operationId}}{{>partial_api_args}} -> {{#isResponseBinary}}io.IOBase{{/isResponseBinary}}{{^isResponseBinary}}{{{returnType}}}{{^returnType}}None{{/returnType}}{{/isResponseBinary}}:
{{/useCustomTemplateCode}}
{{/compatibleWithPythonLegacyApi}}
{{>partial_api}}
{{#compatibleWithPythonLegacyApi}}
{{^async}}
return self.api_client._call_with_legacy_options(
_param,
_response_types_map,
async_req,
_preload_content,
_request_timeout,
{{!
python-legacy ordinary wrappers always forced data-only:
https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb49b45c940c2f694/modules/openapi-generator/src/main/resources/python-legacy/api.mustache#L69-L70
}}
True,
)
{{/async}}
{{#async}}
response_data = {{#async}}await {{/async}}self.api_client.call_api(
*_param,
_request_timeout=_request_timeout
)
{{#async}}await {{/async}}response_data.read()
return self.api_client.response_deserialize(
response_data=response_data,
response_types_map=_response_types_map,
).data
{{/async}}
{{/compatibleWithPythonLegacyApi}}
{{^compatibleWithPythonLegacyApi}}
response_data = {{#async}}await {{/async}}self.api_client.call_api(
*_param,
_request_timeout=_request_timeout
)
{{#async}}await {{/async}}response_data.read()
return self.api_client.response_deserialize(
response_data=response_data,
response_types_map=_response_types_map,
).data
{{/compatibleWithPythonLegacyApi}}
{{#compatibleWithPythonLegacyApi}}
{{^async}}
@validate_call
def {{operationId}}_with_http_info{{>partial_api_args_legacy}} -> Tuple[{{{returnType}}}{{^returnType}}None{{/returnType}}, int, Any]:
{{/async}}
{{#async}}
@validate_call
async def {{operationId}}_with_http_info{{>partial_api_args}} -> ApiResponse[{{{returnType}}}{{^returnType}}None{{/returnType}}]:
{{/async}}
{{/compatibleWithPythonLegacyApi}}
{{^compatibleWithPythonLegacyApi}}
@validate_call
{{^useCustomTemplateCode}}
{{#async}}async {{/async}}def {{operationId}}_with_http_info{{>partial_api_args}} -> ApiResponse[{{{returnType}}}{{^returnType}}None{{/returnType}}]:
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
{{#async}}async {{/async}}def {{operationId}}_with_http_info{{>partial_api_args}} -> ApiResponse[{{#isResponseBinary}}io.IOBase{{/isResponseBinary}}{{^isResponseBinary}}{{{returnType}}}{{^returnType}}None{{/returnType}}{{/isResponseBinary}}]:
{{/useCustomTemplateCode}}
{{/compatibleWithPythonLegacyApi}}
{{>partial_api}}
{{#compatibleWithPythonLegacyApi}}
{{^async}}
return self.api_client._call_with_legacy_options(
_param,
_response_types_map,
async_req,
_preload_content,
_request_timeout,
_return_http_data_only,
)
{{/async}}
{{#async}}
response_data = {{#async}}await {{/async}}self.api_client.call_api(
*_param,
_request_timeout=_request_timeout
)
{{#async}}await {{/async}}response_data.read()
return self.api_client.response_deserialize(
response_data=response_data,
response_types_map=_response_types_map,
)
{{/async}}
{{/compatibleWithPythonLegacyApi}}
{{^compatibleWithPythonLegacyApi}}
response_data = {{#async}}await {{/async}}self.api_client.call_api(
*_param,
_request_timeout=_request_timeout
)
{{#async}}await {{/async}}response_data.read()
return self.api_client.response_deserialize(
response_data=response_data,
response_types_map=_response_types_map,
)
{{/compatibleWithPythonLegacyApi}}
{{^compatibleWithPythonLegacyApi}}
@validate_call
{{#async}}async {{/async}}def {{operationId}}_without_preload_content{{>partial_api_args}} -> RESTResponseType:
{{>partial_api}}
response_data = {{#async}}await {{/async}}self.api_client.call_api(
*_param,
_request_timeout=_request_timeout
)
return response_data.response
{{/compatibleWithPythonLegacyApi}}
{{#supportHttpxSync}}
@validate_call
def {{operationId}}_sync{{>partial_api_args}} -> {{{returnType}}}{{^returnType}}None{{/returnType}}:
"""{{#isDeprecated}}(Deprecated) {{/isDeprecated}}{{{summary}}}{{^summary}}{{operationId}}{{/summary}} (synchronous)
Synchronous variant of :meth:`{{operationId}}`. It calls the asynchronous
method and blocks until it completes.
""" # noqa: E501
return run_sync(
self.{{operationId}}(
{{#allParams}}
{{paramName}}={{paramName}},
{{/allParams}}
_request_timeout=_request_timeout,
_request_auth=_request_auth,
_content_type=_content_type,
_headers=_headers,
_host_index=_host_index,
)
)
@validate_call
def {{operationId}}_sync_with_http_info{{>partial_api_args}} -> ApiResponse[{{{returnType}}}{{^returnType}}None{{/returnType}}]:
"""{{#isDeprecated}}(Deprecated) {{/isDeprecated}}{{{summary}}}{{^summary}}{{operationId}}{{/summary}} (synchronous)
Synchronous variant of :meth:`{{operationId}}_with_http_info`. It calls the
asynchronous method and blocks until it completes.
""" # noqa: E501
return run_sync(
self.{{operationId}}_with_http_info(
{{#allParams}}
{{paramName}}={{paramName}},
{{/allParams}}
_request_timeout=_request_timeout,
_request_auth=_request_auth,
_content_type=_content_type,
_headers=_headers,
_host_index=_host_index,
)
)
@validate_call
def {{operationId}}_sync_without_preload_content{{>partial_api_args}} -> RESTResponseType:
"""{{#isDeprecated}}(Deprecated) {{/isDeprecated}}{{{summary}}}{{^summary}}{{operationId}}{{/summary}} (synchronous)
Synchronous variant of :meth:`{{operationId}}_without_preload_content`. It calls
the asynchronous method and blocks until it completes.
""" # noqa: E501
return run_sync(
self.{{operationId}}_without_preload_content(
{{#allParams}}
{{paramName}}={{paramName}},
{{/allParams}}
_request_timeout=_request_timeout,
_request_auth=_request_auth,
_content_type=_content_type,
_headers=_headers,
_host_index=_host_index,
)
)
{{/supportHttpxSync}}
def _{{operationId}}_serialize(
self,
{{#allParams}}
{{paramName}},
{{/allParams}}
_request_auth,
_content_type,
_headers,
_host_index,
) -> RequestSerialized:
{{#servers.0}}
_hosts = [{{#servers}}
'{{{url}}}'{{^-last}},{{/-last}}{{/servers}}
]
_host = _hosts[_host_index]
{{/servers.0}}
{{^servers.0}}
_host = None
{{/servers.0}}
_collection_formats: Dict[str, str] = {
{{#allParams}}
{{#isArray}}
'{{baseName}}': '{{collectionFormat}}',
{{/isArray}}
{{/allParams}}
}
_path_params: Dict[str, str] = {}
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None
{{#useCustomTemplateCode}}
{{#bodyParam}}
has_files = False
body_param = {{#bodyParam}}{{paramName}}{{/bodyParam}}
excluded_json_fields = set([])
for param_name, param_type in body_param.openapi_types().items():
param_value = getattr(body_param, param_name)
if param_value is None:
continue
if "io.IOBase" in param_type:
has_files = True
_content_type = "multipart/form-data"
excluded_json_fields.add(param_name)
if isinstance(param_value, list):
for index, item in enumerate(param_value):
_files[f'{param_name}[{index}]'] = item
else:
_files[param_name] = param_value
if has_files is True:
_form_params = body_param.to_json_form_params(excluded_json_fields)
{{/bodyParam}}
{{/useCustomTemplateCode}}
# process the path parameters
{{#pathParams}}
if {{paramName}} is not None:
_path_params['{{baseName}}'] = {{paramName}}{{#isEnumRef}}.value{{/isEnumRef}}
{{/pathParams}}
# process the query parameters
{{#queryParams}}
if {{paramName}} is not None:
{{#isDateTime}}
if isinstance({{paramName}}, datetime):
_query_params.append(
(
'{{baseName}}',
{{paramName}}.strftime(
self.api_client.configuration.datetime_format
)
)
)
else:
_query_params.append(('{{baseName}}', {{paramName}}))
{{/isDateTime}}
{{#isDate}}
if isinstance({{paramName}}, date):
_query_params.append(
(
'{{baseName}}',
{{paramName}}.strftime(
self.api_client.configuration.date_format
)
)
)
else:
_query_params.append(('{{baseName}}', {{paramName}}))
{{/isDate}}
{{^isDateTime}}{{^isDate}}
{{#isMap}}
{{#isExplode}}
{{#isDeepObject}}
_query_params.append(('{{baseName}}', {{paramName}}))
{{/isDeepObject}}
{{^isDeepObject}}
_query_params.extend(self.api_client.explode_query_object('{{baseName}}', {{paramName}}))
{{/isDeepObject}}
{{/isExplode}}
{{^isExplode}}
_query_params.append(('{{baseName}}', {{paramName}}))
{{/isExplode}}
{{/isMap}}
{{#isModel}}
{{#isExplode}}
{{#isDeepObject}}
_query_params.append(('{{baseName}}', {{paramName}}))
{{/isDeepObject}}
{{^isDeepObject}}
_query_params.extend(self.api_client.explode_query_object('{{baseName}}', {{paramName}}))
{{/isDeepObject}}
{{/isExplode}}
{{^isExplode}}
_query_params.append(('{{baseName}}', {{paramName}}))
{{/isExplode}}
{{/isModel}}
{{^isMap}}
{{^isModel}}
_query_params.append(('{{baseName}}', {{paramName}}{{#isEnumRef}}.value{{/isEnumRef}}))
{{/isModel}}
{{/isMap}}
{{/isDate}}{{/isDateTime}}
{{/queryParams}}
# process the header parameters
{{#headerParams}}
if {{paramName}} is not None:
{{#vendorExtensions.x-python-json-header}}
_header_params['{{baseName}}'] = json.dumps(
self.api_client.sanitize_for_serialization({{paramName}})
)
{{/vendorExtensions.x-python-json-header}}
{{^vendorExtensions.x-python-json-header}}
_header_params['{{baseName}}'] = {{paramName}}
{{/vendorExtensions.x-python-json-header}}
{{/headerParams}}
# process the form parameters
{{#formParams}}
if {{paramName}} is not None:
{{#isFile}}
_files['{{{baseName}}}'] = {{paramName}}
{{/isFile}}
{{^isFile}}
_form_params.append(('{{{baseName}}}', {{paramName}}))
{{/isFile}}
{{/formParams}}
# process the body parameter
{{#bodyParam}}
{{^useCustomTemplateCode}}
if {{paramName}} is not None:
{{/useCustomTemplateCode}}
{{#useCustomTemplateCode}}
if {{paramName}} is not None and has_files is False:
{{/useCustomTemplateCode}}
{{#isBinary}}
# convert to byte array if the input is a file name (str)
if isinstance({{paramName}}, str):
with open({{paramName}}, "rb") as _fp:
_body_params = _fp.read()
elif isinstance({{paramName}}, tuple):
# drop the filename from the tuple
_body_params = {{paramName}}[1]
else:
_body_params = {{paramName}}
{{/isBinary}}
{{^isBinary}}
_body_params = {{paramName}}
{{/isBinary}}
{{/bodyParam}}
{{#constantParams}}
{{#isQueryParam}}
# Set client side default value of Query Param "{{baseName}}".
_query_params.append(('{{baseName}}', {{#_enum}}'{{{.}}}'{{/_enum}}))
{{/isQueryParam}}
{{#isHeaderParam}}
# Set client side default value of Header Param "{{baseName}}".
_header_params['{{baseName}}'] = {{#_enum}}'{{{.}}}'{{/_enum}}
{{/isHeaderParam}}
{{/constantParams}}
{{#hasProduces}}
# set the HTTP header `Accept`
if 'Accept' not in _header_params:
_header_params['Accept'] = self.api_client.select_header_accept(
[{{#produces}}
'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}
]
)
{{/hasProduces}}
{{#hasConsumes}}
# set the HTTP header `Content-Type`
if _content_type:
_header_params['Content-Type'] = _content_type
else:
_default_content_type = (
self.api_client.select_header_content_type(
[{{#consumes}}
'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/consumes}}
]
)
)
if _default_content_type is not None:
_header_params['Content-Type'] = _default_content_type
{{/hasConsumes}}
# authentication setting
_auth_settings: List[str] = [{{#authMethods}}
'{{name}}'{{^-last}}, {{/-last}}{{/authMethods}}
]
return self.api_client.param_serialize(
method='{{httpMethod}}',
resource_path='{{{path}}}',
path_params=_path_params,
query_params=_query_params,
header_params=_header_params,
body=_body_params,
post_params=_form_params,
files=_files,
auth_settings=_auth_settings,
collection_formats=_collection_formats,
_host=_host,
_request_auth=_request_auth
)
{{/operation}}
{{/operations}}