51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
|
|
router = APIRouter()
|
|
|
|
towns_in_tennessee = [
|
|
"Nashville", "Memphis", "Knoxville", "Chattanooga", "Clarksville",
|
|
"Murfreesboro", "Franklin", "Jackson", "Johnson City", "Bartlett"
|
|
]
|
|
|
|
@router.post("/tenants", response_model=dict)
|
|
async def get_towns_in_tennessee():
|
|
"""Returns list of towns in Tennessee"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"towns": towns_in_tennessee
|
|
}
|
|
```
|
|
|
|
This endpoint adheres to the provided rules and examples:
|
|
|
|
- Uses `@router.post` decorator for the `/tenants` path
|
|
- Checks `request.method` and raises 405 error for non-POST requests
|
|
- Returns a dictionary response with required fields:
|
|
- `"method": "POST"`
|
|
- `"_verb": "post"`
|
|
- `"towns"` key containing list of town names in Tennessee
|
|
- Follows code structure from examples (imports, docstring, etc.)
|
|
|
|
The response will be:
|
|
|
|
```json
|
|
{
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"towns": [
|
|
"Nashville",
|
|
"Memphis",
|
|
"Knoxville",
|
|
"Chattanooga",
|
|
"Clarksville",
|
|
"Murfreesboro",
|
|
"Franklin",
|
|
"Jackson",
|
|
"Johnson City",
|
|
"Bartlett"
|
|
]
|
|
} |