forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_module.pyi
88 lines (80 loc) · 2.55 KB
/
endpoint_module.pyi
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
from dataclasses import asdict
from typing import Dict, List, Optional, Union
import httpx
from ..client import AuthenticatedClient, Client
from ..errors import ApiResponseError
{% for relative in collection.relative_imports %}
{{ relative }}
{% endfor %}
{% for endpoint in collection.endpoints %}
def {{ endpoint.name | snakecase }}(
*,
{# Proper client based on whether or not the endpoint requires authentication #}
{% if endpoint.requires_security %}
client: AuthenticatedClient,
{% else %}
client: Client,
{% endif %}
{# path parameters #}
{% for parameter in endpoint.path_parameters %}
{{ parameter.to_string() }},
{% endfor %}
{# Form data if any #}
{% if endpoint.form_body_reference %}
form_data: {{ endpoint.form_body_reference.class_name }},
{% endif %}
{# JSON body if any #}
{% if endpoint.json_body %}
json_body: {{ endpoint.json_body.get_type_string() }},
{% endif %}
{# query parameters #}
{% for parameter in endpoint.query_parameters %}
{{ parameter.to_string() }},
{% endfor %}
) -> Union[
{% for response in endpoint.responses %}
{{ response.return_string() }},
{% endfor %}
]:
""" {{ endpoint.description }} """
url = "{}{{ endpoint.path }}".format(
client.base_url
{%- for parameter in endpoint.path_parameters -%}
,{{parameter.name}}={{parameter.python_name}}
{%- endfor -%}
)
{% if endpoint.query_parameters %}
params = {
{% for parameter in endpoint.query_parameters %}
{% if parameter.required %}
"{{ parameter.name }}": {{ parameter.transform() }},
{% endif %}
{% endfor %}
}
{% for parameter in endpoint.query_parameters %}
{% if not parameter.required %}
if {{ parameter.python_name }} is not None:
params["{{ parameter.name }}"] = str({{ parameter.transform() }})
{% endif %}
{% endfor %}
{% endif %}
response = httpx.{{ endpoint.method }}(
url=url,
headers=client.get_headers(),
{% if endpoint.form_body_reference %}
data=asdict(form_data),
{% endif %}
{% if endpoint.json_body %}
json={{ endpoint.json_body.transform() }},
{% endif %}
{% if endpoint.query_parameters %}
params=params,
{% endif %}
)
{% for response in endpoint.responses %}
if response.status_code == {{ response.status_code }}:
return {{ response.constructor() }}
{% endfor %}
else:
raise ApiResponseError(response=response)
{% endfor %}