This is an advanced REST API built with Python and FastAPI, integrating with MongoDB for CRUD operations (Create, Read, Update, Delete) on books. FastAPI is a powerful web framework for building APIs, while MongoDB is a NoSQL database that provides flexibility and scalability.
HTTP Method | Endpoint | Description |
---|---|---|
GET | /books | Get all books |
GET | /books/{book_id} | Get a specific book |
POST | /books | Add a new book |
PUT | /books/{book_id} | Update a book |
DELETE | /books/{book_id} | Delete a book |
Clone this repository to your local machine:
git clone https://github.com/BaseMax/FastAPIBooks
Change into the project directory:
cd FastAPIBooks
Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate
Install the project dependencies:
pip install -r requirements.txt
Configure the .env
file with your own MongoDB credentials:
cp .env.example .env
Edit .env
with your own values:
MONGO_URI=<your-mongodb-uri>
Run the application:
uvicorn main:app --reload
The application will start and be available at http://localhost:8000.
Retrieve a list of books:
GET /books
Returns a list of all books in the system:
curl http://localhost:8000/books/ -H "Accept: application/json"
Retrieve details for a specific book:
GET /books/{book_id}
Returns details for a specific book with the given book_id:
curl http://localhost:8000/books/1 -H "Accept: application/json"
Add a new book:
POST /books
Adds a new book to the system. The request body should include a JSON object with the following properties:
title
(string, required): the title of the bookauthor
(string, required): the author of the bookdescription
(string): the description of the bookpublished_year
(integer): the published year of the bookpublisher
(string): the publisher of the book
curl -X POST http://localhost:8000/books/
-H 'Content-Type: application/json'
-d '{"title":"The Lord of the Rings", "author": "J.R.R. Tolkien", "published_year": 1954, "publisher": "George Allen & Unwin", "description": "A hobbit named Frodo Baggins and his companions set out on a quest to destroy the One Ring and defeat the dark lord Sauron."}'
Update an existing book:
PUT /books/{book_id}
Updates an existing book with the given book_id
. The request body should include a JSON object with the following properties:
title
(string): the new title for the bookauthor
(string): the new author for the bookdescription
(string): the new description for the bookpublished_year
(integer): the new published year for the bookpublisher
(string): the new publisher for the book
curl -X PUT http://localhost:8000/books/1
-H "Accept: application/json"
-d '{"title": "The Fellowship of the Ring", "author": "J.R.R. Tolkien", "published_year": 1954, "publisher": "George Allen & Unwin", "description": "Epic quest to destroy powerful Ring, facing dark forces."}'
you can run unit tests by the following command:
pytest test_bookAPI.py
And result look like this:
- Ali Ahmadi
- Max Base
Copyright 2023, Max Base