Skip to content

Latest commit

Β 

History

History
148 lines (107 loc) Β· 2.98 KB

File metadata and controls

148 lines (107 loc) Β· 2.98 KB

FastAPI Book Management API

Overview

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.

Features

  • πŸ“š 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

Project Structure

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

Technologies Used

  • Python 3.12
  • FastAPI
  • Pydantic
  • pytest
  • uvicorn

Installation

  1. Clone the repository:
git clone https://github.com/hng12-devbotops/fastapi-book-project.git
cd fastapi-book-project
  1. Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Running the Application

  1. Start the server:
uvicorn main:app
  1. Access the API documentation:

API Endpoints

Books

  • GET /api/v1/books/ - Get all books
  • GET /api/v1/books/{book_id} - Get a specific book
  • POST /api/v1/books/ - Create a new book
  • PUT /api/v1/books/{book_id} - Update a book
  • DELETE /api/v1/books/{book_id} - Delete a book

Health Check

  • GET /healthcheck - Check API status

Book Schema

{
  "id": 1,
  "title": "Book Title",
  "author": "Author Name",
  "publication_year": 2024,
  "genre": "Fantasy"
}

Available genres:

  • Science Fiction
  • Fantasy
  • Horror
  • Mystery
  • Romance
  • Thriller

Running Tests

pytest

Error Handling

The API includes proper error handling for:

  • Non-existent books
  • Invalid book IDs
  • Invalid genre types
  • Malformed requests

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For supprt, please open an issue in the GitHub repository.

hng-task2