Fix closing client

This commit is contained in:
2022-04-25 13:37:08 +03:00
parent 70bb96e9c3
commit e0931150a0
2 changed files with 3 additions and 8 deletions

View File

@@ -54,8 +54,7 @@ class BookLibraryClient:
@classmethod @classmethod
async def _make_request(cls, url) -> dict: async def _make_request(cls, url) -> dict:
async with httpx.AsyncClient(timeout=60) as client: async with httpx.AsyncClient(timeout=60) as client:
response = await client.get(url, headers=cls.auth_headers) return (await client.get(url, headers=cls.auth_headers)).json()
return response.json()
@classmethod @classmethod
async def get_sources(cls) -> list[Source]: async def get_sources(cls) -> list[Source]:

View File

@@ -83,17 +83,13 @@ class FLDownloader(BaseDownloader):
content_type = response.headers.get("Content-Type") content_type = response.headers.get("Content-Type")
if response.status_code != 200: if response.status_code != 200:
await response.aclose()
await client.aclose()
raise NotSuccess(f"Status code is {response.status_code}!") raise NotSuccess(f"Status code is {response.status_code}!")
if "text/html" in content_type: if "text/html" in content_type:
await response.aclose()
await client.aclose()
raise ReceivedHTML() raise ReceivedHTML()
return client, response, "application/zip" in content_type return client, response, "application/zip" in content_type
except asyncio.CancelledError: except (asyncio.CancelledError, NotSuccess, ReceivedHTML):
await client.aclose() await client.aclose()
await client.aclose() await client.aclose()
raise raise
@@ -224,7 +220,7 @@ class FLDownloader(BaseDownloader):
raise ConvertationError raise ConvertationError
return converter_client, converter_response, False return converter_client, converter_response, False
except asyncio.CancelledError: except (asyncio.CancelledError, ConvertationError):
await converter_response.aclose() await converter_response.aclose()
await converter_client.aclose() await converter_client.aclose()
await aiofiles.os.remove(filename_to_convert) await aiofiles.os.remove(filename_to_convert)