8000 Support AIOHTTPClient init without running event loop (#1271) · stripe/stripe-python@c71b88d · GitHub
[go: up one dir, main page]

Skip to content

Commit c71b88d

Browse files
authored
Support AIOHTTPClient init without running event loop (#1271)
* Support AIOHTTPClient init without running event loop Lazily instantiates and caches an aiohttp `ClientSession` at the points that it is actually used so that `AIOHTTPClient` can be initialized without a running event loop (e.g. when setting `stripe.default_http_client = stripe.AIOHTTPClient()` at the top level of a module). * Format * Assert aiohttp module is present in AIOHTTPClient Fixes a type checking error by having `AIOHTTPClient`'s `_session` property assert that the `aiohttp` module exists * Remove dependence on cached_property The `functools.cached_property` decorator did not exist in Python 3.7, so `AIOHTTPClient`'s `_session` has been changed into a regular property that performs caching manually by storing the `ClientSession` object as a member of the instance. * Define aiohttp cached session attribute in init Ensures `AIOHTTPClient`'s `_cached_session` attribute is defined in the initializer.
1 parent e8078a3 commit c71b88d

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

stripe/_http_client.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,17 +1394,26 @@ def __init__(
13941394
"Unexpected: tried to initialize AIOHTTPClient but the aiohttp module is not present."
13951395
)
13961396

1397-
kwargs = {}
1398-
if self._verify_ssl_certs:
1399-
ssl_context = ssl.create_default_context(
1400-
cafile=stripe.ca_bundle_path
1401-
)
1402-
kwargs["connector"] = aiohttp.TCPConnector(ssl=ssl_context)
1403-
else:
1404-
kwargs["connector"] = aiohttp.TCPConnector(verify_ssl=False)
1405-
1406-
self._session = aiohttp.ClientSession(**kwargs)
14071397
self._timeout = timeout
1398+
self._cached_session = None
1399+
1400+
@property
1401+
def _session(self):
1402+
assert aiohttp is not None
1403+
1404+
if self._cached_session is None:
1405+
kwargs = {}
1406+
if self._verify_ssl_certs:
1407+
ssl_context = ssl.create_default_context(
1408+
cafile=stripe.ca_bundle_path
1409+
)
1410+
kwargs["connector"] = aiohttp.TCPConnector(ssl=ssl_context)
1411+
else:
1412+
kwargs["connector"] = aiohttp.TCPConnector(verify_ssl=False)
1413+
1414+
self._cached_session = aiohttp.ClientSession(**kwargs)
1415+
1416+
return self._cached_session
14081417

14091418
def sleep_async(self, secs):
14101419
return asyncio.sleep(secs)

tests/test_http_client.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,7 @@ def check_default(self, none_libs, expected):
7070
def test_new_http_client_async_fallback_httpx(self, request_mocks):
7171
self.check_default((), _http_client.HTTPXClient)
7272

73-
# Using the AIOHTTPClient constructor will complain if there's
74-
# no active asyncio event loop. This test can pass in isolation
75-
# but if it runs after another asyncio-enabled test that closes
76-
# the asyncio event loop it will fail unless it is declared to
77-
# use the asyncio backend.
78-
@pytest.mark.anyio
79-
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
80-
async def test_new_http_client_async_fallback_aiohttp(
81-
self, requ 538D est_mocks, anyio_backend
82-
):
73+
def test_new_http_client_async_fallback_aiohttp(self, request_mocks):
8374
self.check_default(
8475
(("httpx"),),
8576
_http_client.AIOHTTPClient,

0 commit comments

Comments
 (0)
0