-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastapi.py
More file actions
122 lines (98 loc) · 3.71 KB
/
Copy pathfastapi.py
File metadata and controls
122 lines (98 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
FastAPI REST API example
File: fastapi_rest_api_example.py
This is a minimal, production-oriented example of a REST API using FastAPI.
It demonstrates:
- Pydantic models for request/response validation
- Async endpoints
- CRUD operations (Create, Read, Update, Delete)
- Error handling with HTTPException
- Simple in-memory "database" (replaceable with a real DB)
- CORS middleware example
Dependencies (install with pip):
pip install fastapi uvicorn[standard] python-multipart
Run the server:
uvicorn fastapi_rest_api_example:app --reload --port 8000
Sample requests (using curl):
# Create an item
curl -X POST "http://127.0.0.1:8000/items" -H "Content-Type: application/json" \
-d '{"name": "Example", "description": "An example item", "price": 9.99}'
# List items
curl "http://127.0.0.1:8000/items"
# Get item by id
curl "http://127.0.0.1:8000/items/1"
# Update item
curl -X PUT "http://127.0.0.1:8000/items/1" -H "Content-Type: application/json" \
-d '{"name": "Updated", "description": "Updated desc", "price": 12.5}'
# Delete item
curl -X DELETE "http://127.0.0.1:8000/items/1"
Notes:
- For a production deployment, use a proper database (Postgres, MySQL, SQLite with SQLAlchemy/SQLModel),
add authentication/authorization (OAuth2 / JWT), schema migrations, logging, and observability.
- This example intentionally keeps persistence in-memory for clarity.
"""
from typing import List, Optional
from fastapi import FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from uuid import UUID, uuid4
app = FastAPI(title="Example REST API", version="1.0.0")
# Allow CORS for local dev. Adjust origins for production.
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://127.0.0.1:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Pydantic models
class ItemCreate(BaseModel):
name: str = Field(..., min_length=1, max_length=100)
description: Optional[str] = Field(None, max_length=500)
price: float = Field(..., gt=0)
class Item(ItemCreate):
id: UUID
# "In-memory database". Keys are UUIDs stored as strings for simple JSON-friendly output.
_db: dict[str, Item] = {}
# Helper functions
def get_item_or_404(item_id: str) -> Item:
item = _db.get(item_id)
if not item:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found")
return item
# CRUD endpoints
@app.post("/items", response_model=Item, status_code=status.HTTP_201_CREATED)
async def create_item(payload: ItemCreate):
item_id = str(uuid4())
item = Item(id=UUID(item_id), **payload.dict())
# store as string key
_db[item_id] = item
return item
@app.get("/items", response_model=List[Item])
async def list_items(limit: int = 100, offset: int = 0):
# simple pagination
items = list(_db.values())
return items[offset : offset + limit]
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
item = get_item_or_404(item_id)
return item
@app.put("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, payload: ItemCreate):
existing = get_item_or_404(item_id)
updated = Item(id=existing.id, **payload.dict())
_db[item_id] = updated
return updated
@app.delete("/items/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_item(item_id: str):
# raises 404 if not found
_ = get_item_or_404(item_id)
del _db[item_id]
return None
# Health check and basic metadata
@app.get("/health")
async def health():
return {"status": "ok"}
@app.get("/")
async def root():
return {"service": "example-rest-api", "version": "1.0.0"}