| 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( |
| 19 |
"/items/{item_id}", |
| 20 |
response_model=Item, |
| 21 |
responses={ |
| 22 |
404: {"model": Message, "description": "The item was not found"}, |
| 23 |
200: { |
| 24 |
"description": "Item requested by ID", |
| 25 |
"content": { |
| 26 |
"application/json": { |
| 27 |
"example": {"id": "bar", "value": "The bar tenders"} |
| 28 |
} |
| 29 |
}, |
| 30 |
}, |
| 31 |
}, |
| 32 |
) |
| 33 |
async def read_item(item_id: str): |
| 34 |
if item_id == "foo": |
| 35 |
return {"id": "foo", "value": "there goes my hero"} |
| 36 |
else: |
| 37 |
return JSONResponse(status_code=404, content={"message": "Item not found"}) |