From fbefb12b31661b0bb271b498796b5844bbe49cc5 Mon Sep 17 00:00:00 2001 From: Aleksey Date: Thu, 7 Sep 2023 18:21:16 -0300 Subject: [PATCH] Add session parameter to AsyncClient --- fhirpy/base/lib.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/fhirpy/base/lib.py b/fhirpy/base/lib.py index 64e0a5a..26b3f1b 100644 --- a/fhirpy/base/lib.py +++ b/fhirpy/base/lib.py @@ -107,9 +107,11 @@ def _build_request_url(self, path, params): class AsyncClient(AbstractClient, ABC): aiohttp_config = None + session: aiohttp.ClientSession = None - def __init__(self, url, authorization=None, extra_headers=None, aiohttp_config=None): + def __init__(self, url, authorization=None, extra_headers=None, aiohttp_config=None, session=None): self.aiohttp_config = aiohttp_config or {} + self.session = session super().__init__(url, authorization, extra_headers) @@ -119,8 +121,10 @@ async def execute(self, path, method="post", **kwargs): async def _do_request(self, method, path, data=None, params=None, returning_status=False): headers = self._build_request_headers() url = self._build_request_url(path, params) - async with aiohttp.ClientSession(headers=headers) as session: - async with session.request(method, url, json=data, **self.aiohttp_config) as r: + _session = self.session or aiohttp.ClientSession(headers=headers) + async with _session as session: + logging.error('session id %s', id(session)) + async with session.request(method, url, json=data,headers = headers, **self.aiohttp_config) as r: if 200 <= r.status < 300: data = await r.text() r_data = json.loads(data, object_hook=AttrDict) if data else None