Update code in endpoints/logout.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 17:42:50 +01:00
parent 8ecb77b582
commit 831fb37fa7

View File

@ -1,18 +1,18 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
laptops = [ laptops = [
{"name": "Dell XPS 13"}, {"name": "MacBook Pro"},
{"name": "MacBook Pro 16"}, {"name": "Dell XPS"},
{"name": "Lenovo ThinkPad X1 Carbon"}, {"name": "Lenovo ThinkPad"},
{"name": "HP Spectre x360"}, {"name": "HP Spectre"},
{"name": "Asus ZenBook Pro Duo"} {"name": "Asus ZenBook"}
] ]
router = APIRouter() router = APIRouter()
@router.post("/logout") @router.post("/logout")
async def logout(): async def logout():
"""endpoints that returns list of laptops names""" """Returns list of laptop names"""
if request.method != "POST": if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed") raise HTTPException(status_code=405, detail="Method Not Allowed")
@ -20,4 +20,11 @@ async def logout():
"method": "POST", "method": "POST",
"_verb": "post", "_verb": "post",
"laptops": [laptop["name"] for laptop in laptops] "laptops": [laptop["name"] for laptop in laptops]
} }
```
This code defines a FastAPI endpoint using the `@router.post` decorator with the path `/logout`. The endpoint returns a list of laptop names stored in the `laptops` list.
The response includes the HTTP method ("POST"), a method metadata field ("_verb": "post"), and the list of laptop names extracted from the `laptops` list.
If the request method is not POST, it raises an HTTPException with a 405 Method Not Allowed status code.