2019-04-05 13:54:00 +04:00
|
|
|
from fastapi import FastAPI
|
2020-03-01 21:49:20 +01:00
|
|
|
from fastapi.responses import JSONResponse
|
2019-04-05 13:54:00 +04:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Item(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
value: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Message(BaseModel):
|
|
|
|
|
message: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/items/{item_id}", response_model=Item, responses={404: {"model": Message}})
|
|
|
|
|
async def read_item(item_id: str):
|
|
|
|
|
if item_id == "foo":
|
|
|
|
|
return {"id": "foo", "value": "there goes my hero"}
|
2022-08-26 08:32:30 -05:00
|
|
|
return JSONResponse(status_code=404, content={"message": "Item not found"})
|