- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 229
Redesign client for more flexibility via direct httpx
access
#775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,526
−1,881
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e1fa3da
Prototype tighter httpx integration
dbanty a9618ba
Blacken
dbanty 6bedc75
New client template
dbanty 544ddcf
Update templates & tests
dbanty a43738f
Update generated readmes
dbanty 5b3199c
Rename `*_client` functions to `*_httpx_client`
dbanty 3ef087b
Allow AuthenticatedClient anywhere a Client is allowed
dbanty 156c0f1
Use macros to keep Client/AuthenticatedClient the same
dbanty f9ab1de
Add changeset notes
dbanty 0dfb61f
Add integration test for minimal httpx version
dbanty 1b37b55
Add mypy to integration tests
dbanty d350660
Install lower httpx in the right place for integration tests
dbanty 0ff5e55
Update every attrs to use new syntax, raise minimum httpx version
dbanty 7c411c5
More release dry runs and prerelease action
dbanty 8cab8e0
Put back missing tabs
dbanty ea0f53a
Put back missing tabs
dbanty dfdfe31
Merge branch 'main' into use-httpx-client-directly
dbanty e5e4cd5
Update end_to_end_tests/golden-record/my_test_api_client/client.py
dbanty 88b4259
Regen
dbanty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
.changeset/allow_customizing_the_underlying_httpx_clients.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
default: minor | ||
--- | ||
|
||
#### Allow customizing the underlying `httpx` clients | ||
|
||
There are many use-cases where customizing the underlying `httpx` client directly is necessary. Some examples are: | ||
|
||
- [Event hooks](https://www.python-httpx.org/advanced/#event-hooks) | ||
- [Proxies](https://www.python-httpx.org/advanced/#http-proxying) | ||
- [Custom authentication](https://www.python-httpx.org/advanced/#customizing-authentication) | ||
- [Retries](https://www.python-httpx.org/advanced/#usage_1) | ||
|
||
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: | ||
|
||
```python | ||
client = Client(base_url="https://api.example.com", httpx_args={"proxies": {"https://": "https://proxy.example.com"}}) | ||
``` | ||
|
||
**The underlying clients are constructed lazily, only when needed. `httpx_args` are stored internally in a dictionary until the first request is made.** | ||
|
||
You can force immediate construction of an underlying client in order to edit it directly: | ||
|
||
```python | ||
import httpx | ||
from my_api import Client | ||
|
||
client = Client(base_url="https://api.example.com") | ||
sync_client: httpx.Client = client.get_httpx_client() | ||
sync_client.timeout = 10 | ||
async_client = client.get_async_httpx_client() | ||
async_client.timeout = 15 | ||
``` | ||
|
||
You can also completely override the underlying clients: | ||
|
||
```python | ||
import httpx | ||
from my_api import Client | ||
|
||
client = Client(base_url="https://api.example.com") | ||
# The params you put in here ^ are discarded when you call set_httpx_client or set_async_httpx_client | ||
sync_client = httpx.Client(base_url="https://api.example.com", timeout=10) | ||
client.set_httpx_client(sync_client) | ||
async_client = httpx.AsyncClient(base_url="https://api.example.com", timeout=15) | ||
client.set_async_httpx_client(async_client) | ||
``` |
7 changes: 7 additions & 0 deletions
7
.changeset/authenticatedclient_no_longer_inherits_from_client.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
default: major | ||
--- | ||
|
||
#### `AuthenticatedClient` no longer inherits from `Client` | ||
|
||
The API of `AuthenticatedClient` is still a superset of `Client`, but the two classes no longer share a common base class. | ||
7 changes: 7 additions & 0 deletions
7
...client_and_authenticatedclient_now_use_the_newer_attrs_define_and_field_apis.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
default: major | ||
--- | ||
|
||
#### Generated clients and models now use the newer attrs `@define` and `field` APIs | ||
|
||
See [the attrs docs](https://www.attrs.org/en/stable/names.html#attrs-tng) for more information on how these may affect you. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
default: minor | ||
--- | ||
|
||
#### Clients now reuse connections between requests | ||
|
||
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
default: major | ||
--- | ||
|
||
#### Connections from clients no longer automatically close (PR [#775](https://github.com/openapi-generators/openapi-python-client/pull/775)) | ||
|
||
`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. | ||
|
||
APIs should now be called like: | ||
|
||
```python | ||
with client as client: | ||
my_api.sync(client) | ||
another_api.sync(client) | ||
# client is closed here and can no longer be used | ||
``` | ||
|
||
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
default: major | ||
--- | ||
|
||
#### Minimum httpx version raised to 0.20 | ||
|
||
Some features of generated clients already failed at runtime when using httpx < 0.20, but now the minimum version is enforced at generation time. |
14 changes: 14 additions & 0 deletions
14
.changeset/removed_public_attributes_for_client_and_authenticatedclient.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
default: major | ||
--- | ||
|
||
#### Removed public attributes for `Client` and `AuthenticatedClient` | ||
|
||
The following attributes have been removed from `Client` and `AuthenticatedClient`: | ||
|
||
- `base_url`—this can now only be set via the initializer | ||
- `cookies`—set at initialization or use `.with_cookies()` | ||
- `headers`—set at initialization or use `.with_headers()` | ||
- `timeout`—set at initialization or use `.with_timeout()` | ||
- `verify_ssl`—this can now only be set via the initializer | ||
- `follow_redirects`—this can now only be set via the initializer |
5 changes: 5 additions & 0 deletions
5
...t/stop_showing_poetry_instructions_in_generated_readmes_when_not_appropriate.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
default: patch | ||
--- | ||
|
||
#### Stop showing Poetry instructions in generated READMEs when not appropriate |
5 changes: 5 additions & 0 deletions
5
...e_timeout_param_and_with_timeout_now_take_an_httpxtimeout_instead_of_a_float.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
default: major | ||
--- | ||
|
||
#### The `timeout` param and `with_timeout` now take an `httpx.Timeout` instead of a float |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.