Building APIs using FastAPI
- FastAPI Getting Started
- Setting up the project Refer Here
- A simple code in main.py
from fastapi import FastAPI
app = FastAPI(
title="Hello Library",
summary="Getting started with FastAPI",
version="1.0.0")
@app.get("/")
def home() -> str:
return "Welcome to Library"
# if __name__ == "__main__":
# app
- For debugging watch classroom video.
Adding Books Resource
- Overview
| Endpoint | Method | Description | Request Body | Response |
| —————– | ———- | ——————————- | ——————————————- | ———————— |
| /api/books | GET | Get list of all books | Query: ?title=java&author=gosling | 200 OK – List of books |
| /api/books/{id} | GET | Get details of a single book | — | 200 OK – Book info |
| /api/books | POST | Add a new book (Librarian only) | { title, author, isbn, category, copies } | 201 Created |
| /api/books/{id} | PUT | Update book details | { title, author, category, copies } | 200 OK |
| /api/books/{id} | DELETE | Remove book (Librarian only) | — | 204 No Content |
- Parameters in FastAPI
- Raising Exceptions and Returning Status Codes in Fast API
Models in FastAPI
- FastAPI uses Pydantic Models
- Validators in pydantic
- Refer Here for the changes done to implement pydantic models
