kipping-0fs9ya/endpoints/logout.post.py
2025-03-23 17:42:50 +01:00

30 lines
985 B
Python

from fastapi import APIRouter, HTTPException
laptops = [
{"name": "MacBook Pro"},
{"name": "Dell XPS"},
{"name": "Lenovo ThinkPad"},
{"name": "HP Spectre"},
{"name": "Asus ZenBook"}
]
router = APIRouter()
@router.post("/logout")
async def logout():
"""Returns list of laptop names"""
if request.method != "POST":
raise HTTPException(status_code=405, detail="Method Not Allowed")
return {
"method": "POST",
"_verb": "post",
"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.