From b415d9990db4545333e1c5d12afca73617afe64a Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sun, 23 Mar 2025 00:21:56 +0100 Subject: [PATCH] Update code in endpoints/man.get.py --- endpoints/man.get.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/endpoints/man.get.py b/endpoints/man.get.py index 3c8ff5f..9331c8e 100644 --- a/endpoints/man.get.py +++ b/endpoints/man.get.py @@ -3,23 +3,33 @@ from fastapi import APIRouter, HTTPException people = [ {"name": "Alice", "age": 55, "country": "UK"}, {"name": "Bob", "age": 45, "country": "ESP"}, - {"name": "Charlie", "age": 60, "country": "Nigeria"}, - {"name": "David", "age": 52, "country": "UK"}, - {"name": "Eve", "age": 48, "country": "ESP"} + {"name": "Charlie", "age": 60, "country": "Togo"}, + {"name": "David", "age": 35, "country": "UK"}, + {"name": "Eve", "age": 52, "country": "ESP"} ] router = APIRouter() @router.get("/man") async def get_people_over_50(): - """Fetches list of people over 50 years of age""" - over_50 = [p for p in people if p["age"] > 50] - + """Endpoint to fetch 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", + "method": "GET", "_verb": "get", "data": over_50 } ``` -This endpoint defines a list of people with their name, age, and country. The `get_people_over_50` function filters the list to only include people whose age is greater than 50, and returns a dictionary with the method metadata and the filtered list of people. \ No newline at end of file +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 list of people in the `"data"` field. + +Note that the `people` list is initialized with some sample data for demonstration purposes. In a real application, this data would likely come from a database or other external source. \ No newline at end of file