This is an example CRUD (Create, Read, Update, Delete) book application built with FastAPI and Pydantic. It allows you to manage books via a simple to use API. This application also includes test coverage using Pytest.
- Create Book: Add a new book to the collection.
- Read Books: Retrieve a list of all books or a specific book by its ID.
- Update Book: Modify details of an existing book.
- Delete Book: Remove a book from the collection.
- FastAPI
- Pydantic
- Uvicorn (for running the server)
pip install -r requirements.txt
uvicorn main:app --reload
POST /books/Add a new book. Requires a JSON body withtitle,author, andyear.GET /books/Retrieve all books. Optional query parameterlimitto limit the number of books returned.GET /books/{book_id}Retrieve a book by its ID.PUT /books/{book_id}Update a book by its ID. Requires a JSON body with updatedtitle,author, andyear.DELETE /books/{book_id}Delete a book by its ID.
title: stringauthor: stringyear: integer
- Returns a 404 error if a book with the specified ID is not found.
curl -X 'POST' \
'http://127.0.0.1:8000/books/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"year": 1925
}'
curl -X 'GET' \
'http://127.0.0.1:8000/books/' \
-H 'accept: application/json'
With a limit
curl -X 'GET' \
'http://127.0.0.1:8000/books/?limit=2' \
-H 'accept: application/json'
curl -X 'GET' \
'http://127.0.0.1:8000/books/{book_id}' \
-H 'accept: application/json'
curl -X 'PUT' \
'http://127.0.0.1:8000/books/{book_id}' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"title": "New Title",
"author": "New Author",
"year": 1926
}'
curl -X 'DELETE' \
'http://127.0.0.1:8000/books/{book_id}' \
-H 'accept: application/json'
