This is a FastAPI-based RESTful API that replicates the LibraryService functionality from the Java Spring Boot project.
- RESTful API for managing books
- In-memory storage (same as Java version)
- Filtering and pagination support
- Automatic OpenAPI/Swagger documentation
- Demo data seeding on startup
GET /api/v1/books— List all books with optional filtering and pagination- Query parameters:
author,genre,dewey,page(0-based),size
- Query parameters:
GET /api/v1/books/{id}— Get a single book by IDPOST /api/v1/books— Create a new bookPUT /api/v1/books/{id}— Update an existing bookDELETE /api/v1/books/{id}— Delete a book
The project includes comprehensive API tests using pytest.
pip install -r requirements.txtpython main.py# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test
pytest test_api.py::test_list_books
# Run tests with coverage
pytest --cov=.
# Generate HTML test report
pytest --html=report.html-
Install dependencies:
pip install -r requirements.txt
-
Run the server:
python main.py
-
Open your browser to:
- Interactive API docs:
http://localhost:8000/docs - API info:
http://localhost:8000/api - OpenAPI spec:
http://localhost:8000/openapi.json
- Interactive API docs:
The application seeds 11 demo books on startup with IDs starting from 33333333-3333-3333-3333-333333333333.
Example curl commands:
Create a book:
curl -X POST -H "Content-Type: application/json" \
-d '{"title":"My Book","author":"You","isbn":"ISBN-1","pages":320,"synopsis":"Short blurb"}' \
http://localhost:8000/api/v1/booksGet a seeded book:
curl -v "http://localhost:8000/api/v1/books/33333333-3333-3333-3333-333333333333"curl -v "http://localhost:8000/api/v1/books/33333333-3333-3333-3333-333333333333"