Skip to content

Commit

Permalink
MOD:增加重试
Browse files Browse the repository at this point in the history
  • Loading branch information
ShilongLee committed Aug 13, 2024
1 parent fb90192 commit 1715267
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/requests/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1715267

Please sign in to comment.