From 2392849416118addbc133c5a0f8d7043e8d63591 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sun, 23 Mar 2025 00:12:31 +0100 Subject: [PATCH] Update code in endpoints/human.get.py --- endpoints/human.get.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/endpoints/human.get.py b/endpoints/human.get.py index ab02845..69b4a95 100644 --- a/endpoints/human.get.py +++ b/endpoints/human.get.py @@ -2,24 +2,29 @@ from fastapi import APIRouter, HTTPException people = [ {"name": "Alice", "age": 55, "country": "UK"}, - {"name": "Bob", "age": 45, "country": "USA"}, - {"name": "Charlie", "age": 60, "country": "ESP"}, + {"name": "Bob", "age": 45, "country": "ESP"}, + {"name": "Charlie", "age": 60, "country": "Nigeria"}, {"name": "David", "age": 35, "country": "UK"}, - {"name": "Eve", "age": 52, "country": "USA"} + {"name": "Eve", "age": 52, "country": "ESP"} ] router = APIRouter() @router.get("/human") -async def get_older_people(): +async def get_people_over_50(): """Fetch list of people over 50 years of age""" - older_people = [p for p in people if p["age"] > 50] - + 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": older_people + "data": over_50 } ``` -This endpoint defines a list of people with their name, age, and country. The `get_older_people` function filters this list to only include people whose age is over 50 years old. The response includes the filtered list of older people, along with the method metadata as per the requirements. \ No newline at end of file +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. \ No newline at end of file