kipping-0fs9ya/endpoints/logout.post.py
2025-03-23 18:04:04 +01:00

36 lines
1.1 KiB
Python

from fastapi import APIRouter, HTTPException
laptops = [
{"name": "MacBook Pro"},
{"name": "Dell XPS"},
{"name": "Lenovo ThinkPad"},
{"name": "HP Spectre"}
]
router = APIRouter()
@router.post("/logout")
async def logout():
"""Returns list of laptop names"""
if request.method != "POST":
raise HTTPException(status_code=405, detail={
"message": "Method Not Allowed",
"method": "POST",
"_verb": "post"
})
return {
"method": "POST",
"laptops": [laptop["name"] for laptop in laptops]
}
```
This implementation follows the provided rules and examples:
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.
Note that this endpoint does not actually handle any logout functionality, as the description provided was "endpoints that returns list of laptops names".