Update code in endpoints/tenants.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 14:29:45 +01:00
parent fb75fc4ee9
commit b9c8bc184a

View File

@ -3,8 +3,16 @@ from fastapi import APIRouter, HTTPException
router = APIRouter()
towns = [
"Memphis", "Nashville", "Knoxville", "Chattanooga", "Clarksville",
"Murfreesboro", "Franklin", "Jackson", "Johnson City", "Bartlett"
"Memphis",
"Nashville",
"Knoxville",
"Chattanooga",
"Clarksville",
"Murfreesboro",
"Franklin",
"Jackson",
"Johnson City",
"Bartlett"
]
@router.post("/tenants")
@ -18,12 +26,3 @@ async def get_tennessee_towns():
"_verb": "post",
"towns": towns
}
```
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.
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.