-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathasync_endpoint_module.pyi
84 lines (76 loc) · 2.49 KB
/
async_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
from dataclasses import asdict
from typing import Any, Dict, List, Optional, Union, cast
import httpx
from ..client import AuthenticatedClient, Client
from ..errors import ApiResponseError
{% for relative in collection.relative_imports %}
{{ relative }}
{% endfor %}
{% for endpoint in collection.endpoints %}
async def {{ endpoint.name }}(
*,
{# 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 = f"{client.base_url}{{ endpoint.path }}"
{% 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.name }} is not None:
params["{{ parameter.name }}"] = {{ parameter.transform() }}
{% endif %}
{% endfor %}
{% endif %}
async with httpx.AsyncClient() as _client:
response = await _client.{{ 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 %}