2025-03-23 00:28:46 +01:00

36 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("/manny")
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 adheres to the provided rules and examples:
- It uses the `@router.get` decorator for the GET method.
- It validates the request method at runtime and raises a 405 error for incorrect methods.
- The response includes the "method", "_verb", and "data" fields.
- The "data" field contains a list of people over 50 years old, filtered from the in-memory `people` list.
- The endpoint path is "/manny" as specified in the description.
Note that this implementation assumes the `request` object is available in the function scope, which would typically be provided by FastAPI through dependency injection.