From 1f2c681c9ea06847dc84a2123565d30a73e3f932 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sat, 22 Mar 2025 23:45:14 +0100 Subject: [PATCH] Update code in endpoints/human.get.py --- endpoints/human.get.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/endpoints/human.get.py b/endpoints/human.get.py index 982061f..305c5de 100644 --- a/endpoints/human.get.py +++ b/endpoints/human.get.py @@ -1,17 +1,18 @@ 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"} + {"name": "Alice", "age": 55}, + {"name": "Bob", "age": 45}, + {"name": "Charlie", "age": 60}, + {"name": "David", "age": 35}, + {"name": "Eve", "age": 52} ] router = APIRouter() @router.get("/human") async def get_people_over_50(): - """Fetch list of people over 50 years of age""" + """Fetches list of people over 50 years of age""" if request.method != "GET": raise HTTPException(status_code=405, detail="Method Not Allowed") @@ -20,15 +21,8 @@ async def get_people_over_50(): return { "method": "GET", "_verb": "get", - "data": over_50 + "people_over_50": 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. \ No newline at end of file +This endpoint filters the `people` list to only include those whose age is over 50, and returns that filtered list in the response along with the expected metadata fields. It also includes a check to ensure the request method is GET, raising a 405 Method Not Allowed error if it is not. \ No newline at end of file