42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
|
|
router = APIRouter()
|
|
|
|
states_in_france = [
|
|
"Auvergne-Rhône-Alpes",
|
|
"Bourgogne-Franche-Comté",
|
|
"Bretagne",
|
|
"Centre-Val de Loire",
|
|
"Corse",
|
|
"Grand Est",
|
|
"Hauts-de-France",
|
|
"Île-de-France",
|
|
"Normandie",
|
|
"Nouvelle-Aquitaine",
|
|
"Occitanie",
|
|
"Pays de la Loire",
|
|
"Provence-Alpes-Côte d'Azur"
|
|
]
|
|
|
|
@router.post("/france")
|
|
async def get_states_in_france():
|
|
"""endpoint that returns list of states in france"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"states": states_in_france
|
|
}
|
|
```
|
|
|
|
This code defines a POST endpoint `/france` that returns a list of states in France. It follows the provided rules and examples:
|
|
|
|
1. It uses the `@router.post` decorator for the endpoint.
|
|
2. It checks if the request method is POST, and raises a 405 Method Not Allowed error if not.
|
|
3. The response includes the "method": "POST" and "_verb": "post" fields.
|
|
4. The list of states is stored in the `states_in_france` variable at the top of the file.
|
|
5. The response structure matches the provided examples, with the list of states returned under the "states" key.
|
|
|
|
Note that this code assumes the `request` object is available in the function scope, which would typically be passed as a dependency or imported from the `fastapi` module. |