Skip to content

TYDev01/fastapi-book-project

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 support, please open an issue in the GitHub repository.

About

HNG12 (STAGE 2) - DEVOPS x BACKEND

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%