35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
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": "Laptop A"},
|
|
{"name": "Laptop B"},
|
|
{"name": "Laptop C"}
|
|
]
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/logout")
|
|
async def logout_demo():
|
|
"""Demo logout endpoint"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"laptop_names": [laptop["name"] for laptop in laptops]
|
|
}
|
|
```
|
|
|
|
This endpoint:
|
|
|
|
- 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
|
|
|
|
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. |