Skip to content

Latest commit

 

History

History

README.md

Python Library API

This is a FastAPI-based RESTful API that replicates the LibraryService functionality from the Java Spring Boot project.

Features

  • RESTful API for managing books
  • In-memory storage (same as Java version)
  • Filtering and pagination support
  • Automatic OpenAPI/Swagger documentation
  • Demo data seeding on startup

API Endpoints

  • GET /api/v1/books — List all books with optional filtering and pagination
    • Query parameters: author, genre, dewey, page (0-based), size
  • GET /api/v1/books/{id} — Get a single book by ID
  • POST /api/v1/books — Create a new book
  • PUT /api/v1/books/{id} — Update an existing book
  • DELETE /api/v1/books/{id} — Delete a book

Running Tests

The project includes comprehensive API tests using pytest.

Install test dependencies:

pip install -r requirements.txt

Start the server (in one terminal):

python main.py

Run tests (in another terminal):

# Run all tests
pytest

# Run with verbose output
pytest -v

# Run specific test
pytest test_api.py::test_list_books

# Run tests with coverage
pytest --cov=.

# Generate HTML test report
pytest --html=report.html

Running the Application

  1. Install dependencies:

    pip install -r requirements.txt
  2. Run the server:

    python main.py
  3. Open your browser to:

    • Interactive API docs: http://localhost:8000/docs
    • API info: http://localhost:8000/api
    • OpenAPI spec: http://localhost:8000/openapi.json

Demo Data

The application seeds 11 demo books on startup with IDs starting from 33333333-3333-3333-3333-333333333333.

Example curl commands:

Create a book:

curl -X POST -H "Content-Type: application/json" \
  -d '{"title":"My Book","author":"You","isbn":"ISBN-1","pages":320,"synopsis":"Short blurb"}' \
  http://localhost:8000/api/v1/books

Get a seeded book:

curl -v "http://localhost:8000/api/v1/books/33333333-3333-3333-3333-333333333333"
curl -v "http://localhost:8000/api/v1/books/33333333-3333-3333-3333-333333333333"