Skip to content
Snippets Groups Projects
Commit 5acfb4e6 authored by Krzysztof Mochocki's avatar Krzysztof Mochocki
Browse files

Fix potential bug with creation of communicators session

parent 9c92af97
No related branches found
No related tags found
1 merge request!73Clive integration related fixes
...@@ -25,7 +25,7 @@ class AioHttpCommunicator(AbstractCommunicator): ...@@ -25,7 +25,7 @@ class AioHttpCommunicator(AbstractCommunicator):
self.__session: aiohttp.ClientSession | None = None self.__session: aiohttp.ClientSession | None = None
@property @property
def session(self) -> aiohttp.ClientSession: async def session(self) -> aiohttp.ClientSession:
if self.__session is None: if self.__session is None:
self.__session = aiohttp.ClientSession( self.__session = aiohttp.ClientSession(
headers=self._json_headers(), timeout=aiohttp.ClientTimeout(total=self.settings.timeout.total_seconds()) headers=self._json_headers(), timeout=aiohttp.ClientTimeout(total=self.settings.timeout.total_seconds())
...@@ -38,7 +38,7 @@ class AioHttpCommunicator(AbstractCommunicator): ...@@ -38,7 +38,7 @@ class AioHttpCommunicator(AbstractCommunicator):
while not self._is_amount_of_retries_exceeded(amount=amount_of_retries): while not self._is_amount_of_retries_exceeded(amount=amount_of_retries):
amount_of_retries += 1 amount_of_retries += 1
try: try:
async with self.session.post(url.as_string(), data=data) as response: async with (await self.session).post(url.as_string(), data=data) as response:
return await response.text() return await response.text()
except (aiohttp.ServerTimeoutError, asyncio.TimeoutError): except (aiohttp.ServerTimeoutError, asyncio.TimeoutError):
last_exception = self._construct_timeout_exception(url, data, stopwatch.lap) last_exception = self._construct_timeout_exception(url, data, stopwatch.lap)
...@@ -56,4 +56,5 @@ class AioHttpCommunicator(AbstractCommunicator): ...@@ -56,4 +56,5 @@ class AioHttpCommunicator(AbstractCommunicator):
raise NotImplementedError raise NotImplementedError
def teardown(self) -> None: def teardown(self) -> None:
self._asyncio_run(self.session.close()) if self.__session is not None:
self._asyncio_run(self.__session.close())
...@@ -37,7 +37,7 @@ class HttpxCommunicator(AbstractCommunicator): ...@@ -37,7 +37,7 @@ class HttpxCommunicator(AbstractCommunicator):
def __create_client(self, client_type: type[ClientTypes]) -> ClientTypes: def __create_client(self, client_type: type[ClientTypes]) -> ClientTypes:
return client_type(timeout=self.settings.timeout.total_seconds(), http2=True) return client_type(timeout=self.settings.timeout.total_seconds(), http2=True)
def get_async_client(self) -> httpx.AsyncClient: async def get_async_client(self) -> httpx.AsyncClient:
if self.__async_client is None: if self.__async_client is None:
self.__async_client = cast(httpx.AsyncClient, self.__create_client(httpx.AsyncClient)) self.__async_client = cast(httpx.AsyncClient, self.__create_client(httpx.AsyncClient))
return self.__async_client return self.__async_client
...@@ -53,7 +53,7 @@ class HttpxCommunicator(AbstractCommunicator): ...@@ -53,7 +53,7 @@ class HttpxCommunicator(AbstractCommunicator):
while not self._is_amount_of_retries_exceeded(amount=amount_of_retries): while not self._is_amount_of_retries_exceeded(amount=amount_of_retries):
amount_of_retries += 1 amount_of_retries += 1
try: try:
response: httpx.Response = await self.get_async_client().post( response: httpx.Response = await (await self.get_async_client()).post(
url.as_string(), content=data, headers=self._json_headers() url.as_string(), content=data, headers=self._json_headers()
) )
data_received = response.content.decode() data_received = response.content.decode()
......
...@@ -18,7 +18,7 @@ if TYPE_CHECKING: ...@@ -18,7 +18,7 @@ if TYPE_CHECKING:
async def send_notification(address: HttpUrl, notification: KnownNotificationT) -> None: async def send_notification(address: HttpUrl, notification: KnownNotificationT) -> None:
communicator = HttpxCommunicator(settings=helpy._communication.settings.CommunicationSettings()) communicator = HttpxCommunicator(settings=helpy._communication.settings.CommunicationSettings())
await communicator.get_async_client().put( await (await communicator.get_async_client()).put(
address.as_string(), address.as_string(),
headers=communicator._json_headers(), headers=communicator._json_headers(),
content=Notification( content=Notification(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment