@@ -5,6 +5,31 @@ from attrs import define, field, evolve
5
5
import httpx
6
6
7
7
8
+ {% set attrs_info = {
9
+ "raise_on_unexpected_status" : namespace (
10
+ type ="bool" ,
11
+ default ="field(default=False, kw_only=True)" ,
12
+ docstring ="Whether or not to raise an errors.UnexpectedStatus if the API returns a status code"
13
+ " that was not documented in the source OpenAPI document. Can also be provided as a keyword"
14
+ " argument to the constructor."
15
+ ),
16
+ "token" : namespace (type ="str" , default ="" , docstring ="The token to use for authentication" ),
17
+ "prefix" : namespace (type ="str" , default ='"Bearer"' , docstring ="The prefix to use for the Authorization header" ),
18
+ "auth_header_name" : namespace (type ="str" , default ='"Authorization"' , docstring ="The name of the Authorization header" ),
19
+ } %}
20
+
21
+ {% macro attr_in_class_docstring (name ) %}
22
+ {{ name }}: {{ attrs_info[name] .docstring }}
23
+ {% - endmacro %}
24
+
25
+ {% macro declare_attr (name ) %}
26
+ {% set attr = attrs_info [name ] %}
27
+ {{ name }}: {{ attr.type }}{% if attr .default %} = {{ attr.default }}{% endif %}
28
+ {% if attr .docstring and config .docstrings_on_attributes +%}
29
+ """{{ attr.docstring }}"""
30
+ {% - endif %}
31
+ {% endmacro %}
32
+
8
33
@define
9
34
class Client:
10
35
"""A class for keeping track of data related to the API
@@ -29,14 +54,14 @@ class Client:
29
54
``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
30
55
{% endmacro %}
31
56
{{ httpx_args_docstring() }}
57
+ {% if not config .docstrings_on_attributes %}
32
58
33
59
Attributes:
34
- raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
35
- status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
36
- argument to the constructor.
60
+ {{ attr_in_class_docstring("raise_on_unexpected_status") | wordwrap(100) | indent(12) }}
61
+ {% endif %}
37
62
"""
38
63
{% macro attributes () %}
39
- raise_on_unexpected_status: bool = field(default=False, kw_only=True)
64
+ {{ declare_attr(" raise_on_unexpected_status") | indent(4) }}
40
65
_base_url: str = field(alias="base_url")
41
66
_cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
42
67
_headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
@@ -147,20 +172,20 @@ class AuthenticatedClient:
147
172
"""A Client which has been authenticated for use on secured endpoints
148
173
149
174
{{ httpx_args_docstring() }}
175
+ {% if not config .docstrings_on_attributes %}
150
176
151
177
Attributes:
152
- raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
153
- status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
154
- argument to the constructor.
155
- token: The token to use for authentication
156
- prefix: The prefix to use for the Authorization header
157
- auth_header_name: The name of the Authorization header
178
+ {{ attr_in_class_docstring("raise_on_unexpected_status") | wordwrap(100) | indent(12) }}
179
+ {{ attr_in_class_docstring("token") | indent(8) }}
180
+ {{ attr_in_class_docstring("prefix") | indent(8) }}
181
+ {{ attr_in_class_docstring("auth_header_name") | indent(8) }}
182
+ {% endif %}
158
183
"""
159
184
160
185
{{ attributes() }}
161
- token: str
162
- prefix: str = "Bearer"
163
- auth_header_name: str = "Authorization"
186
+ {{ declare_attr(" token") | indent(4) }}
187
+ {{ declare_attr(" prefix") | indent(4) }}
188
+ {{ declare_attr(" auth_header_name") | indent(4) }}
164
189
165
190
{{ builders("AuthenticatedClient") }}
166
191
{{ httpx_stuff("AuthenticatedClient", "self._headers[self.auth_header_name] = f\"{self.prefix} {self.token}\" if self.prefix else self.token") }}
0 commit comments