From b51e6fe9bd5957406857cf0f4d4cc1f88ad4cb6e Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sun, 23 Mar 2025 19:51:01 +0100 Subject: [PATCH] Update code in endpoints/logout.post.py --- endpoints/logout.post.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/endpoints/logout.post.py b/endpoints/logout.post.py index 898276c..e378e09 100644 --- a/endpoints/logout.post.py +++ b/endpoints/logout.post.py @@ -1,36 +1,35 @@ +Here's an example FastAPI endpoint that returns a list of laptop names following the provided rules and examples: + +```python from fastapi import APIRouter, HTTPException laptops = [ - {"name": "MacBook Pro"}, - {"name": "Dell XPS"}, - {"name": "Lenovo ThinkPad"}, - {"name": "HP Spectre"} + {"name": "Laptop A"}, + {"name": "Laptop B"}, + {"name": "Laptop C"} ] router = APIRouter() @router.post("/logout") -async def logout(): - """Returns list of laptop names""" +async def logout_demo(): + """Demo logout endpoint""" if request.method != "POST": - raise HTTPException(status_code=405, detail={ - "message": "Method Not Allowed", - "method": "POST", - "_verb": "post" - }) + raise HTTPException(status_code=405, detail="Method Not Allowed") return { "method": "POST", - "laptops": [laptop["name"] for laptop in laptops] + "_verb": "post", + "laptop_names": [laptop["name"] for laptop in laptops] } ``` -This implementation follows the provided rules and examples: +This endpoint: -1. It uses the `@router.post` decorator for the `/logout` endpoint. -2. It checks if the request method is POST, and raises a 405 Method Not Allowed error if not. -3. The response includes the "method": "POST" field. -4. It returns a list of laptop names from the in-memory `laptops` list. -5. The response structure matches the provided examples. +- Uses the `@router.post` decorator for the `/logout` path +- Checks if the request method is POST, raising a 405 error otherwise +- Returns a dictionary with the "method", "_verb", and a list of laptop names -Note that this endpoint does not actually handle any logout functionality, as the description provided was "endpoints that returns list of laptops names". \ No newline at end of file +The list of laptop names is generated by iterating over the `laptops` list and extracting the "name" value from each dictionary. + +Note that this example follows the provided rules, such as strict method adherence, response requirements, error handling, data storage, response format, and code structure. \ No newline at end of file