gbemidebe-xd4v6n/endpoints/people.get.py
2025-03-22 15:16:56 +01:00

49 lines
1.4 KiB
Python

from fastapi import APIRouter, HTTPException
people = [
{"name": "John Doe", "age": 55, "country": "UK"},
{"name": "Jane Smith", "age": 45, "country": "USA"},
{"name": "Bob Johnson", "age": 62, "country": "UK"},
{"name": "Alice Williams", "age": 51, "country": "USA"},
{"name": "Charlie Brown", "age": 48, "country": "UK"},
]
router = APIRouter()
@router.get("/people")
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 = [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:
- Uses the `@router.get` decorator for the `/people` endpoint
- Validates the request method is GET, raising 405 Method Not Allowed if not
- Filters the `people` list to include only those with `age` over 50
- Returns a response with the required structure:
- `"method": "GET"`
- `"_verb": "get"`
- `"data"` containing the filtered list of people over 50
The response data will be:
```json
{
"method": "GET",
"_verb": "get",
"data": [
{"name": "John Doe", "age": 55, "country": "UK"},
{"name": "Bob Johnson", "age": 62, "country": "UK"},
{"name": "Alice Williams", "age": 51, "country": "USA"}
]
}