An open API service indexing awesome lists of open source software.

https://github.com/hngprojects/fastapi-book-project


https://github.com/hngprojects/fastapi-book-project

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# 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:

```bash
git clone https://github.com/hng12-devbotops/fastapi-book-project.git
cd fastapi-book-project
```

2. Create a virtual environment:

```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```

3. Install dependencies:

```bash
pip install -r requirements.txt
```

## Running the Application

1. Start the server:

```bash
uvicorn main:app
```

2. Access the API documentation:

- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc

## 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

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

Available genres:

- Science Fiction
- Fantasy
- Horror
- Mystery
- Romance
- Thriller

## Running Tests

```bash
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](LICENSE) file for details.

## Support

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