Fix downloading

This commit is contained in:
2023-01-22 23:03:08 +01:00
parent 34e0d001ec
commit 778d6429d5
4 changed files with 61 additions and 36 deletions

View File

@@ -2,6 +2,7 @@ from base64 import b64decode
from typing import Optional
import httpx
from sentry_sdk import capture_exception
from core.config import env_config
@@ -37,14 +38,18 @@ async def download(
async def get_filename(book_id: int, file_type: str) -> Optional[str]:
headers = {"Authorization": env_config.DOWNLOADER_API_KEY}
async with httpx.AsyncClient() as client:
response = await client.get(
f"{env_config.DOWNLOADER_URL}/filename/{book_id}/{file_type}",
headers=headers,
timeout=5 * 60,
)
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"{env_config.DOWNLOADER_URL}/filename/{book_id}/{file_type}",
headers=headers,
timeout=5 * 60,
)
if response.status_code != 200:
return None
if response.status_code != 200:
return None
return response.text
return response.text
except httpx.HTTPError as e:
capture_exception(e)
return None

View File

@@ -3,6 +3,7 @@ from typing import Generic, Optional, TypeVar
import httpx
from pydantic import BaseModel
from sentry_sdk import capture_exception
from core.config import env_config
@@ -47,8 +48,13 @@ class BookDetail(Book):
AUTH_HEADERS = {"Authorization": env_config.LIBRARY_API_KEY}
async def get_book(book_id: int, retry: int = 3) -> Optional[BookDetail]:
async def get_book(
book_id: int, retry: int = 3, last_exp: Exception | None = None
) -> Optional[BookDetail]:
if retry == 0:
if last_exp:
capture_exception(last_exp)
return None
try:
@@ -61,8 +67,8 @@ async def get_book(book_id: int, retry: int = 3) -> Optional[BookDetail]:
return None
return BookDetail.parse_obj(response.json())
except (httpx.ConnectError, httpx.ReadError, httpx.ReadTimeout):
return await get_book(book_id, retry=retry - 1)
except httpx.HTTPError as e:
return await get_book(book_id, retry=retry - 1, last_exp=e)
async def get_books(page: int, page_size: int) -> Page[Book]: