34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
Here's a possible implementation of a FastAPI endpoint that returns a list of towns in Arizona:
|
|
|
|
```python
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
router = APIRouter()
|
|
|
|
arizona_towns = [
|
|
"Phoenix", "Tucson", "Mesa", "Chandler", "Glendale",
|
|
"Scottsdale", "Gilbert", "Tempe", "Peoria", "Surprise"
|
|
]
|
|
|
|
@router.post("/open", response_model=dict)
|
|
async def list_arizona_towns(request: dict):
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"towns": arizona_towns
|
|
}
|
|
```
|
|
|
|
This endpoint adheres to the following rules:
|
|
|
|
1. It uses the `@router.post` decorator for the `/open` endpoint path.
|
|
2. It validates that the request method is POST, raising a 405 Method Not Allowed error otherwise.
|
|
3. It returns a dictionary response with the following structure:
|
|
- `"method": "POST"` (the HTTP method used)
|
|
- `"_verb": "post"` (metadata about the HTTP verb)
|
|
- `"towns": arizona_towns` (a list of town names in Arizona)
|
|
|
|
The `arizona_towns` list is defined at the top of the file as a Python list containing town names. |