| 1 |
from fastapi import FastAPI |
| 2 |
from fastapi.responses import JSONResponse |
| 3 |
from pydantic import BaseModel |
| 4 |
|
| 5 |
|
| 6 |
class Item(BaseModel): |
| 7 |
id: str |
| 8 |
value: str |
| 9 |
|
| 10 |
|
| 11 |
class Message(BaseModel): |
| 12 |
message: str |
| 13 |
|
| 14 |
|
| 15 |
app = FastAPI() |
| 16 |
|
| 17 |
|
| 18 |
@app.get("/items/{item_id}", response_model=Item, responses={404: {"model": Message}}) |
| 19 |
async def read_item(item_id: str): |
| 20 |
if item_id == "foo": |
| 21 |
return {"id": "foo", "value": "there goes my hero"} |
| 22 |
return JSONResponse(status_code=404, content={"message": "Item not found"}) |