Update code in endpoints/open.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 16:31:12 +01:00
parent 686d9a9ccf
commit a952b1f0c9

View File

@ -1,8 +1,10 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
router = APIRouter()
towns = [ towns = [
"Douala",
"Yaoundé", "Yaoundé",
"Douala",
"Garoua", "Garoua",
"Bamenda", "Bamenda",
"Bafoussam", "Bafoussam",
@ -10,16 +12,24 @@ towns = [
"Maroua", "Maroua",
"Ngaoundéré", "Ngaoundéré",
"Kumba", "Kumba",
"Edéa" "Édéa",
"Kribi",
"Ebolowa",
"Bertoua",
"Buea",
"Limbé",
"Dschang",
"Bafang",
"Loum",
"Mbouda",
"Foumban"
] ]
router = APIRouter()
@router.post("/open") @router.post("/open")
async def get_towns(): async def get_towns():
"""Returns list of towns in Cameroun""" """Returns list of towns in Cameroun"""
if request.method != "POST": if not towns:
raise HTTPException(status_code=405, detail="Method Not Allowed") raise HTTPException(status_code=404, detail="No towns found")
return { return {
"method": "POST", "method": "POST",
@ -28,8 +38,6 @@ async def get_towns():
} }
``` ```
This code defines a list of towns in Cameroun, then creates a FastAPI router with a single POST endpoint at `/open`. When this endpoint is called with a POST request, it returns the list of towns along with the request method metadata. This endpoint follows the provided rules and examples. It defines a list of towns in Cameroon, and when a POST request is made to `/open`, it returns the list of towns along with the required `method` and `_verb` fields in the response.
If the request is made with any HTTP method other than POST, it will raise a 405 Method Not Allowed error. If the `towns` list is empty, it raises an HTTPException with a 404 status code and an appropriate error message.
The response format adheres strictly to the provided examples, including the `method`, `_verb`, and nested data structure under the `towns` key.