Add async context manager support for AIOHTTPClient and HTTPXClient#1806
Open
jlaportebot wants to merge 1 commit intostripe:masterfrom
Open
Add async context manager support for AIOHTTPClient and HTTPXClient#1806jlaportebot wants to merge 1 commit intostripe:masterfrom
jlaportebot wants to merge 1 commit intostripe:masterfrom
Conversation
This commit adds async context manager support (__aenter__ and __aexit__)
to both AIOHTTPClient and HTTPXClient, allowing users to use the standard
async with pattern to manage the lifecycle of these clients.
Previously, users had to manually call close_async() to clean up resources,
which was error-prone and less ergonomic. With this change, users can now use:
async with stripe.AIOHTTPClient() as http_client:
client = stripe.StripeClient("sk_test_...", http_client=http_client)
customer = await client.v1.customers.retrieve_async("cus_123")
The context manager automatically calls close_async() when exiting,
ensuring proper resource cleanup even if an exception occurs.
Fixes stripe#1796
|
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds async context manager support (
__aenter__and__aexit__) to both AIOHTTPClient and HTTPXClient, allowing users to use the standardasync withpattern to manage the lifecycle of these clients.Problem
Previously, users had to manually call
close_async()to clean up resources, which was error-prone and less ergonomic.Solution
Added async context manager support to both AIOHTTPClient and HTTPXClient. Now users can use:
The context manager automatically calls
close_async()when exiting, ensuring proper resource cleanup even if an exception occurs.Changes
__aenter__and__aexit__methods toHTTPXClient__aenter__and__aexit__methods toAIOHTTPClientTesting
Added 4 new tests:
test_httpx_async_context_manager: Verifies HTTPXClient context manager workstest_aiohttp_async_context_manager: Verifies AIOHTTPClient context manager workstest_httpx_async_context_manager_with_exception: Verifies HTTPXClient handles exceptionstest_aiohttp_async_context_manager_with_exception: Verifies AIOHTTPClient handles exceptionsAll tests pass.
Fixes #1796