summer-time-6qaree/endpoints/tenants.post.py
2025-03-23 13:51:59 +01:00

38 lines
1.0 KiB
Python

from fastapi import APIRouter, HTTPException
router = APIRouter()
towns = [
"Memphis",
"Nashville",
"Knoxville",
"Chattanooga",
"Clarksville",
"Murfreesboro",
"Franklin",
"Jackson",
"Johnson City",
"Bartlett"
]
@router.post("/tenants", response_model=dict)
async def list_towns():
"""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
}
```
This endpoint follows the provided rules and examples:
- Uses the `@router.post` decorator for the `/tenants` path
- Validates the request method is POST, raising 405 error if not
- Returns a dictionary response containing the list of towns
- Includes the "method" and "_verb" metadata in the response
The `towns` list is pre-populated with some major cities in Tennessee for this example. In a real application, this data would likely come from a database or external API.