This project is a RESTful API built with FastAPI for managing a book collection. It provides comprehensive CRUD (Create, Read, Update, Delete) operations for books with proper error handling, input validation, and documentation.
- 📚 Book management (CRUD operations)
- ✅ Input validation using Pydantic models
- 🔍 Enum-based genre classification
- 🧪 Complete test coverage
- 📝 API documentation (auto-generated by FastAPI)
- 🔒 CORS middleware enabled
fastapi-book-project/ ├── api/ │ ├── db/ │ │ ├── __init__.py │ │ └── schemas.py # Data models and in-memory database │ ├── routes/ │ │ ├── __init__.py │ │ └── books.py # Book route handlers │ └── router.py # API router configuration ├── core/ │ ├── __init__.py │ └── config.py # Application settings ├── tests/ │ ├── __init__.py │ └── test_books.py # API endpoint tests ├── main.py # Application entry point ├── requirements.txt # Project dependencies └── README.md - Python 3.12
- FastAPI
- Pydantic
- pytest
- uvicorn
- Clone the repository:
git clone https://github.com/hng12-devbotops/fastapi-book-project.git cd fastapi-book-project- Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Start the server:
uvicorn main:app- Access the API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
GET /api/v1/books/- Get all booksGET /api/v1/books/{book_id}- Get a specific bookPOST /api/v1/books/- Create a new bookPUT /api/v1/books/{book_id}- Update a bookDELETE /api/v1/books/{book_id}- Delete a book
GET /healthcheck- Check API status
{ "id": 1, "title": "Book Title", "author": "Author Name", "publication_year": 2024, "genre": "Fantasy" }Available genres:
- Science Fiction
- Fantasy
- Horror
- Mystery
- Romance
- Thriller
pytestThe API includes proper error handling for:
- Non-existent books
- Invalid book IDs
- Invalid genre types
- Malformed requests
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support, please open an issue in the GitHub repository.