33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
biros = [] # In-memory storage
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/biro")
|
|
async def create_biro(biro: BiroSchema):
|
|
"""Create a new biro"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
biro_dict = biro.dict()
|
|
biros.append(biro_dict)
|
|
|
|
return {
|
|
"message": "Biro created successfully",
|
|
"biro": biro_dict,
|
|
"_verb": "post",
|
|
"method": "POST"
|
|
}
|
|
```
|
|
|
|
This code defines a POST endpoint `/biro` that creates a new biro object and stores it in an in-memory list called `biros`. It includes:
|
|
|
|
1. A `BiroSchema` Pydantic model to validate the request body.
|
|
2. The `/biro` endpoint function `create_biro` that takes a `BiroSchema` instance as input.
|
|
3. Validation to ensure the request method is POST, raising a 405 error otherwise.
|
|
4. Appending the new biro dictionary to the `biros` list.
|
|
5. Returning a response with the created biro, a success message, and metadata about the request method.
|
|
|
|
The response format matches the provided examples, including the `_verb` and `method` fields. |