From 473caa4bcb541933c7249d831333a0cd9a693da3 Mon Sep 17 00:00:00 2001 From: TheOnlyWayUp Date: Tue, 25 Jun 2024 15:51:40 +0000 Subject: [PATCH] feat(api): Add expontential backoff on failed requests --- src/api/requirements.txt | 1 + src/api/src/create_book.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/api/requirements.txt b/src/api/requirements.txt index 1278a35..06164e5 100644 --- a/src/api/requirements.txt +++ b/src/api/requirements.txt @@ -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 diff --git a/src/api/src/create_book.py b/src/api/src/create_book.py index f407deb..a389c7f 100644 --- a/src/api/src/create_book.py +++ b/src/api/src/create_book.py @@ -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