Coverage for src/hush_hush/_generated/client.py: 54%
117 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-10-16 16:01 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-10-16 16:01 +0000
1# Code generated by openapi-python-client from hush-hush/api/openapi.yaml; DO NOT EDIT.
3import ssl
4from typing import Any, Self
6import httpx
7from attrs import define, evolve, field
10@define
11class Client:
12 """A class for keeping track of data related to the API
14 The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
16 ``base_url``: The base URL for the API, all requests are made to a relative path to this URL
18 ``cookies``: A dictionary of cookies to be sent with every request
20 ``headers``: A dictionary of headers to be sent with every request
22 ``timeout``: The maximum amount of a time a request can take. API functions will raise
23 httpx.TimeoutException if this is exceeded.
25 ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
26 but can be set to False for testing purposes.
28 ``follow_redirects``: Whether or not to follow redirects. Default value is False.
30 ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
33 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.
37 """
39 raise_on_unexpected_status: bool = field(default=False, kw_only=True)
40 _base_url: str = field(alias="base_url")
41 _cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
42 _headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
43 _timeout: httpx.Timeout | None = field(default=None, kw_only=True, alias="timeout")
44 _verify_ssl: str | bool | ssl.SSLContext = field(default=True, kw_only=True, alias="verify_ssl")
45 _follow_redirects: bool = field(default=False, kw_only=True, alias="follow_redirects")
46 _httpx_args: dict[str, Any] = field(factory=dict, kw_only=True, alias="httpx_args")
47 _client: httpx.Client | None = field(default=None, init=False)
48 _async_client: httpx.AsyncClient | None = field(default=None, init=False)
50 def with_headers(self, headers: dict[str, str]) -> "Client":
51 """Get a new client matching this one with additional headers"""
52 if self._client is not None:
53 self._client.headers.update(headers)
54 if self._async_client is not None:
55 self._async_client.headers.update(headers)
56 return evolve(self, headers={**self._headers, **headers})
58 def with_cookies(self, cookies: dict[str, str]) -> "Client":
59 """Get a new client matching this one with additional cookies"""
60 if self._client is not None:
61 self._client.cookies.update(cookies)
62 if self._async_client is not None:
63 self._async_client.cookies.update(cookies)
64 return evolve(self, cookies={**self._cookies, **cookies})
66 def with_timeout(self, timeout: httpx.Timeout) -> "Client":
67 """Get a new client matching this one with a new timeout configuration"""
68 if self._client is not None:
69 self._client.timeout = timeout
70 if self._async_client is not None:
71 self._async_client.timeout = timeout
72 return evolve(self, timeout=timeout)
74 def set_httpx_client(self, client: httpx.Client) -> Self:
75 """Manually set the underlying httpx.Client
77 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
78 """
79 self._client = client
80 return self
82 def get_httpx_client(self) -> httpx.Client:
83 """Get the underlying httpx.Client, constructing a new one if not previously set"""
84 if self._client is None:
85 self._client = httpx.Client(
86 base_url=self._base_url,
87 cookies=self._cookies,
88 headers=self._headers,
89 timeout=self._timeout,
90 verify=self._verify_ssl,
91 follow_redirects=self._follow_redirects,
92 **self._httpx_args,
93 )
94 return self._client
96 def __enter__(self) -> Self:
97 """Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
98 self.get_httpx_client().__enter__()
99 return self
101 def __exit__(self, *args: Any, **kwargs: Any) -> None:
102 """Exit a context manager for internal httpx.Client (see httpx docs)"""
103 self.get_httpx_client().__exit__(*args, **kwargs)
105 def set_async_httpx_client(self, async_client: httpx.AsyncClient) -> Self:
106 """Manually set the underlying httpx.AsyncClient
108 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
109 """
110 self._async_client = async_client
111 return self
113 def get_async_httpx_client(self) -> httpx.AsyncClient:
114 """Get the underlying httpx.AsyncClient, constructing a new one if not previously set"""
115 if self._async_client is None:
116 self._async_client = httpx.AsyncClient(
117 base_url=self._base_url,
118 cookies=self._cookies,
119 headers=self._headers,
120 timeout=self._timeout,
121 verify=self._verify_ssl,
122 follow_redirects=self._follow_redirects,
123 **self._httpx_args,
124 )
125 return self._async_client
127 async def __aenter__(self) -> Self:
128 """Enter a context manager for underlying httpx.AsyncClient—you cannot enter twice (see httpx docs)"""
129 await self.get_async_httpx_client().__aenter__()
130 return self
132 async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
133 """Exit a context manager for underlying httpx.AsyncClient (see httpx docs)"""
134 await self.get_async_httpx_client().__aexit__(*args, **kwargs)
137@define
138class AuthenticatedClient:
139 """A Client which has been authenticated for use on secured endpoints
141 The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
143 ``base_url``: The base URL for the API, all requests are made to a relative path to this URL
145 ``cookies``: A dictionary of cookies to be sent with every request
147 ``headers``: A dictionary of headers to be sent with every request
149 ``timeout``: The maximum amount of a time a request can take. API functions will raise
150 httpx.TimeoutException if this is exceeded.
152 ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
153 but can be set to False for testing purposes.
155 ``follow_redirects``: Whether or not to follow redirects. Default value is False.
157 ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
160 Attributes:
161 raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
162 status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
163 argument to the constructor.
164 token: The token to use for authentication
165 prefix: The prefix to use for the Authorization header
166 auth_header_name: The name of the Authorization header
167 """
169 raise_on_unexpected_status: bool = field(default=False, kw_only=True)
170 _base_url: str = field(alias="base_url")
171 _cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
172 _headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
173 _timeout: httpx.Timeout | None = field(default=None, kw_only=True, alias="timeout")
174 _verify_ssl: str | bool | ssl.SSLContext = field(default=True, kw_only=True, alias="verify_ssl")
175 _follow_redirects: bool = field(default=False, kw_only=True, alias="follow_redirects")
176 _httpx_args: dict[str, Any] = field(factory=dict, kw_only=True, alias="httpx_args")
177 _client: httpx.Client | None = field(default=None, init=False)
178 _async_client: httpx.AsyncClient | None = field(default=None, init=False)
180 token: str
181 prefix: str = "Bearer"
182 auth_header_name: str = "Authorization"
184 def with_headers(self, headers: dict[str, str]) -> "AuthenticatedClient":
185 """Get a new client matching this one with additional headers"""
186 if self._client is not None:
187 self._client.headers.update(headers)
188 if self._async_client is not None:
189 self._async_client.headers.update(headers)
190 return evolve(self, headers={**self._headers, **headers})
192 def with_cookies(self, cookies: dict[str, str]) -> "AuthenticatedClient":
193 """Get a new client matching this one with additional cookies"""
194 if self._client is not None:
195 self._client.cookies.update(cookies)
196 if self._async_client is not None:
197 self._async_client.cookies.update(cookies)
198 return evolve(self, cookies={**self._cookies, **cookies})
200 def with_timeout(self, timeout: httpx.Timeout) -> "AuthenticatedClient":
201 """Get a new client matching this one with a new timeout configuration"""
202 if self._client is not None:
203 self._client.timeout = timeout
204 if self._async_client is not None:
205 self._async_client.timeout = timeout
206 return evolve(self, timeout=timeout)
208 def set_httpx_client(self, client: httpx.Client) -> Self:
209 """Manually set the underlying httpx.Client
211 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
212 """
213 self._client = client
214 return self
216 def get_httpx_client(self) -> httpx.Client:
217 """Get the underlying httpx.Client, constructing a new one if not previously set"""
218 if self._client is None:
219 self._headers[self.auth_header_name] = (
220 f"{self.prefix} {self.token}" if self.prefix else self.token
221 )
222 self._client = httpx.Client(
223 base_url=self._base_url,
224 cookies=self._cookies,
225 headers=self._headers,
226 timeout=self._timeout,
227 verify=self._verify_ssl,
228 follow_redirects=self._follow_redirects,
229 **self._httpx_args,
230 )
231 return self._client
233 def __enter__(self) -> Self:
234 """Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
235 self.get_httpx_client().__enter__()
236 return self
238 def __exit__(self, *args: Any, **kwargs: Any) -> None:
239 """Exit a context manager for internal httpx.Client (see httpx docs)"""
240 self.get_httpx_client().__exit__(*args, **kwargs)
242 def set_async_httpx_client(self, async_client: httpx.AsyncClient) -> Self:
243 """Manually set the underlying httpx.AsyncClient
245 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
246 """
247 self._async_client = async_client
248 return self
250 def get_async_httpx_client(self) -> httpx.AsyncClient:
251 """Get the underlying httpx.AsyncClient, constructing a new one if not previously set"""
252 if self._async_client is None:
253 self._headers[self.auth_header_name] = (
254 f"{self.prefix} {self.token}" if self.prefix else self.token
255 )
256 self._async_client = httpx.AsyncClient(
257 base_url=self._base_url,
258 cookies=self._cookies,
259 headers=self._headers,
260 timeout=self._timeout,
261 verify=self._verify_ssl,
262 follow_redirects=self._follow_redirects,
263 **self._httpx_args,
264 )
265 return self._async_client
267 async def __aenter__(self) -> Self:
268 """Enter a context manager for underlying httpx.AsyncClient—you cannot enter twice (see httpx docs)"""
269 await self.get_async_httpx_client().__aenter__()
270 return self
272 async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
273 """Exit a context manager for underlying httpx.AsyncClient (see httpx docs)"""
274 await self.get_async_httpx_client().__aexit__(*args, **kwargs)