Update code in endpoints/users.get.py

This commit is contained in:
Backend IM Bot 2025-03-22 13:42:13 +01:00
parent 56aa633c85
commit bf942c1ddf

View File

@ -1,9 +1,18 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
users = [ users = [
{"id": "1", "name": "Alice"}, {
{"id": "2", "name": "Bob"}, "id": "user1",
{"id": "3", "name": "Charlie"} "username": "alice",
"email": "alice@example.com",
"disabled": False
},
{
"id": "user2",
"username": "bob",
"email": "bob@example.com",
"disabled": True
}
] ]
router = APIRouter() router = APIRouter()
@ -11,6 +20,9 @@ router = APIRouter()
@router.get("/users") @router.get("/users")
async def get_users(): async def get_users():
"""Fetch list of users""" """Fetch list of users"""
if request.method != "GET":
raise HTTPException(status_code=405, detail="Method Not Allowed")
return { return {
"method": "GET", "method": "GET",
"_verb": "get", "_verb": "get",
@ -18,14 +30,10 @@ async def get_users():
} }
``` ```
To summarize: This endpoint follows the provided rules and examples:
- Imported necessary modules - It uses the `@router.get` decorator for the GET method.
- Initialized an in-memory `users` list with sample data - It checks if the request method is GET and raises a 405 error if not.
- Created a `GET` endpoint at `/users` path using `@router.get` decorator - The response includes the "method" and "_verb" metadata fields.
- Returned a dictionary response with: - It returns a list of user objects from the in-memory `users` list.
- `"method": "GET"` to include the HTTP method - The structure and fields of the response match the provided examples.
- `"_verb": "get"` for method metadata
- `"users": users` to return the list of user dictionaries
The code follows the provided rules and examples, returning only the list of users for a `GET` request to `/users`.