From 17152676b038bb18044f366eefef7cdf1bd5f8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=96=E9=BE=99?= <1261586682@qq.com> Date: Tue, 13 Aug 2024 09:30:06 +0800 Subject: [PATCH] =?UTF-8?q?MOD:=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/requests/requests.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/requests/requests.py b/lib/requests/requests.py index 521b48d..3e577b3 100644 --- a/lib/requests/requests.py +++ b/lib/requests/requests.py @@ -9,11 +9,25 @@ def __init__(self, status_code, text): def json(self): return json.loads(self.text) +def retry_request(func, max_retries=3): + async def wrapper(*args, **kwargs): + retries = 0 + while retries < max_retries: + try: + return await func(*args, **kwargs) + except (httpx.ConnectError, httpx.ReadTimeout) as e: + retries += 1 + raise Exception(f"Failed after {max_retries} retries") + + return wrapper + +@retry_request async def get(url, headers=None, params=None) -> Response: async with httpx.AsyncClient() as client: response = await client.get(url, headers=headers, params=params) return Response(response.status_code, response.text) +@retry_request async def post(url, headers=None, data=None, json=None) -> Response: async with httpx.AsyncClient() as client: response = await client.post(url, headers=headers, json=json, data=data)