From 7fa49278c3988b8f1ced6ebbd23e5c04520d7949 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sat, 22 Mar 2025 14:10:19 +0100 Subject: [PATCH] Update code in endpoints/people.get.py --- endpoints/people.get.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 endpoints/people.get.py diff --git a/endpoints/people.get.py b/endpoints/people.get.py new file mode 100644 index 0000000..2b967e8 --- /dev/null +++ b/endpoints/people.get.py @@ -0,0 +1,35 @@ +from fastapi import APIRouter, HTTPException + +people = [ + {"name": "Alice", "age": 55}, + {"name": "Bob", "age": 45}, + {"name": "Charlie", "age": 60}, + {"name": "David", "age": 35}, + {"name": "Eve", "age": 52} +] + +router = APIRouter() + +@router.get("/people") +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 adheres to the provided rules and examples: + +1. It uses the `@router.get` decorator for the `/people` endpoint. +2. It validates the request method and raises a 405 error if not GET. +3. It filters the `people` list to only include those over 50 years old. +4. The response includes the required `method` and `_verb` fields, as well as the filtered `data`. + +Note that this assumes the `people` list is already defined and populated with dictionaries containing `name` and `age` keys. \ No newline at end of file