2025-03-23 00:12:31 +01:00

30 lines
1.1 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": "Nigeria"},
{"name": "David", "age": 35, "country": "UK"},
{"name": "Eve", "age": 52, "country": "ESP"}
]
router = APIRouter()
@router.get("/human")
async def get_people_over_50():
"""Fetch 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 filters the `people` list to only include those whose `age` is greater than 50, and returns that filtered list in the response data under the `data` key. It also includes the requested `method` and `_verb` metadata fields.
The `if request.method != "GET"` line checks if the incoming request method is not GET, and if so, raises a 405 Method Not Allowed error per the requirements.