This project allow to add .epub books (they will be handled and added to DB (book name, author name, names and texts of chapters)), search in book with query, returning cutted fragment of found text.
Project using:
- Clone current repo:
git clone https://github.com/xaer981/book_api.git
- Go to the new dir and create .env file from example:
cd book_api/ && cp default.env .env
- ADMIN_USER - username for using in HTTP Basic auth to add new books to DB
- ADMIN_PASSWORD - password for using in HTTP Basic auth to add new books to DB
- DB_URL - default url to get access to DB from FastAPI(
postgresql://postgres:postgres@book_api-db/{name of DB}
) - POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_DB - name of DB. If not default, also need to change in DB_URL
- POSTGRES_INITDB_ARGS - using here for locale (needed for correct search in books), if books will be in english use
POSTGRES_INITDB_ARGS="--locale=en_US"
, and change "russian" to "english" in /app/db/crud -> search_in_book -> query_func + func.ts_headline inside "if results". - REDIS_URL - default url to get access to Redis from FastAPI
- Go to 'infra' dir and start container:
cd infra/ && docker-compose up -d
- Server started! It's available on
localhost:8000/{endpoint}/
- OpenAPI docs are on
localhost:8000/docs/
and onlocalhost:8000/redoc/
- Add your first book! The easiest way to do it is using redoc. Go to
localhost:8000/redoc/
, then "Authorize" (use credentials from your .env file), then "Try it out" on "POST /books/", attach your .epub book and "Execute".
-
- Get all books ->
GET /books/
- Get books with pagination ->
GET /books/?size={desired size}&page={desired page}
- Get chapters of book ->
GET /books/{book_id}/
- Get text of chapter ->
GET /books/{book_id}/chapter/{chapter_number}/
- Search chapters in book containing query ->
GET /books/{book_id}/search/
+body = {"query": "your query"}
- Get all authors ->
GET /authors/
- Get authors with pagination ->
GET /authors/?size={desired size}&page={desired page}
- Get books of author ->
GET /authors/{author_id}/
- Get all books ->
-
- If you're using something without auto encoding of credentials, you should get credentials encoded in base64 to use in HTTP Basic auth.
- To get encoded credentials, you can use "encode_bs64.py" from "core". Just add ADMIN_USER and ADMIN_PASSWORD in .env (described above) and run this file
python app/core/encode_bs64.py
(also need to install dotenvpython -m pip install python-dotenv
or use "encode_credentials" function separately. - Add header "Authorization" with value
Basic {your encoded in base64 credentials}
.
-
- Project using custom cache key builder (needed to cache paginated results correctly) -> app/core/cache.py "custom_key_builder". If you're not going to use pagination, just delete "key_builder=..." from app/main.py -> "lifespan" function -> FastAPICache.init.
- Project also using custom cache coder (needed to cache results made with pydantic models ORM correctly).
- Lifetime. I think that permanent cache meets the requirements of this project. Redis cache flushing on FastAPI shutdown and after adding every new book in DB. If you want to use cache with expire time, just add
expire={time in seconds}
to every "cache()" decorator in app/main.py.
-
- Pagination turned on in two endpoints:
GET /authors/
andGET /books/
. If you want to turn it off, changeresponse_model=CustomPage[AuthorInfo]
in app/main.py -> "author_list" function -> "@app.get" decorator toresponse_model=AuthorInfo
. Same with app/main.py -> "book_list" function -> "@app.get" decorator.
- Pagination turned on in two endpoints:
If you have any troubles or just want to improve something, feel free to open issues or PRs.
Do not hesitate to contact me 👉 Telegram 👈