Skip to content

Commit d28bc12

Browse files
dbantyemann
andauthored
Redesign client for more flexibility via direct httpx access (#775)
* Prototype tighter httpx integration * Blacken * New client template * Update templates & tests * Update generated readmes * Rename `*_client` functions to `*_httpx_client` * Allow AuthenticatedClient anywhere a Client is allowed * Use macros to keep Client/AuthenticatedClient the same * Add changeset notes * Add integration test for minimal httpx version * Add mypy to integration tests * Install lower httpx in the right place for integration tests * Update every attrs to use new syntax, raise minimum httpx version * More release dry runs and prerelease action * Put back missing tabs * Put back missing tabs * Update end_to_end_tests/golden-record/my_test_api_client/client.py Co-authored-by: Ethan Mann <[email protected]> * Regen --------- Co-authored-by: Dylan Anthony <[email protected]> Co-authored-by: Ethan Mann <[email protected]>
1 parent 7b38b52 commit d28bc12

File tree

136 files changed

+2526
-1881
lines changed

Some content is hidden

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

136 files changed

+2526
-1881
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
default: minor
3+
---
4+
5+
#### Allow customizing the underlying `httpx` clients
6+
7+
There are many use-cases where customizing the underlying `httpx` client directly is necessary. Some examples are:
8+
9+
- [Event hooks](https://www.python-httpx.org/advanced/#event-hooks)
10+
- [Proxies](https://www.python-httpx.org/advanced/#http-proxying)
11+
- [Custom authentication](https://www.python-httpx.org/advanced/#customizing-authentication)
12+
- [Retries](https://www.python-httpx.org/advanced/#usage_1)
13+
14+
The new `Client` and `AuthenticatedClient` classes come with several methods to customize underlying clients. You can pass arbitrary arguments to `httpx.Client` or `httpx.AsyncClient` when they are constructed:
15+
16+
```python
17+
client = Client(base_url="https://api.example.com", httpx_args={"proxies": {"https://": "https://proxy.example.com"}})
18+
```
19+
20+
**The underlying clients are constructed lazily, only when needed. `httpx_args` are stored internally in a dictionary until the first request is made.**
21+
22+
You can force immediate construction of an underlying client in order to edit it directly:
23+
24+
```python
25+
import httpx
26+
from my_api import Client
27+
28+
client = Client(base_url="https://api.example.com")
29+
sync_client: httpx.Client = client.get_httpx_client()
30+
sync_client.timeout = 10
31+
async_client = client.get_async_httpx_client()
32+
async_client.timeout = 15
33+
```
34+
35+
You can also completely override the underlying clients:
36+
37+
```python
38+
import httpx
39+
from my_api import Client
40+
41+
client = Client(base_url="https://api.example.com")
42+
# The params you put in here ^ are discarded when you call set_httpx_client or set_async_httpx_client
43+
sync_client = httpx.Client(base_url="https://api.example.com", timeout=10)
44+
client.set_httpx_client(sync_client)
45+
async_client = httpx.AsyncClient(base_url="https://api.example.com", timeout=15)
46+
client.set_async_httpx_client(async_client)
47+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
default: major
3+
---
4+
5+
#### `AuthenticatedClient` no longer inherits from `Client`
6+
7+
The API of `AuthenticatedClient` is still a superset of `Client`, but the two classes no longer share a common base class.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
default: major
3+
---
4+
5+
#### Generated clients and models now use the newer attrs `@define` and `field` APIs
6+
7+
See [the attrs docs](https://www.attrs.org/en/stable/names.html#attrs-tng) for more information on how these may affect you.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
default: minor
3+
---
4+
5+
#### Clients now reuse connections between requests
6+
7+
This happens every time you use the same `Client` or `AuthenticatedClient` instance for multiple requests, however it is best to use a context manager (e.g., `with client as client:`) to ensure the client is closed properly.

.changeset/connections_dont_close.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
default: major
3+
---
4+
5+
#### Connections from clients no longer automatically close (PR [#775](https://github.com/openapi-generators/openapi-python-client/pull/775))
6+
7+
`Client` and `AuthenticatedClient` now reuse an internal [`httpx.Client`](https://www.python-httpx.org/advanced/#client-instances) (or `AsyncClient`)—keeping connections open between requests. This will improve performance overall, but may cause resource leaking if clients are not closed properly. The new clients are intended to be used via context managers—though for compatibility they don't _have_ to be used with context managers. If not using a context manager, connections will probably leak. Note that once a client is closed (by leaving the context manager), it can no longer be used—and attempting to do so will raise an exception.
8+
9+
APIs should now be called like:
10+
11+
```python
12+
with client as client:
13+
my_api.sync(client)
14+
another_api.sync(client)
15+
# client is closed here and can no longer be used
16+
```
17+
18+
Generated READMEs reflect the new syntax, but READMEs for existing generated clients should be updated manually. See [this diff](https://github.com/openapi-generators/openapi-python-client/pull/775/files#diff-62b50316369f84439d58f4981c37538f5b619d344393cb659080dadbda328547) for inspiration.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
default: major
3+
---
4+
5+
#### Minimum httpx version raised to 0.20
6+
7+
Some features of generated clients already failed at runtime when using httpx < 0.20, but now the minimum version is enforced at generation time.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
default: major
3+
---
4+
5+
#### Removed public attributes for `Client` and `AuthenticatedClient`
6+
7+
The following attributes have been removed from `Client` and `AuthenticatedClient`:
8+
9+
- `base_url`—this can now only be set via the initializer
10+
- `cookies`—set at initialization or use `.with_cookies()`
11+
- `headers`—set at initialization or use `.with_headers()`
12+
- `timeout`—set at initialization or use `.with_timeout()`
13+
- `verify_ssl`—this can now only be set via the initializer
14+
- `follow_redirects`—this can now only be set via the initializer
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
#### Stop showing Poetry instructions in generated READMEs when not appropriate
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: major
3+
---
4+
5+
#### The `timeout` param and `with_timeout` now take an `httpx.Timeout` instead of a float

.github/workflows/checks.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ jobs:
9090
integration:
9191
name: Integration Tests
9292
runs-on: ubuntu-latest
93+
strategy:
94+
matrix:
95+
httpx_version:
96+
- "0.20.0"
97+
- ""
9398
services:
9499
openapi-test-server:
95100
image: ghcr.io/openapi-generators/openapi-test-server:0.0.1
@@ -135,9 +140,15 @@ jobs:
135140
python -m venv .venv
136141
poetry run python -m pip install --upgrade pip
137142
poetry install
143+
- name: Set httpx version
144+
if: matrix.httpx_version != ''
145+
run: |
146+
cd integration-tests
147+
poetry run pip install httpx==${{ matrix.httpx_version }}
138148
- name: Run Tests
139149
run: |
140150
cd integration-tests
141151
poetry run pytest
152+
poetry run mypy . --strict
142153
143154

0 commit comments

Comments
 (0)