| 1 |
from fastapi import FastAPI |
| 2 |
from pydantic import BaseModel |
| 3 |
|
| 4 |
app = FastAPI() |
| 5 |
|
| 6 |
|
| 7 |
class BaseItem(BaseModel): |
| 8 |
description: str |
| 9 |
type: str |
| 10 |
|
| 11 |
|
| 12 |
class CarItem(BaseItem): |
| 13 |
type: str = "car" |
| 14 |
|
| 15 |
|
| 16 |
class PlaneItem(BaseItem): |
| 17 |
type: str = "plane" |
| 18 |
size: int |
| 19 |
|
| 20 |
|
| 21 |
items = { |
| 22 |
"item1": {"description": "All my friends drive a low rider", "type": "car"}, |
| 23 |
"item2": { |
| 24 |
"description": "Music is my aeroplane, it's my aeroplane", |
| 25 |
"type": "plane", |
| 26 |
"size": 5, |
| 27 |
}, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
@app.get("/items/{item_id}", response_model=PlaneItem | CarItem) |
| 32 |
async def read_item(item_id: str): |
| 33 |
return items[item_id] |