@@ -5,6 +5,31 @@ from attrs import define, field, evolve
55import httpx
66
77
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+
833@define
934class Client:
1035 """A class for keeping track of data related to the API
@@ -29,14 +54,14 @@ class Client:
2954 ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
3055{% endmacro %}
3156{{ httpx_args_docstring() }}
57+ {% if not config .docstrings_on_attributes %}
3258
3359 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(101) | indent(12) }}
61+ {% endif %}
3762 """
3863{% macro attributes () %}
39- raise_on_unexpected_status: bool = field(default=False, kw_only=True)
64+ {{ declare_attr(" raise_on_unexpected_status") | indent(4) }}
4065 _base_url: str = field(alias="base_url")
4166 _cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
4267 _headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
@@ -147,20 +172,20 @@ class AuthenticatedClient:
147172 """A Client which has been authenticated for use on secured endpoints
148173
149174{{ httpx_args_docstring() }}
175+ {% if not config .docstrings_on_attributes %}
150176
151177 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(101) | 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 %}
158183 """
159184
160185{{ 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) }}
164189
165190{{ builders("AuthenticatedClient") }}
166191{{ httpx_stuff("AuthenticatedClient", "self._headers[self.auth_header_name] = f\"{self.prefix} {self.token}\" if self.prefix else self.token") }}
0 commit comments