11import ssl
22from typing import Any , Self
33
4- import httpx
4+ import httpx2
55from attrs import define , evolve , field
66
77
88@define
99class Client :
1010 """A class for keeping track of data related to the API
1111
12- The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
12+ The following are accepted as keyword arguments and will be used to construct httpx2 Clients internally:
1313
1414 ``base_url``: The base URL for the API, all requests are made to a relative path to this URL
1515
@@ -18,14 +18,14 @@ class Client:
1818 ``headers``: A dictionary of headers to be sent with every request
1919
2020 ``timeout``: The maximum amount of a time a request can take. API functions will raise
21- httpx .TimeoutException if this is exceeded.
21+ httpx2 .TimeoutException if this is exceeded.
2222
2323 ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
2424 but can be set to False for testing purposes.
2525
2626 ``follow_redirects``: Whether or not to follow redirects. Default value is False.
2727
28- ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx .Client`` and ``httpx .AsyncClient`` constructor.
28+ ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx2 .Client`` and ``httpx2 .AsyncClient`` constructor.
2929
3030 """
3131
@@ -34,12 +34,12 @@ class Client:
3434 _base_url : str = field (alias = "base_url" )
3535 _cookies : dict [str , str ] = field (factory = dict , kw_only = True , alias = "cookies" )
3636 _headers : dict [str , str ] = field (factory = dict , kw_only = True , alias = "headers" )
37- _timeout : httpx .Timeout | None = field (default = None , kw_only = True , alias = "timeout" )
37+ _timeout : httpx2 .Timeout | None = field (default = None , kw_only = True , alias = "timeout" )
3838 _verify_ssl : str | bool | ssl .SSLContext = field (default = True , kw_only = True , alias = "verify_ssl" )
3939 _follow_redirects : bool = field (default = False , kw_only = True , alias = "follow_redirects" )
4040 _httpx_args : dict [str , Any ] = field (factory = dict , kw_only = True , alias = "httpx_args" )
41- _client : httpx .Client | None = field (default = None , init = False )
42- _async_client : httpx .AsyncClient | None = field (default = None , init = False )
41+ _client : httpx2 .Client | None = field (default = None , init = False )
42+ _async_client : httpx2 .AsyncClient | None = field (default = None , init = False )
4343
4444 def with_headers (self , headers : dict [str , str ]) -> "Client" :
4545 """Get a new client matching this one with additional headers"""
@@ -57,26 +57,26 @@ def with_cookies(self, cookies: dict[str, str]) -> "Client":
5757 self ._async_client .cookies .update (cookies )
5858 return evolve (self , cookies = {** self ._cookies , ** cookies })
5959
60- def with_timeout (self , timeout : httpx .Timeout ) -> "Client" :
60+ def with_timeout (self , timeout : httpx2 .Timeout ) -> "Client" :
6161 """Get a new client matching this one with a new timeout configuration"""
6262 if self ._client is not None :
6363 self ._client .timeout = timeout
6464 if self ._async_client is not None :
6565 self ._async_client .timeout = timeout
6666 return evolve (self , timeout = timeout )
6767
68- def set_httpx_client (self , client : httpx .Client ) -> Self :
69- """Manually set the underlying httpx .Client
68+ def set_httpx_client (self , client : httpx2 .Client ) -> Self :
69+ """Manually set the underlying httpx2 .Client
7070
7171 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
7272 """
7373 self ._client = client
7474 return self
7575
76- def get_httpx_client (self ) -> httpx .Client :
77- """Get the underlying httpx .Client, constructing a new one if not previously set"""
76+ def get_httpx_client (self ) -> httpx2 .Client :
77+ """Get the underlying httpx2 .Client, constructing a new one if not previously set"""
7878 if self ._client is None :
79- self ._client = httpx .Client (
79+ self ._client = httpx2 .Client (
8080 base_url = self ._base_url ,
8181 cookies = self ._cookies ,
8282 headers = self ._headers ,
@@ -88,26 +88,26 @@ def get_httpx_client(self) -> httpx.Client:
8888 return self ._client
8989
9090 def __enter__ (self ) -> Self :
91- """Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
91+ """Enter a context manager for self.client—you cannot enter twice (see httpx2 docs)"""
9292 self .get_httpx_client ().__enter__ ()
9393 return self
9494
9595 def __exit__ (self , * args : Any , ** kwargs : Any ) -> None :
96- """Exit a context manager for internal httpx .Client (see httpx docs)"""
96+ """Exit a context manager for internal httpx2 .Client (see httpx2 docs)"""
9797 self .get_httpx_client ().__exit__ (* args , ** kwargs )
9898
99- def set_async_httpx_client (self , async_client : httpx .AsyncClient ) -> Self :
100- """Manually set the underlying httpx .AsyncClient
99+ def set_async_httpx_client (self , async_client : httpx2 .AsyncClient ) -> Self :
100+ """Manually set the underlying httpx2 .AsyncClient
101101
102102 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
103103 """
104104 self ._async_client = async_client
105105 return self
106106
107- def get_async_httpx_client (self ) -> httpx .AsyncClient :
108- """Get the underlying httpx .AsyncClient, constructing a new one if not previously set"""
107+ def get_async_httpx_client (self ) -> httpx2 .AsyncClient :
108+ """Get the underlying httpx2 .AsyncClient, constructing a new one if not previously set"""
109109 if self ._async_client is None :
110- self ._async_client = httpx .AsyncClient (
110+ self ._async_client = httpx2 .AsyncClient (
111111 base_url = self ._base_url ,
112112 cookies = self ._cookies ,
113113 headers = self ._headers ,
@@ -119,20 +119,20 @@ def get_async_httpx_client(self) -> httpx.AsyncClient:
119119 return self ._async_client
120120
121121 async def __aenter__ (self ) -> Self :
122- """Enter a context manager for underlying httpx .AsyncClient—you cannot enter twice (see httpx docs)"""
122+ """Enter a context manager for underlying httpx2 .AsyncClient—you cannot enter twice (see httpx2 docs)"""
123123 await self .get_async_httpx_client ().__aenter__ ()
124124 return self
125125
126126 async def __aexit__ (self , * args : Any , ** kwargs : Any ) -> None :
127- """Exit a context manager for underlying httpx .AsyncClient (see httpx docs)"""
127+ """Exit a context manager for underlying httpx2 .AsyncClient (see httpx2 docs)"""
128128 await self .get_async_httpx_client ().__aexit__ (* args , ** kwargs )
129129
130130
131131@define
132132class AuthenticatedClient :
133133 """A Client which has been authenticated for use on secured endpoints
134134
135- The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
135+ The following are accepted as keyword arguments and will be used to construct httpx2 Clients internally:
136136
137137 ``base_url``: The base URL for the API, all requests are made to a relative path to this URL
138138
@@ -141,14 +141,14 @@ class AuthenticatedClient:
141141 ``headers``: A dictionary of headers to be sent with every request
142142
143143 ``timeout``: The maximum amount of a time a request can take. API functions will raise
144- httpx .TimeoutException if this is exceeded.
144+ httpx2 .TimeoutException if this is exceeded.
145145
146146 ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
147147 but can be set to False for testing purposes.
148148
149149 ``follow_redirects``: Whether or not to follow redirects. Default value is False.
150150
151- ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx .Client`` and ``httpx .AsyncClient`` constructor.
151+ ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx2 .Client`` and ``httpx2 .AsyncClient`` constructor.
152152
153153 """
154154
@@ -157,12 +157,12 @@ class AuthenticatedClient:
157157 _base_url : str = field (alias = "base_url" )
158158 _cookies : dict [str , str ] = field (factory = dict , kw_only = True , alias = "cookies" )
159159 _headers : dict [str , str ] = field (factory = dict , kw_only = True , alias = "headers" )
160- _timeout : httpx .Timeout | None = field (default = None , kw_only = True , alias = "timeout" )
160+ _timeout : httpx2 .Timeout | None = field (default = None , kw_only = True , alias = "timeout" )
161161 _verify_ssl : str | bool | ssl .SSLContext = field (default = True , kw_only = True , alias = "verify_ssl" )
162162 _follow_redirects : bool = field (default = False , kw_only = True , alias = "follow_redirects" )
163163 _httpx_args : dict [str , Any ] = field (factory = dict , kw_only = True , alias = "httpx_args" )
164- _client : httpx .Client | None = field (default = None , init = False )
165- _async_client : httpx .AsyncClient | None = field (default = None , init = False )
164+ _client : httpx2 .Client | None = field (default = None , init = False )
165+ _async_client : httpx2 .AsyncClient | None = field (default = None , init = False )
166166
167167 token : str
168168 """The token to use for authentication"""
@@ -187,27 +187,27 @@ def with_cookies(self, cookies: dict[str, str]) -> "AuthenticatedClient":
187187 self ._async_client .cookies .update (cookies )
188188 return evolve (self , cookies = {** self ._cookies , ** cookies })
189189
190- def with_timeout (self , timeout : httpx .Timeout ) -> "AuthenticatedClient" :
190+ def with_timeout (self , timeout : httpx2 .Timeout ) -> "AuthenticatedClient" :
191191 """Get a new client matching this one with a new timeout configuration"""
192192 if self ._client is not None :
193193 self ._client .timeout = timeout
194194 if self ._async_client is not None :
195195 self ._async_client .timeout = timeout
196196 return evolve (self , timeout = timeout )
197197
198- def set_httpx_client (self , client : httpx .Client ) -> Self :
199- """Manually set the underlying httpx .Client
198+ def set_httpx_client (self , client : httpx2 .Client ) -> Self :
199+ """Manually set the underlying httpx2 .Client
200200
201201 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
202202 """
203203 self ._client = client
204204 return self
205205
206- def get_httpx_client (self ) -> httpx .Client :
207- """Get the underlying httpx .Client, constructing a new one if not previously set"""
206+ def get_httpx_client (self ) -> httpx2 .Client :
207+ """Get the underlying httpx2 .Client, constructing a new one if not previously set"""
208208 if self ._client is None :
209209 self ._headers [self .auth_header_name ] = f"{ self .prefix } { self .token } " if self .prefix else self .token
210- self ._client = httpx .Client (
210+ self ._client = httpx2 .Client (
211211 base_url = self ._base_url ,
212212 cookies = self ._cookies ,
213213 headers = self ._headers ,
@@ -219,27 +219,27 @@ def get_httpx_client(self) -> httpx.Client:
219219 return self ._client
220220
221221 def __enter__ (self ) -> Self :
222- """Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
222+ """Enter a context manager for self.client—you cannot enter twice (see httpx2 docs)"""
223223 self .get_httpx_client ().__enter__ ()
224224 return self
225225
226226 def __exit__ (self , * args : Any , ** kwargs : Any ) -> None :
227- """Exit a context manager for internal httpx .Client (see httpx docs)"""
227+ """Exit a context manager for internal httpx2 .Client (see httpx2 docs)"""
228228 self .get_httpx_client ().__exit__ (* args , ** kwargs )
229229
230- def set_async_httpx_client (self , async_client : httpx .AsyncClient ) -> Self :
231- """Manually set the underlying httpx .AsyncClient
230+ def set_async_httpx_client (self , async_client : httpx2 .AsyncClient ) -> Self :
231+ """Manually set the underlying httpx2 .AsyncClient
232232
233233 **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
234234 """
235235 self ._async_client = async_client
236236 return self
237237
238- def get_async_httpx_client (self ) -> httpx .AsyncClient :
239- """Get the underlying httpx .AsyncClient, constructing a new one if not previously set"""
238+ def get_async_httpx_client (self ) -> httpx2 .AsyncClient :
239+ """Get the underlying httpx2 .AsyncClient, constructing a new one if not previously set"""
240240 if self ._async_client is None :
241241 self ._headers [self .auth_header_name ] = f"{ self .prefix } { self .token } " if self .prefix else self .token
242- self ._async_client = httpx .AsyncClient (
242+ self ._async_client = httpx2 .AsyncClient (
243243 base_url = self ._base_url ,
244244 cookies = self ._cookies ,
245245 headers = self ._headers ,
@@ -251,10 +251,10 @@ def get_async_httpx_client(self) -> httpx.AsyncClient:
251251 return self ._async_client
252252
253253 async def __aenter__ (self ) -> Self :
254- """Enter a context manager for underlying httpx .AsyncClient—you cannot enter twice (see httpx docs)"""
254+ """Enter a context manager for underlying httpx2 .AsyncClient—you cannot enter twice (see httpx2 docs)"""
255255 await self .get_async_httpx_client ().__aenter__ ()
256256 return self
257257
258258 async def __aexit__ (self , * args : Any , ** kwargs : Any ) -> None :
259- """Exit a context manager for underlying httpx .AsyncClient (see httpx docs)"""
259+ """Exit a context manager for underlying httpx2 .AsyncClient (see httpx2 docs)"""
260260 await self .get_async_httpx_client ().__aexit__ (* args , ** kwargs )
0 commit comments