You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
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:
**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:
#### 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.
#### 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.
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
@@ -61,8 +63,6 @@ client = AuthenticatedClient(
61
63
)
62
64
```
63
65
64
-
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.
65
-
66
66
Things to know:
67
67
1. Every path/method combo becomes a Python module with four functions:
68
68
1.`sync`: Blocking request that returns parsed data (if successful) or `None`
@@ -74,7 +74,42 @@ Things to know:
74
74
1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)
75
75
1. Any endpoint which did not have a tag will be in `my_test_api_client.api.default`
76
76
77
-
## Building / publishing this Client
77
+
## Advanced customizations
78
+
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):
80
+
81
+
```python
82
+
from my_test_api_client import Client
83
+
84
+
deflog_request(request):
85
+
print(f"Request event hook: {request.method}{request.url} - Waiting for response")
86
+
87
+
deflog_response(response):
88
+
request = response.request
89
+
print(f"Response event hook: {request.method}{request.url} - Status {response.status_code}")
0 commit comments