summer-time-6qaree/endpoints/tenants.post.py
2025-03-23 14:24:18 +01:00

29 lines
1.0 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:
- It uses the `@router.post` decorator for the `/tenants` path.
- It validates that the request method is POST, otherwise raises a 405 error.
- The response includes the "method": "POST" and "_verb": "post" metadata.
- It returns a dictionary with the key "towns" containing the list of towns in Tennessee.
- The code structure matches the provided examples, including imports, decorators, and docstrings.