stripe / stripe-python

Python library for the Stripe API.
https://stripe.com
MIT License
1.64k stars 418 forks source link

Expading list-items doesn't work with retrieve_async #1351

Closed rafaelcapucho closed 2 months ago

rafaelcapucho commented 2 months ago

Describe the bug

Requesting list-items isn't working when using async requests

To Reproduce

  1. It's able to use retrieve_async(id) but it isn't supporting the expand option.
    
    import stripe, asyncio

http_client = stripe.AIOHTTPClient()

client = stripe.StripeClient("rk_test_51ItkJ1F0XZ..redacted", http_client=http_client)

cs_id = "cs_test_a1Ew12lsEH1c..redacted"

async def function_working(): return await client.checkout.sessions.retrieve_async(cs_id)

async def function_not_working():

None of the following works

return await client.checkout.sessions.retrieve_async(cs_id, expand=['line_items'])
return await client.checkout.sessions.retrieve_async(cs_id, params=['line_items'])
return await client.checkout.sessions.retrieve_async(cs_id, params={'expand': 'line_items'})

loop = asyncio.new_event_loop() loop.run_until_complete(function_working())

loop = asyncio.new_event_loop() loop.run_until_complete(function_not_working())

Error:

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "", line 2, in f2 File "/usr/local/lib/python3.12/site-packages/stripe/checkout/_session_service.py", line 2378, in retrieve_async await self._request_async( File "/usr/local/lib/python3.12/site-packages/stripe/_stripe_service.py", line 52, in _request_async return await self._requestor.request_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/stripe/_api_requestor.py", line 217, in request_async rbody, rcode, rheaders = await requestor.request_raw_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/stripe/_api_requestor.py", line 683, in request_raw_async ) = await self._get_http_client().request_with_retries_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/stripe/_http_client.py", line 434, in request_with_retries_async return await self._request_with_retries_internal_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/stripe/_http_client.py", line 549, in _request_with_retries_internal_as ync raise connection_error File "/usr/local/lib/python3.12/site-packages/stripe/_http_client.py", line 514, in _request_with_retries_internal_as ync response = await self.request_async( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/stripe/_http_client.py", line 1443, in request_async ) = await self.request_stream_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/stripe/_http_client.py", line 1474, in request_stream_async self._handle_request_error(e) File "/usr/local/lib/python3.12/site-packages/stripe/_http_client.py", line 1458, in _handle_request_error raise APIConnectionError(msg, should_retry=should_retry) stripe._error.APIConnectionError: Unexpected error communicating with Stripe. If this problem persists, let us know at support@stripe.com.

(Network error: A RuntimeError was raised)



### Expected behavior

The session content with the items expanded.

### Code snippets

_No response_

### OS

Linux CoreOS 

### Language version

Python 3.12

### Library version

Latest

### API version

2024-04-10

### Additional context

_No response_
remi-stripe commented 2 months ago

@rafaelcapucho none of the option you're using are correct which is why it's failing. What you need is this:

checkout_session = client.checkout.sessions.retrieve(
  'cs_test_123',
  params={
    'expand': ['line_items'],
  },
)
rafaelcapucho commented 2 months ago

Thank you so much for the reply @remi-stripe, Changing your snippet to retireve_async works, I'm closing the Issue

async def f3():
    print(
        await client.checkout.sessions.retrieve_async(
            cs_id,
            params={
                'expand': ['line_items'],
            },
        )
    )

loop = asyncio.new_event_loop()
loop.run_until_complete(f3())