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
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".
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.