@@ -1401,6 +1401,8 @@ def __init__(
14011401 self ,
14021402 timeout : Optional [Union [float , "AIOHTTPTimeout" ]] = 80 ,
14031403 _lib = None , # used for internal unit testing
1404+ session = None ,
1405+ connector = None ,
14041406 ** kwargs ,
14051407 ):
14061408 super (AIOHTTPClient , self ).__init__ (** kwargs )
@@ -1413,25 +1415,33 @@ def __init__(
14131415 self .aiohttp = _lib
14141416
14151417 self ._timeout = timeout
1418+ self ._user_session = session
1419+ self ._user_connector = connector
1420+ self ._internally_managed_session = session is None
14161421 self ._cached_session = None
14171422
14181423 @property
14191424 def _session (self ):
14201425 if self ._cached_session is None :
1421- kwargs = {}
1422- if self ._verify_ssl_certs :
1423- ssl_context = ssl .create_default_context (
1424- cafile = stripe .ca_bundle_path
1425- )
1426- kwargs ["connector" ] = self .aiohttp .TCPConnector (
1427- ssl = ssl_context
1428- )
1426+ if self ._user_session :
1427+ self ._cached_session = self ._user_session
14291428 else :
1430- kwargs ["connector" ] = self .aiohttp .TCPConnector (
1431- verify_ssl = False
1432- )
1429+ kwargs = {}
1430+ if self ._user_connector :
1431+ kwargs ["connector" ] = self ._user_connector
1432+ elif self ._verify_ssl_certs :
1433+ ssl_context = ssl .create_default_context (
1434+ cafile = stripe .ca_bundle_path
1435+ )
1436+ kwargs ["connector" ] = self .aiohttp .TCPConnector (
1437+ ssl = ssl_context
1438+ )
1439+ else :
1440+ kwargs ["connector" ] = self .aiohttp .TCPConnector (
1441+ verify_ssl = False
1442+ )
14331443
1434- self ._cached_session = self .aiohttp .ClientSession (** kwargs )
1444+ self ._cached_session = self .aiohttp .ClientSession (** kwargs )
14351445
14361446 return self ._cached_session
14371447
@@ -1514,7 +1524,8 @@ def close(self):
15141524 pass
15151525
15161526 async def close_async (self ):
1517- await self ._session .close ()
1527+ if self ._internally_managed_session :
1528+ await self ._session .close ()
15181529
15191530
15201531class NoImportFoundAsyncClient (HTTPClient ):
0 commit comments