35 lines
1.2 KiB
Python
35 lines
1.2 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": 38, "country": "USA"},
|
|
{"name": "Charlie Brown", "age": 51, "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:
|
|
|
|
- It uses the `@router.get` decorator for the GET method.
|
|
- It checks if the request method is GET, and raises a 405 error if not.
|
|
- It filters the `people` list to include only those over 50 years of age.
|
|
- The response includes the "method", "_verb", and "data" (filtered list) fields.
|
|
|
|
Note: The `people` list is pre-populated with sample data for demonstration purposes. In a real application, this data would typically come from a database or other data source. |