Update code in endpoints/tenants.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 14:25:12 +01:00
parent ddccf1cfb7
commit fb75fc4ee9

View File

@ -2,13 +2,13 @@ from fastapi import APIRouter, HTTPException
router = APIRouter()
towns_in_tennessee = [
"Nashville", "Memphis", "Knoxville", "Chattanooga", "Clarksville",
towns = [
"Memphis", "Nashville", "Knoxville", "Chattanooga", "Clarksville",
"Murfreesboro", "Franklin", "Jackson", "Johnson City", "Bartlett"
]
@router.post("/tenants", response_model=dict)
async def get_towns_in_tennessee():
@router.post("/tenants")
async def get_tennessee_towns():
"""Returns list of towns in Tennessee"""
if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed")
@ -16,14 +16,14 @@ async def get_towns_in_tennessee():
return {
"method": "POST",
"_verb": "post",
"towns": towns_in_tennessee
"towns": towns
}
```
This endpoint adheres to the provided rules and examples:
This code defines a FastAPI endpoint `/tenants` that accepts POST requests and returns a list of towns in Tennessee. The list of towns is stored in the `towns` variable at the top of the file.
- 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.
The endpoint function `get_tennessee_towns` first checks if the request method is POST using `if request.method != "POST"`. If the method is not POST, it raises an HTTPException with a 405 Method Not Allowed status code.
If the method is POST, it returns a dictionary with three keys: `"method"` (the HTTP method used), `"_verb"` (a metadata field indicating the verb is "post"), and `"towns"` (the list of towns in Tennessee).
Note that this code follows the provided rules and examples, including strict method adherence, response format, error handling, data storage, and code structure.