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.
34 lines
805 B
34 lines
805 B
from typing import List
|
|
|
|
import yaml
|
|
from fastapi import FastAPI, HTTPException, Request
|
|
from pydantic import BaseModel, ValidationError
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Item(BaseModel):
|
|
name: str
|
|
tags: List[str]
|
|
|
|
|
|
@app.post(
|
|
"/items/",
|
|
openapi_extra={
|
|
"requestBody": {
|
|
"content": {"application/x-yaml": {"schema": Item.model_json_schema()}},
|
|
"required": True,
|
|
},
|
|
},
|
|
)
|
|
async def create_item(request: Request):
|
|
raw_body = await request.body()
|
|
try:
|
|
data = yaml.safe_load(raw_body)
|
|
except yaml.YAMLError:
|
|
raise HTTPException(status_code=422, detail="Invalid YAML")
|
|
try:
|
|
item = Item.model_validate(data)
|
|
except ValidationError as e:
|
|
raise HTTPException(status_code=422, detail=e.errors())
|
|
return item
|
|
|