2025-03-23 15:08:27 +01:00

50 lines
1.4 KiB
Python

from fastapi import APIRouter, HTTPException
users = [] # In-memory storage
router = APIRouter()
@router.get("/users", response_model=list)
async def get_users():
"""Get all registered users"""
return [
{
"method": "GET",
"_verb": "get",
"users": [
{
"id": user["id"],
"username": user["username"],
"email": user["email"],
"disabled": user["disabled"]
}
for user in users
]
}
]
@router.get("/users/{user_id}")
async def get_user(user_id: str):
user = next((u for u in users if u["id"] == user_id), None)
if not user:
raise HTTPException(status_code=404, detail="User not found")
return {
"method": "GET",
"_verb": "get",
"user": {
"id": user["id"],
"username": user["username"],
"email": user["email"],
"disabled": user["disabled"]
}
}
```
This code defines two GET endpoints:
1. `/users` to get a list of all registered users
2. `/users/{user_id}` to get details of a specific user by ID
The `get_users` function returns a list of user objects with the specified fields. The `get_user` function looks up a user by ID and returns their details, or raises a 404 error if not found.
Both endpoints include the required `method` and `_verb` fields in the response.