forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint_macros.py.jinja
186 lines (166 loc) · 6.02 KB
/
endpoint_macros.py.jinja
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
{% from "property_templates/helpers.jinja" import guarded_statement %}
{% from "helpers.jinja" import safe_docstring %}
{% macro header_params(endpoint) %}
{% if endpoint.header_parameters or endpoint.bodies | length > 0 %}
headers: Dict[str, Any] = {}
{% if endpoint.header_parameters %}
{% for parameter in endpoint.header_parameters %}
{% import "property_templates/" + parameter.template as param_template %}
{% if param_template.transform_header %}
{% set expression = param_template.transform_header(parameter.python_name) %}
{% else %}
{% set expression = parameter.python_name %}
{% endif %}
{% set statement = 'headers["' + parameter.name + '"]' + " = " + expression %}
{{ guarded_statement(parameter, parameter.python_name, statement) }}
{% endfor %}
{% endif %}
{% endif %}
{% endmacro %}
{% macro cookie_params(endpoint) %}
{% if endpoint.cookie_parameters %}
cookies = {}
{% for parameter in endpoint.cookie_parameters %}
{% if parameter.required %}
cookies["{{ parameter.name}}"] = {{ parameter.python_name }}
{% else %}
if {{ parameter.python_name }} is not UNSET:
cookies["{{ parameter.name}}"] = {{ parameter.python_name }}
{% endif %}
{% endfor %}
{% endif %}
{% endmacro %}
{% macro query_params(endpoint) %}
{% if endpoint.query_parameters %}
params: Dict[str, Any] = {}
{% for property in endpoint.query_parameters %}
{% set destination = property.python_name %}
{% import "property_templates/" + property.template as prop_template %}
{% if prop_template.transform %}
{% set destination = "json_" + property.python_name %}
{{ prop_template.transform(property, property.python_name, destination) }}
{% endif %}
{%- if not property.json_is_dict %}
params["{{ property.name }}"] = {{ destination }}
{% else %}
{{ guarded_statement(property, destination, "params.update(" + destination + ")") }}
{% endif %}
{% endfor %}
params = {k: v for k, v in params.items() if v is not UNSET and v is not None}
{% endif %}
{% endmacro %}
{% macro body_to_kwarg(body, destination) %}
{% if body.body_type == "data" %}
{{ destination }} = body.to_dict()
{% elif body.body_type == "files"%}
{{ multipart_body(body, destination) }}
{% elif body.body_type == "json" %}
{{ json_body(body, destination) }}
{% elif body.body_type == "content" %}
{{ destination }} = body.payload
{% endif %}
{% endmacro %}
{% macro json_body(body, destination) %}
{% set property = body.prop %}
{% import "property_templates/" + property.template as prop_template %}
{% if prop_template.transform %}
{{ prop_template.transform(property, property.python_name, destination) }}
{% else %}
{{ destination }} = {{ property.python_name }}
{% endif %}
{% endmacro %}
{% macro multipart_body(body, destination) %}
{% set property = body.prop %}
{% import "property_templates/" + property.template as prop_template %}
{% if prop_template.transform_multipart_body %}
{{ prop_template.transform_multipart_body(property, property.python_name, destination) }}
{% endif %}
{% endmacro %}
{# The all the kwargs passed into an endpoint (and variants thereof)) #}
{% macro arguments(endpoint, include_client=True) %}
{# path parameters #}
{% for parameter in endpoint.path_parameters %}
{{ parameter.to_string() }},
{% endfor %}
{% if include_client or ((endpoint.list_all_parameters() | length) > (endpoint.path_parameters | length)) %}
*,
{% endif %}
{# Proper client based on whether or not the endpoint requires authentication #}
{% if include_client %}
{% if endpoint.requires_security %}
client: AuthenticatedClient,
{% else %}
client: Union[AuthenticatedClient, Client],
{% endif %}
{% endif %}
{# Any allowed bodies #}
{% if endpoint.bodies | length == 1 %}
body: {{ endpoint.bodies[0].prop.get_type_string() }},
{% elif endpoint.bodies | length > 1 %}
body: Union[
{% for body in endpoint.bodies %}
{{ body.prop.get_type_string() }},
{% endfor %}
],
{% endif %}
{# query parameters #}
{% for parameter in endpoint.query_parameters %}
{{ parameter.to_string() }},
{% endfor %}
{% for parameter in endpoint.header_parameters %}
{{ parameter.to_string() }},
{% endfor %}
{# cookie parameters #}
{% for parameter in endpoint.cookie_parameters %}
{{ parameter.to_string() }},
{% endfor %}
{% endmacro %}
{# Just lists all kwargs to endpoints as name=name for passing to other functions #}
{% macro kwargs(endpoint, include_client=True) %}
{% for parameter in endpoint.path_parameters %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
{% if include_client %}
client=client,
{% endif %}
{% if endpoint.bodies | length > 0 %}
body=body,
{% endif %}
{% for parameter in endpoint.query_parameters %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
{% for parameter in endpoint.header_parameters %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
{% for parameter in endpoint.cookie_parameters %}
{{ parameter.python_name }}={{ parameter.python_name }},
{% endfor %}
{% endmacro %}
{% macro docstring_content(endpoint, return_string, is_detailed) %}
{% if endpoint.summary %}{{ endpoint.summary | wordwrap(100)}}
{% endif -%}
{%- if endpoint.description %} {{ endpoint.description | wordwrap(100) }}
{% endif %}
{% if not endpoint.summary and not endpoint.description %}
{# Leave extra space so that Args or Returns isn't at the top #}
{% endif %}
{% set all_parameters = endpoint.list_all_parameters() %}
{% if all_parameters %}
Args:
{% for parameter in all_parameters %}
{{ parameter.to_docstring() | wordwrap(90) | indent(8) }}
{% endfor %}
{% endif %}
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
{% if is_detailed %}
Response[{{ return_string }}]
{% else %}
{{ return_string }}
{% endif %}
{% endmacro %}
{% macro docstring(endpoint, return_string, is_detailed) %}
{{ safe_docstring(docstring_content(endpoint, return_string, is_detailed)) }}
{% endmacro %}