Update code in endpoints/france.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 09:56:19 +01:00
parent 384e38c671
commit f2282a3768

View File

@ -1,8 +1,11 @@
Here's the FastAPI endpoint that returns a list of states in France:
```python
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
router = APIRouter() router = APIRouter()
states_in_france = [ states = [
"Auvergne-Rhône-Alpes", "Auvergne-Rhône-Alpes",
"Bourgogne-Franche-Comté", "Bourgogne-Franche-Comté",
"Bretagne", "Bretagne",
@ -15,26 +18,26 @@ states_in_france = [
"Nouvelle-Aquitaine", "Nouvelle-Aquitaine",
"Occitanie", "Occitanie",
"Pays de la Loire", "Pays de la Loire",
"Provence-Alpes-Côte d'Azur" "Provence-Alpes-Côte d'Azur",
] ]
@router.post("/france") @router.post("/france")
async def get_states_in_france(): async def get_states_france(request):
"""endpoint that returns list of states in france"""
if request.method != "POST": if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed") raise HTTPException(status_code=405, detail="Method Not Allowed")
return { return {
"method": "POST", "method": "POST",
"_verb": "post", "_verb": "post",
"states": states_in_france "states": states
} }
``` ```
This endpoint follows the provided rules and examples: This endpoint follows the provided rules:
- It uses the `@router.post` decorator for the `/france` path. 1. It uses the `@router.post` decorator for the `/france` path.
- It validates that the request method is POST, otherwise raises a 405 Method Not Allowed error. 2. It validates that the request method is POST, raising a 405 Method Not Allowed error otherwise.
- The response includes the "method": "POST" and "_verb": "post" fields. 3. The response includes the "method": "POST" and "_verb": "post" fields.
- The response contains a list of states in France under the "states" key. 4. The response contains a list of states in France under the "states" key.
- The code structure, imports, and naming conventions match the examples.
Note: The list of states is hardcoded in the `states` variable at the top of the file. In a real application, this data would likely come from a database or external API.