Skip to content
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

Beta: async streaming #1233

Merged
merged 9 commits into from
Feb 14, 2024
Merged

Beta: async streaming #1233

merged 9 commits into from
Feb 14, 2024

Conversation

richardm-stripe
Copy link
Contributor

@richardm-stripe richardm-stripe commented Feb 13, 2024

Summary

Provides an implementation for request_stream_async for HTTPClientAsync, so e.g. await stripe.Quote.pdf_async(...) will work.

chunks = []
result = await stripe.Quote.pdf_async("qt_123")
async for chunk in result.stream:
    chunks.append(str(chunk, "utf-8"))

Details

  • There's a new public type StripeStreamResponseAsync, which is the async analog of StripeStreamResponse. (This is the type that will be returned to the user when calling stripe.Quote.pdf_async). The difference from StripeStreamResponse is that the stream is represented by stream: AsyncIterable[bytes] instead of io: IOBase.
    • FYI: IOBase implements Iterable[bytes] -- so there's more symmetry between StripeStreamResponse and StripeResponse than appears at first glance.
  • APIRequestor has been modified to use/produce it accordingly.
  • The return type of HTTPClientAsync.request_stream_async is now Tuple[AsyncIterable[bytes], ...] and an implementation is provided for HTTPXHttpClient.

I was worried about a resource leak -- inside HTTPXHttpClient it calls response = httpx.ASyncClient().send(...) and then takes content = response.aiter_bytes() and only returns content to the caller. Does response get closed properly? I manually tested, and yes, after you do an async for through the bytes in content the response gets closed automatically.

@richardm-stripe richardm-stripe marked this pull request as ready for review February 14, 2024 02:17
@richardm-stripe richardm-stripe changed the title [WIP] Beta: async streaming Beta: async streaming Feb 14, 2024
@richardm-stripe richardm-stripe requested review from a team and helenye-stripe and removed request for a team February 14, 2024 02:26
Copy link
Contributor

@anniel-stripe anniel-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Left a few nits / questions but nothing blocking

@richardm-stripe richardm-stripe enabled auto-merge (squash) February 14, 2024 22:53
@richardm-stripe richardm-stripe enabled auto-merge (squash) February 14, 2024 22:53
@richardm-stripe richardm-stripe merged commit c4bc5d2 into beta Feb 14, 2024
14 checks passed
@xavdid-stripe xavdid-stripe deleted the richardm-async-streaming branch May 10, 2024 03:31
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.

None yet

2 participants