Update code in endpoints/logout.post.py

This commit is contained in:
Backend IM Bot 2025-03-23 19:51:01 +01:00
parent 4f3c1e321e
commit b51e6fe9bd

View File

@ -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 from fastapi import APIRouter, HTTPException
laptops = [ laptops = [
{"name": "MacBook Pro"}, {"name": "Laptop A"},
{"name": "Dell XPS"}, {"name": "Laptop B"},
{"name": "Lenovo ThinkPad"}, {"name": "Laptop C"}
{"name": "HP Spectre"}
] ]
router = APIRouter() router = APIRouter()
@router.post("/logout") @router.post("/logout")
async def logout(): async def logout_demo():
"""Returns list of laptop names""" """Demo logout endpoint"""
if request.method != "POST": if request.method != "POST":
raise HTTPException(status_code=405, detail={ raise HTTPException(status_code=405, detail="Method Not Allowed")
"message": "Method Not Allowed",
"method": "POST",
"_verb": "post"
})
return { return {
"method": "POST", "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. - Uses the `@router.post` decorator for the `/logout` path
2. It checks if the request method is POST, and raises a 405 Method Not Allowed error if not. - Checks if the request method is POST, raising a 405 error otherwise
3. The response includes the "method": "POST" field. - Returns a dictionary with the "method", "_verb", and a list of laptop names
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". 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.