35 lines
1018 B
Python
35 lines
1018 B
Python
from fastapi import APIRouter, HTTPException
|
|
|
|
towns = [
|
|
"Douala",
|
|
"Yaoundé",
|
|
"Garoua",
|
|
"Bamenda",
|
|
"Bafoussam",
|
|
"Nkongsamba",
|
|
"Maroua",
|
|
"Ngaoundéré",
|
|
"Kumba",
|
|
"Edéa"
|
|
]
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/open")
|
|
async def get_towns():
|
|
"""Returns list of towns in Cameroun"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"towns": towns
|
|
}
|
|
```
|
|
|
|
This code defines a list of towns in Cameroun, then creates a FastAPI router with a single POST endpoint at `/open`. When this endpoint is called with a POST request, it returns the list of towns along with the request method metadata.
|
|
|
|
If the request is made with any HTTP method other than POST, it will raise a 405 Method Not Allowed error.
|
|
|
|
The response format adheres strictly to the provided examples, including the `method`, `_verb`, and nested data structure under the `towns` key. |