35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
|
|
people = [
|
|
{"name": "Alice", "age": 55, "country": "UK"},
|
|
{"name": "Bob", "age": 45, "country": "ESP"},
|
|
{"name": "Charlie", "age": 60, "country": "Togo"},
|
|
{"name": "David", "age": 35, "country": "UK"},
|
|
{"name": "Eve", "age": 52, "country": "ESP"}
|
|
]
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/man")
|
|
async def get_people_over_50():
|
|
"""Endpoint to fetch list of people over 50 years of age"""
|
|
if request.method != "GET":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
over_50 = [person for person in people if person["age"] > 50]
|
|
|
|
return {
|
|
"method": "GET",
|
|
"_verb": "get",
|
|
"data": over_50
|
|
}
|
|
```
|
|
|
|
This endpoint follows the provided rules and examples:
|
|
|
|
1. It uses the `@router.get` decorator for the GET method.
|
|
2. It validates the request method and raises a 405 error if not GET.
|
|
3. It filters the `people` list to include only those over 50 years old.
|
|
4. The response includes the required `"method"` and `"_verb"` fields, along with the filtered list of people in the `"data"` field.
|
|
|
|
Note that the `people` list is initialized with some sample data for demonstration purposes. In a real application, this data would likely come from a database or other external source. |