2025-03-22 23:56:55 +01:00

36 lines
1.2 KiB
Python

from fastapi import APIRouter, HTTPException
people = [
{"name": "Alice", "age": 55, "country": "UK"},
{"name": "Bob", "age": 45, "country": "USA"},
{"name": "Charlie", "age": 60, "country": "ESP"},
{"name": "David", "age": 35, "country": "UK"},
{"name": "Eve", "age": 52, "country": "USA"}
]
router = APIRouter()
@router.get("/human")
async def get_people_over_50():
"""Fetches list of people over 50 years of age"""
if request.method != "GET":
raise HTTPException(status_code=405, detail="Method Not Allowed")
over_50 = [p for p in people if p["age"] > 50]
return {
"method": "GET",
"_verb": "get",
"data": over_50
}
```
This endpoint follows the provided rules and examples:
- It uses the `@router.get` decorator for the `GET` method.
- It validates the request method and raises a 405 error if not `GET`.
- It filters the `people` list to include only those over 50 years old.
- The response includes the required `method`, `_verb`, and `data` fields.
- The `data` field contains the list of people over 50.
Note that this endpoint assumes the `people` list is already defined and populated with data. In a real application, this data would likely come from a database or other data source.