Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Library API

A FastAPI-based REST service to manage library books using in-memory storage.


Setup Instructions

Prerequisites

Ensure you have Python 3.9+ installed on your system.

Create Virtual Environement

python -m venv venv
# In cmd.exe
venv\Scripts\activate.bat
# In PowerShell
venv\Scripts\Activate.ps1

Installation & Running the API

pip install -r requirement.txt
uvicorn main:app --reload

The API will start at http://127.0.0.1:8000

Access the Swagger UI

Open your browser and visit:

http://127.0.0.1:8000/docs

This interactive Swagger UI allows you to test all API endpoints directly.


The Logic (How I Thought)

Why FastAPI?

  1. High Performance: FastAPI is built on Starlette and async Python, delivering excellent performance for REST APIs
  2. Automatic API Documentation: Swagger UI is auto-generated without extra effort
  3. Built-in Validation: Pydantic models handle request/response validation automatically
  4. Type Hints: Python type hints make code readable and enable IDE support
  5. Industry Standard: FastAPI is production-ready and widely adopted for backend APIs

Why In-Memory Dictionary?

  1. Simplicity: Python dictionaries provide O(1) lookup by ID, perfect for this use case
  2. No External Dependencies: No need for databases or file systems
  3. Fast Operations: All CRUD operations are instantaneous in-memory
  4. Clear Implementation: Makes code logic transparent and easy to understand
  5. Perfect for Prototyping: Ideal for demonstrating API functionality

How API Routes are Structured

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

Hardest Bug Faced

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_db check 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

Output Screenshots

API Output


Future Improvements

  1. Persistent Database Integration

    • Replace in-memory storage with PostgreSQL/MongoDB for data persistence across sessions
    • Implement SQLAlchemy ORM for better database abstraction
  2. 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
  3. Update Book Endpoint

    • Add PUT /books/{id} to update existing book details
    • Support partial updates with PATCH /books/{id}
    • Validate changes before updating
  4. Pagination & Advanced Filtering

    • Implement offset/limit pagination for large book lists
    • Add filters by author, title, or year range
    • Support sorting by different fields

How to Test

  1. Start the API: uvicorn main:app --reload
  2. Open Swagger UI: http://127.0.0.1:8000/docs
  3. Use the interactive interface to test each endpoint:
    • Add books with different IDs
    • Retrieve books by ID
    • Search books by publication year
    • Delete books
  4. All responses are displayed with their status codes and JSON format

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages