Skip to content

Add async context manager support for AIOHTTPClient and HTTPXClient #1796

@Zhang-Charlie

Description

@Zhang-Charlie

Is your feature request related to a problem? Please describe.

When using the async HTTP clients directly, there is currently no idiomatic way to manage their lifecycle with async with.

In stripe/_http_client.py:

  • AIOHTTPClient.close() is a no-op
  • AIOHTTPClient cleanup happens through close_async()
  • HTTPXClient also exposes close_async()
  • async-capable clients do not appear to expose __aenter__ / __aexit__

That means users need to remember to call close_async() manually instead of using the standard async context-manager pattern.

Describe the solution you'd like

Add async context manager support to async-capable HTTP clients, for example:

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")

Describe alternatives you've considered

Users can call await close_async() manually after using the client, but that is easier to forget and less ergonomic than supporting the standard async with pattern.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions