feat(api): Add expontential backoff on failed requests

This commit is contained in:
TheOnlyWayUp
2024-06-25 15:51:40 +00:00
parent 57d7f74922
commit 473caa4bcb
2 changed files with 13 additions and 3 deletions
+1
View File
@@ -8,6 +8,7 @@ anyio==4.2.0
asttokens==2.4.1
async-timeout==4.0.3
attrs==23.1.0
backoff==2.2.1
click==8.1.7
comm==0.2.0
debugpy==1.8.0
+12 -3
View File
@@ -2,9 +2,12 @@ import asyncio
from ebooklib import epub
import unicodedata
import re
import backoff
from aiohttp import ClientResponseError
from aiohttp_client_cache.session import CachedSession
from aiohttp_client_cache import FileBackend
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
@@ -15,6 +18,7 @@ cache = FileBackend(
)
@backoff.on_exception(backoff.expo, ClientResponseError, max_time=15)
async def retrieve_story(story_id: int, retry=True) -> dict:
"""Taking a story_id, return its information from the Wattpad API."""
async with CachedSession(headers=headers, cache=cache) as session:
@@ -24,12 +28,14 @@ async def retrieve_story(story_id: int, retry=True) -> dict:
if not response.ok:
if response.status in [404, 400]:
return {}
raise ValueError("Status Code:", response.status)
response.raise_for_status()
body = await response.json()
return body
@backoff.on_exception(backoff.expo, ClientResponseError, max_time=15)
async def fetch_part_content(part_id: int) -> str:
"""Return the HTML Content of a Part."""
async with CachedSession(headers=headers, cache=cache) as session:
@@ -39,12 +45,14 @@ async def fetch_part_content(part_id: int) -> str:
if not response.ok:
if response.status in [404, 400]:
return ""
raise ValueError("Status Code:", response.status)
response.raise_for_status()
body = await response.text()
return body
@backoff.on_exception(backoff.expo, ClientResponseError, max_time=15)
async def fetch_cover(url: str) -> bytes:
"""Fetch image bytes."""
async with CachedSession(headers=headers, cache=cache) as session:
@@ -52,7 +60,8 @@ async def fetch_cover(url: str) -> bytes:
if not response.ok:
if response.status in [404, 400]:
return bytes()
raise ValueError("Status Code:", response.status)
response.raise_for_status()
body = await response.read()
return body