Skip to content

Add async context manager support for AIOHTTPClient and HTTPXClient#1806

Open
jlaportebot wants to merge 1 commit intostripe:masterfrom
jlaportebot:feature/async-context-manager-support
Open

Add async context manager support for AIOHTTPClient and HTTPXClient#1806
jlaportebot wants to merge 1 commit intostripe:masterfrom
jlaportebot:feature/async-context-manager-support

Conversation

@jlaportebot
Copy link
Copy Markdown

This PR 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.

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:

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.

Changes

  • Added __aenter__ and __aexit__ methods to HTTPXClient
  • Added __aenter__ and __aexit__ methods to AIOHTTPClient
  • Added comprehensive tests for async context manager support
  • Tests verify that context managers work correctly and handle exceptions properly

Testing

Added 4 new tests:

  • test_httpx_async_context_manager: Verifies HTTPXClient context manager works
  • test_aiohttp_async_context_manager: Verifies AIOHTTPClient context manager works
  • test_httpx_async_context_manager_with_exception: Verifies HTTPXClient handles exceptions
  • test_aiohttp_async_context_manager_with_exception: Verifies AIOHTTPClient handles exceptions

All tests pass.

Fixes #1796

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
@jlaportebot jlaportebot requested a review from a team as a code owner May 7, 2026 07:49
@jlaportebot jlaportebot requested review from xavdid and removed request for a team May 7, 2026 07:49
@cla-assistant
Copy link
Copy Markdown

cla-assistant Bot commented May 7, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add async context manager support for AIOHTTPClient and HTTPXClient

1 participant