From 6b684d7f57ef329b757fb5223e53aaca09080725 Mon Sep 17 00:00:00 2001 From: nareksa Date: Tue, 16 Jan 2024 12:39:51 +0400 Subject: [PATCH] fix AIOHttpSession retry --- .../lib/infrastructure/services/annotation.py | 6 +++--- .../lib/infrastructure/services/http_client.py | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/superannotate/lib/infrastructure/services/annotation.py b/src/superannotate/lib/infrastructure/services/annotation.py index d16dd6661..9d478e6e9 100644 --- a/src/superannotate/lib/infrastructure/services/annotation.py +++ b/src/superannotate/lib/infrastructure/services/annotation.py @@ -80,9 +80,9 @@ async def _sync_large_annotation(self, team_id, project_id, item_id): async with AIOHttpSession( connector=aiohttp.TCPConnector(ssl=False), headers=self.client.default_headers, + raise_for_status=True, ) as session: _response = await session.request("post", sync_url, params=sync_params) - _response.raise_for_status() sync_params.pop("current_source") sync_params.pop("desired_source") @@ -123,9 +123,9 @@ async def get_big_annotation( async with AIOHttpSession( connector=aiohttp.TCPConnector(ssl=False), headers=self.client.default_headers, + raise_for_status=True, ) as session: start_response = await session.request("post", url, params=query_params) - start_response.raise_for_status() large_annotation = await start_response.json() reporter.update_progress() @@ -206,9 +206,9 @@ async def download_big_annotation( async with AIOHttpSession( connector=aiohttp.TCPConnector(ssl=False), headers=self.client.default_headers, + raise_for_status=True, ) as session: start_response = await session.request("post", url, params=query_params) - start_response.raise_for_status() res = await start_response.json() Path(download_path).mkdir(exist_ok=True, parents=True) diff --git a/src/superannotate/lib/infrastructure/services/http_client.py b/src/superannotate/lib/infrastructure/services/http_client.py index bfd25e6db..78783a3e9 100644 --- a/src/superannotate/lib/infrastructure/services/http_client.py +++ b/src/superannotate/lib/infrastructure/services/http_client.py @@ -251,12 +251,13 @@ async def request(self, *args, **kwargs) -> aiohttp.ClientResponse: response = await super()._request(*args, **kwargs) if attempts <= 1 or response.status not in self.RETRY_STATUS_CODES: return response + if isinstance(kwargs["data"], aiohttp.FormData): + raise RuntimeError(await response.text()) except (aiohttp.ClientError, RuntimeError) as e: if attempts <= 1: raise - if isinstance(e, RuntimeError): - data = kwargs["data"] - if isinstance(data, aiohttp.FormData): - kwargs["data"] = self._copy_form_data(data) + data = kwargs["data"] + if isinstance(data, aiohttp.FormData): + kwargs["data"] = self._copy_form_data(data) attempts -= 1 await asyncio.sleep(delay)