Update code in endpoints\users.get.py
This commit is contained in:
parent
03d34f83e8
commit
785e8ad0bd
@ -1,30 +1,50 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
users = [
|
||||
{
|
||||
"id": "1",
|
||||
"email": "user1@example.com",
|
||||
"password": "password1",
|
||||
"disabled": False
|
||||
},
|
||||
{
|
||||
"id": "2",
|
||||
"email": "user2@example.com",
|
||||
"password": "password2",
|
||||
"disabled": True
|
||||
}
|
||||
]
|
||||
users = [] # In-memory storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/users", response_model=list)
|
||||
async def get_users():
|
||||
"""Get all registered users"""
|
||||
if request.method != "GET":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
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",
|
||||
"users": users
|
||||
"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.
|
Loading…
x
Reference in New Issue
Block a user