17 changed files with 16 additions and 117 deletions
@ -1,30 +0,0 @@ |
|||||
from typing import Set, Union |
|
||||
|
|
||||
from fastapi import FastAPI |
|
||||
from pydantic import BaseModel |
|
||||
|
|
||||
app = FastAPI() |
|
||||
|
|
||||
|
|
||||
class Item(BaseModel): |
|
||||
name: str |
|
||||
description: Union[str, None] = None |
|
||||
price: float |
|
||||
tax: Union[float, None] = None |
|
||||
tags: Set[str] = set() |
|
||||
|
|
||||
|
|
||||
@app.post("/items/", response_model=Item, summary="Create an item") |
|
||||
async def create_item(item: Item): |
|
||||
""" |
|
||||
Create an item with all the information: |
|
||||
|
|
||||
- **name**: each item must have a name |
|
||||
- **description**: a long description |
|
||||
- **price**: required |
|
||||
- **tax**: if the item doesn't have tax, you can omit this |
|
||||
- **tags**: a set of unique tag strings for this item |
|
||||
\f |
|
||||
:param item: User input. |
|
||||
""" |
|
||||
return item |
|
||||
@ -1,34 +0,0 @@ |
|||||
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(include_url=False)) |
|
||||
return item |
|
||||
@ -1,34 +0,0 @@ |
|||||
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.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.parse_obj(data) |
|
||||
except ValidationError as e: |
|
||||
raise HTTPException(status_code=422, detail=e.errors()) |
|
||||
return item |
|
||||
Loading…
Reference in new issue