2022-03-09 21:07:59 +08:00

29 lines
564 B
Python

from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI()
class Item(BaseModel):
id: Optional[int] = None
name: str = Field(description="The name of the item", regex="^[a-zA-Z0-9]*$")
price: float
is_offer: Optional[bool] = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}
@app.post("/items")
def create_item(item: Item) -> Item:
return item