Skip to content

Commit 3dd36d7

Browse files
s-sajid-alijohnthagendbanty
authored
Port from httpx to httpx2 (#1461)
This PR assumes that the deprecation of generating clients with `httpx` is imminent and updates all golden records to use `httpx2` instead. This leaves the client generation process with legacy `httpx` untested. This will cause issues when `httpx2` introduces breaking changes and it is assumed that the option to use `httpx` will be removed by that time. Additionally, this leaves the generated method`get_httpx_client` as is, preferring to update it to `get_httpx2_client` when support for `httpx` is removed. --------- Co-authored-by: John Hagen <johnthagen@users.noreply.github.com> Co-authored-by: Dylan Anthony <43723790+dbanty@users.noreply.github.com> Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>
1 parent 99208ca commit 3dd36d7

110 files changed

Lines changed: 956 additions & 957 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/default_to_httpx2.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
default: major
3+
---
4+
5+
# Switch from `httpx` to `httpx2` in generated clients
6+
7+
[`httpx2`](https://pydantic.dev/docs/httpx2/get-started/) is largely compatible with `httpx` and should only be a breaking change for users who customize the internal `Client`.
8+
9+
Thanks to @s-sajid-ali and @johnthagen for making this one happen in PR #1461 and discussion #1455!

‎end_to_end_tests/functional_tests/generated_code_execution/test_path_parameters.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest.mock import MagicMock
22

3-
import httpx
3+
import httpx2
44

55
from end_to_end_tests.functional_tests.helpers import (
66
with_generated_client_fixture,
@@ -44,8 +44,8 @@ class TestPathParameterEncoding:
4444

4545
def test_path_params_with_normal_chars_work(self, sync_detailed, Client):
4646
"""Test that normal alphanumeric path parameters still work correctly"""
47-
mock_httpx_client = MagicMock(spec=httpx.Client)
48-
mock_response = MagicMock(spec=httpx.Response)
47+
mock_httpx_client = MagicMock(spec=httpx2.Client)
48+
mock_response = MagicMock(spec=httpx2.Response)
4949
mock_response.status_code = 200
5050
mock_response.json.return_value = {"id": "test"}
5151
mock_response.content = b'{"id": "test"}'
@@ -70,16 +70,16 @@ def test_path_params_with_normal_chars_work(self, sync_detailed, Client):
7070

7171
def test_path_params_with_reserved_chars_are_encoded(self, sync_detailed, Client):
7272
"""Test that path parameters with reserved characters are properly URL-encoded"""
73-
# Create a mock httpx client
74-
mock_httpx_client = MagicMock(spec=httpx.Client)
75-
mock_response = MagicMock(spec=httpx.Response)
73+
# Create a mock httpx2 client
74+
mock_httpx_client = MagicMock(spec=httpx2.Client)
75+
mock_response = MagicMock(spec=httpx2.Response)
7676
mock_response.status_code = 200
7777
mock_response.json.return_value = {"id": "test"}
7878
mock_response.content = b'{"id": "test"}'
7979
mock_response.headers = {}
8080
mock_httpx_client.request.return_value = mock_response
8181

82-
# Create a client with the mock httpx client
82+
# Create a client with the mock httpx2 client
8383
client = Client(base_url="https://api.example.com")
8484
client.set_httpx_client(mock_httpx_client)
8585

@@ -100,8 +100,8 @@ def test_path_params_with_reserved_chars_are_encoded(self, sync_detailed, Client
100100

101101
def test_path_params_with_spaces_are_encoded(self, sync_detailed, Client):
102102
"""Test that path parameters with spaces are properly URL-encoded"""
103-
mock_httpx_client = MagicMock(spec=httpx.Client)
104-
mock_response = MagicMock(spec=httpx.Response)
103+
mock_httpx_client = MagicMock(spec=httpx2.Client)
104+
mock_response = MagicMock(spec=httpx2.Response)
105105
mock_response.status_code = 200
106106
mock_response.json.return_value = {"id": "test"}
107107
mock_response.content = b'{"id": "test"}'
@@ -126,8 +126,8 @@ def test_path_params_with_spaces_are_encoded(self, sync_detailed, Client):
126126

127127
def test_path_params_with_hash_are_encoded(self, sync_detailed, Client):
128128
"""Test that path parameters with hash/fragment characters are properly URL-encoded"""
129-
mock_httpx_client = MagicMock(spec=httpx.Client)
130-
mock_response = MagicMock(spec=httpx.Response)
129+
mock_httpx_client = MagicMock(spec=httpx2.Client)
130+
mock_response = MagicMock(spec=httpx2.Response)
131131
mock_response.status_code = 200
132132
mock_response.json.return_value = {"id": "test"}
133133
mock_response.content = b'{"id": "test"}'

‎end_to_end_tests/golden-records/docstrings-on-attributes-golden-record/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Things to know:
7676

7777
## Advanced customizations
7878

79-
There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying `httpx.Client` or `httpx.AsyncClient` (depending on your use-case):
79+
There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying `httpx2.Client` or `httpx2.AsyncClient` (depending on your use-case):
8080

8181
```python
8282
from my_test_api_client import Client
@@ -93,20 +93,20 @@ client = Client(
9393
httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
9494
)
9595

96-
# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()
96+
# Or get the underlying httpx2 client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()
9797
```
9898

99-
You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):
99+
You can even set the httpx2 client directly, but beware that this will override any existing settings (e.g., base_url):
100100

101101
```python
102-
import httpx
102+
import httpx2
103103
from my_test_api_client import Client
104104

105105
client = Client(
106106
base_url="https://api.example.com",
107107
)
108108
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
109-
client.set_httpx_client(httpx.Client(base_url="https://api.example.com", proxies="http://localhost:8030"))
109+
client.set_httpx_client(httpx2.Client(base_url="https://api.example.com", proxy="http://localhost:8030"))
110110
```
111111

112112
## Building / publishing this package

‎end_to_end_tests/golden-records/docstrings-on-attributes-golden-record/my_test_api_client/client.py‎

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import ssl
22
from typing import Any, Self
33

4-
import httpx
4+
import httpx2
55
from attrs import define, evolve, field
66

77

88
@define
99
class 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
132132
class 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)

‎end_to_end_tests/golden-records/docstrings-on-attributes-golden-record/my_test_api_client/types.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __bool__(self) -> Literal[False]:
1414

1515
UNSET: Unset = Unset()
1616

17-
# The types that `httpx.Client(files=)` can accept, copied from that library.
17+
# The types that `httpx2.Client(files=)` can accept, copied from that library.
1818
FileContent = IO[bytes] | bytes | str
1919
FileTypes = (
2020
# (filename, file (or bytes), content_type)
@@ -34,7 +34,7 @@ class File:
3434
mime_type: str | None = None
3535

3636
def to_tuple(self) -> FileTypes:
37-
"""Return a tuple representation that httpx will accept for multipart/form-data"""
37+
"""Return a tuple representation that httpx2 will accept for multipart/form-data"""
3838
return self.file_name, self.payload, self.mime_type
3939

4040

‎end_to_end_tests/golden-records/docstrings-on-attributes-golden-record/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include = ["my_test_api_client/py.typed"]
1111

1212
[tool.poetry.dependencies]
1313
python = "^3.11"
14-
httpx = ">=0.23.1,<0.29.0"
14+
httpx2 = ">=2.13.1,<3"
1515
attrs = ">=22.2.0"
1616

1717
[build-system]

0 commit comments

Comments
 (0)