From d51e5c64ccdb101a42315c0725554ff124314406 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 25 Apr 2023 23:00:44 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20post=20timeout=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/apihelper/client/base/httpxrequest.py | 14 ++++++++++++-- modules/apihelper/client/components/hyperion.py | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/apihelper/client/base/httpxrequest.py b/modules/apihelper/client/base/httpxrequest.py index 7557f88..0a7bb3c 100644 --- a/modules/apihelper/client/base/httpxrequest.py +++ b/modules/apihelper/client/base/httpxrequest.py @@ -6,10 +6,20 @@ import httpx __all__ = ("HTTPXRequest",) +from core.config import config + +timeout = httpx.Timeout( + timeout=config.timeout, + read=config.read_timeout, + write=config.write_timeout, + connect=config.connect_timeout, + pool=config.pool_timeout, +) + class HTTPXRequest(AbstractAsyncContextManager): def __init__(self, *args, headers=None, **kwargs): - self._client = httpx.AsyncClient(headers=headers, *args, **kwargs) + self._client = httpx.AsyncClient(headers=headers, timeout=timeout, *args, **kwargs) async def __aenter__(self): try: @@ -26,7 +36,7 @@ class HTTPXRequest(AbstractAsyncContextManager): async def initialize(self): if self._client.is_closed: - self._client = httpx.AsyncClient() + self._client = httpx.AsyncClient(timeout=timeout) async def shutdown(self): if self._client.is_closed: diff --git a/modules/apihelper/client/components/hyperion.py b/modules/apihelper/client/components/hyperion.py index 3031324..3c8d7e6 100644 --- a/modules/apihelper/client/components/hyperion.py +++ b/modules/apihelper/client/components/hyperion.py @@ -123,7 +123,7 @@ class Hyperion: _, file_extension = os.path.splitext(filename) is_image = bool(file_extension in ".jpg" or file_extension in ".png") response = await self.client.get( - url, params=self.get_images_params(resize=2000) if is_image else None, timeout=10, de_json=False + url, params=self.get_images_params(resize=2000) if is_image else None, de_json=False ) return ArtworkImage( art_id=art_id, page=page, file_name=filename, file_extension=url.split(".")[-1], data=response.content