# Books API
A FastAPI-based REST API for accessing and querying a books database. This API provides endpoints to search, filter, and retrieve information about books, authors, languages, and subjects.
## Features
- RESTful API endpoints
- Filtering capabilities (by author, language, subject, etc.)
- Pagination support (25 items per page)
- Sorting by download count
- Swagger UI documentation
- MySQL database integration
## Tech Stack
- **FastAPI**: Modern web framework for building APIs
- **SQLAlchemy**: SQL toolkit and ORM
- **MySQL**: Database
- **Pydantic**: Data validation using Python type annotations
- **Uvicorn**: ASGI server implementation
## Installation
1. Clone the repository:
```bash
git clone <repository-url>
cd books-api- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set up environment variables in
.env:
DATABASE_URL=mysql+mysqlconnector://user:password@localhost/books_db- Create MySQL database:
CREATE DATABASE books_db;- Import database dump:
mysql -u root -p books_db < ignite/gutendex.sql- Start the server:
uvicorn app.main:app --reload- Access the API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
-
GET /books/: List all books with pagination- Query Parameters:
page: Page number (default: 1)limit: Items per page (default: 25)title: Filter by titleauthor: Filter by author namelanguage: Filter by language codesubject: Filter by subjectsort_by_downloads: Sort by download count
- Query Parameters:
-
GET /books/{book_id}: Get specific book details
GET /authors/: List all authors- Query Parameters:
page: Page numberlimit: Items per pagename: Filter by author name
- Query Parameters:
GET /languages/: List all available languages
GET /subjects/: List all subjects- Query Parameters:
page: Page numberlimit: Items per pagename: Filter by subject name
- Query Parameters:
import requests
# Get first page of books
response = requests.get("http://localhost:8000/books/")
# Get books by Shakespeare
response = requests.get("http://localhost:8000/books/?author=Shakespeare")
# Get English books about Fiction
response = requests.get("http://localhost:8000/books/?language=en&subject=Fiction")books-api/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application and endpoints
│ ├── database.py # Database connection and session
│ ├── models.py # SQLAlchemy models
│ └── schemas.py # Pydantic models
├── .env # Environment variables
└── requirements.txt # Project dependencies
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.