diff --git a/src/app/services/book_library.py b/src/app/services/book_library.py index 1da9c77..4770dd2 100644 --- a/src/app/services/book_library.py +++ b/src/app/services/book_library.py @@ -38,6 +38,10 @@ class Book(BaseModel): authors: list[BookAuthor] +class BookDetail(Book): + remote_id: int + + class BookLibraryClient: API_KEY = env_config.BOOK_LIBRARY_API_KEY BASE_URL = env_config.BOOK_LIBRARY_URL @@ -61,6 +65,12 @@ class BookLibraryClient: return [Source.parse_obj(item) for item in page.items] + @classmethod + async def get_book(cls, book_id: int) -> BookDetail: + data = await cls._make_request(f"{cls.BASE_URL}/api/v1/books/{book_id}") + + return BookDetail.parse_obj(data) + @classmethod async def get_remote_book(cls, source_id: int, book_id: int) -> Book: data = await cls._make_request( diff --git a/src/app/views.py b/src/app/views.py index d8e623b..865fc21 100644 --- a/src/app/views.py +++ b/src/app/views.py @@ -1,8 +1,9 @@ -from fastapi import APIRouter, Depends -from fastapi.responses import Response +from fastapi import APIRouter, Depends, Response from app.depends import check_token +from app.services.book_library import BookLibraryClient from app.services.dowloaders_manager import DownloadersManager +from app.services.utils import get_filename as _get_filename router = APIRouter( @@ -20,3 +21,10 @@ async def download(source_id: int, remote_id: int, file_type: str): return Response( content, headers={"Content-Disposition": f"attachment; filename={filename}"} ) + + +@router.get("/filename/{book_id}/{file_type}", response_model=str) +async def get_filename(book_id: int, file_type: str): + book = await BookLibraryClient.get_book(book_id) + + return _get_filename(book.remote_id, book, file_type)