pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
939 B
44 lines
939 B
from typing import List
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.routing import APIRoute
|
|
from pydantic import BaseModel
|
|
|
|
|
|
def custom_generate_unique_id(route: APIRoute):
|
|
return f"{route.tags[0]}-{route.name}"
|
|
|
|
|
|
app = FastAPI(generate_unique_id_function=custom_generate_unique_id)
|
|
|
|
|
|
class Item(BaseModel):
|
|
name: str
|
|
price: float
|
|
|
|
|
|
class ResponseMessage(BaseModel):
|
|
message: str
|
|
|
|
|
|
class User(BaseModel):
|
|
username: str
|
|
email: str
|
|
|
|
|
|
@app.post("/items/", response_model=ResponseMessage, tags=["items"])
|
|
async def create_item(item: Item):
|
|
return {"message": "Item received"}
|
|
|
|
|
|
@app.get("/items/", response_model=List[Item], tags=["items"])
|
|
async def get_items():
|
|
return [
|
|
{"name": "Plumbus", "price": 3},
|
|
{"name": "Portal Gun", "price": 9001},
|
|
]
|
|
|
|
|
|
@app.post("/users/", response_model=ResponseMessage, tags=["users"])
|
|
async def create_user(user: User):
|
|
return {"message": "User received"}
|
|
|