2025-03-22 13:42:13 +01:00

39 lines
1004 B
Python

from fastapi import APIRouter, HTTPException
users = [
{
"id": "user1",
"username": "alice",
"email": "alice@example.com",
"disabled": False
},
{
"id": "user2",
"username": "bob",
"email": "bob@example.com",
"disabled": True
}
]
router = APIRouter()
@router.get("/users")
async def get_users():
"""Fetch list of users"""
if request.method != "GET":
raise HTTPException(status_code=405, detail="Method Not Allowed")
return {
"method": "GET",
"_verb": "get",
"users": users
}
```
This endpoint follows the provided rules and examples:
- It uses the `@router.get` decorator for the GET method.
- It checks if the request method is GET and raises a 405 error if not.
- The response includes the "method" and "_verb" metadata fields.
- It returns a list of user objects from the in-memory `users` list.
- The structure and fields of the response match the provided examples.