A FastAPI-based REST service to manage library books using in-memory storage.
Ensure you have Python 3.9+ installed on your system.
python -m venv venv
# In cmd.exe
venv\Scripts\activate.bat
# In PowerShell
venv\Scripts\Activate.ps1pip install -r requirement.txt
uvicorn main:app --reloadThe API will start at http://127.0.0.1:8000
Open your browser and visit:
http://127.0.0.1:8000/docs
This interactive Swagger UI allows you to test all API endpoints directly.
- High Performance: FastAPI is built on Starlette and async Python, delivering excellent performance for REST APIs
- Automatic API Documentation: Swagger UI is auto-generated without extra effort
- Built-in Validation: Pydantic models handle request/response validation automatically
- Type Hints: Python type hints make code readable and enable IDE support
- Industry Standard: FastAPI is production-ready and widely adopted for backend APIs
- Simplicity: Python dictionaries provide O(1) lookup by ID, perfect for this use case
- No External Dependencies: No need for databases or file systems
- Fast Operations: All CRUD operations are instantaneous in-memory
- Clear Implementation: Makes code logic transparent and easy to understand
- Perfect for Prototyping: Ideal for demonstrating API functionality
The API is organized around RESTful principles:
- POST /books - Creates a new book resource
- GET /books/{id} - Retrieves a specific book by ID (unique identifier)
- GET /books/search?year=2024 - Queries books by publication year
- DELETE /books/{id} - Removes a book from inventory
Each endpoint:
- Validates input using Pydantic models
- Returns appropriate HTTP status codes (201 Created, 404 Not Found, 400 Bad Request)
- Includes clear error messages for invalid operations
- Uses in-memory dictionary as the data store
Issue: Duplicate ID Handling
- Problem: Initially, adding a book with a duplicate ID would silently overwrite the existing book, causing data loss
- Root Cause: Missing validation check before insertion into the dictionary
- Solution: Added explicit
if book_id in books_dbcheck that raises an HTTPException with status code 400 (Bad Request). This ensures the API rejects duplicate IDs gracefully with a clear error message - Learning: Always validate unique constraints before mutating the data store
-
Persistent Database Integration
- Replace in-memory storage with PostgreSQL/MongoDB for data persistence across sessions
- Implement SQLAlchemy ORM for better database abstraction
-
Authentication & Authorization
- Add JWT token-based authentication for secure API access
- Implement role-based access control (admin, user, guest roles)
- Protect endpoints with login requirements
-
Update Book Endpoint
- Add PUT /books/{id} to update existing book details
- Support partial updates with PATCH /books/{id}
- Validate changes before updating
-
Pagination & Advanced Filtering
- Implement offset/limit pagination for large book lists
- Add filters by author, title, or year range
- Support sorting by different fields
- Start the API:
uvicorn main:app --reload - Open Swagger UI:
http://127.0.0.1:8000/docs - Use the interactive interface to test each endpoint:
- Add books with different IDs
- Retrieve books by ID
- Search books by publication year
- Delete books
- All responses are displayed with their status codes and JSON format
