2222import aiohttp .web
2323import attr
2424
25- from dl_configs .utils import get_root_certificates_path
26-
2725
2826LOGGER = logging .getLogger (__name__ )
2927
@@ -93,10 +91,6 @@ async def retry_request(
9391 raise Exception ("You should not be here" )
9492
9593
96- def default_ssl_context () -> ssl .SSLContext :
97- return ssl .create_default_context (cafile = get_root_certificates_path ())
98-
99-
10094@attr .s (kw_only = True )
10195class BIAioHTTPClient :
10296 base_url : str = attr .ib ()
@@ -112,26 +106,24 @@ class BIAioHTTPClient:
112106
113107 retrier : BaseRetrier = attr .ib (factory = NoRetriesRetrier )
114108
115- ssl_context : Optional [ssl .SSLContext ] = attr .ib (factory = default_ssl_context )
116- _session : Optional [aiohttp .ClientSession ] = attr .ib ()
117- close_session_on_exit : Optional [bool ] = attr .ib (default = None )
109+ _ca_data : bytes = attr .ib ()
110+ _session : Optional [aiohttp .ClientSession ] = attr .ib (init = False )
118111
119- def make_default_session (self ) -> aiohttp .ClientSession :
112+ def __attrs_post_init__ (self ):
113+ self ._session = self ._make_session ()
114+
115+ def _make_session (self ) -> aiohttp .ClientSession :
116+ ssl_context = ssl .create_default_context (cadata = self ._ca_data .decode ("utf-8" ))
120117 return aiohttp .ClientSession (
121- cookies = self .cookies , headers = self .headers , connector = aiohttp .TCPConnector (ssl_context = self .ssl_context )
118+ cookies = self .cookies ,
119+ headers = self .headers ,
120+ connector = aiohttp .TCPConnector (
121+ ssl_context = ssl_context ,
122+ ),
122123 )
123124
124- @property
125- def session (self ) -> aiohttp .ClientSession :
126- if self ._session is None :
127- self ._session = self .make_default_session ()
128- if self .close_session_on_exit is None :
129- self .close_session_on_exit = True
130- return self ._session
131-
132125 async def close (self ) -> None :
133- if self .close_session_on_exit :
134- await self .session .close ()
126+ await self ._session .close ()
135127
136128 async def __aenter__ (self ) -> BIAioHTTPClient :
137129 return self
@@ -168,7 +160,7 @@ async def _request(
168160 sock_connect = conn_timeout_sec or self .conn_timeout_sec ,
169161 sock_read = read_timeout_sec or self .read_timeout_sec ,
170162 )
171- return await self .session .request (
163+ return await self ._session .request (
172164 method = method ,
173165 url = self .url (path ),
174166 params = params ,
0 commit comments