| 1 |
from fastapi import FastAPI |
| 2 |
from pydantic import BaseModel |
| 3 |
|
| 4 |
app = FastAPI() |
| 5 |
|
| 6 |
|
| 7 |
class Item(BaseModel): |
| 8 |
name: str |
| 9 |
description: str | None = None |
| 10 |
price: float |
| 11 |
tax: float = 10.5 |
| 12 |
|
| 13 |
|
| 14 |
items = { |
| 15 |
"foo": {"name": "Foo", "price": 50.2}, |
| 16 |
"bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2}, |
| 17 |
"baz": { |
| 18 |
"name": "Baz", |
| 19 |
"description": "There goes my baz", |
| 20 |
"price": 50.2, |
| 21 |
"tax": 10.5, |
| 22 |
}, |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
@app.get( |
| 27 |
"/items/{item_id}/name", |
| 28 |
response_model=Item, |
| 29 |
response_model_include=["name", "description"], |
| 30 |
) |
| 31 |
async def read_item_name(item_id: str): |
| 32 |
return items[item_id] |
| 33 |
|
| 34 |
|
| 35 |
@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude=["tax"]) |
| 36 |
async def read_item_public_data(item_id: str): |
| 37 |
return items[item_id] |