Update code in endpoints/human.get.py
This commit is contained in:
parent
fdb2f9dca6
commit
b7a0a503c5
34
endpoints/human.get.py
Normal file
34
endpoints/human.get.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
|
people = [
|
||||||
|
{"name": "Alice", "age": 55, "country": "UK"},
|
||||||
|
{"name": "Bob", "age": 45, "country": "USA"},
|
||||||
|
{"name": "Charlie", "age": 60, "country": "UK"},
|
||||||
|
{"name": "David", "age": 52, "country": "USA"}
|
||||||
|
]
|
||||||
|
|
||||||
|
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 follows the provided rules and examples:
|
||||||
|
|
||||||
|
1. It uses the `@router.get` decorator for the `GET` method.
|
||||||
|
2. It validates the request method and raises a 405 error if not `GET`.
|
||||||
|
3. It filters the `people` list to include only those over 50 years old.
|
||||||
|
4. The response includes the required `"method"` and `"_verb"` fields, along with the filtered `"data"` list.
|
||||||
|
|
||||||
|
Note that this implementation assumes the `people` list is already defined and populated with dictionaries containing `"name"`, `"age"`, and `"country"` keys.
|
Loading…
x
Reference in New Issue
Block a user