Update code in endpoints/open.post.py
This commit is contained in:
parent
a760476616
commit
57a142fb3d
34
endpoints/open.post.py
Normal file
34
endpoints/open.post.py
Normal file
@ -0,0 +1,34 @@
|
||||
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.
|
Loading…
x
Reference in New Issue
Block a user