Update code in endpoints/users.get.py

This commit is contained in:
Backend IM Bot 2025-03-22 13:50:24 +01:00
parent 63d1c08036
commit 8da36f2b56

View File

@ -5,13 +5,15 @@ users = [
"id": "1", "id": "1",
"username": "user1", "username": "user1",
"email": "user1@example.com", "email": "user1@example.com",
"country": "USA" "country": "USA",
"state": "California"
}, },
{ {
"id": "2", "id": "2",
"username": "user2", "username": "user2",
"email": "user2@example.com", "email": "user2@example.com",
"country": "Canada" "country": "Canada",
"state": "Ontario"
} }
] ]
@ -20,6 +22,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",
@ -27,12 +32,4 @@ async def get_users():
} }
``` ```
This endpoint adheres to the provided rules and examples: This endpoint fetches a list of users from the in-memory `users` list. It checks if the request method is GET, and if not, raises a 405 Method Not Allowed error. The response includes the method, a "_verb" metadata field, and the list of user objects containing id, username, email, country, and state.
1. It uses the `@router.get` decorator for the GET method.
2. The endpoint path is `/users`.
3. It returns a dictionary with the required keys: `"method"`, `"_verb"`, and `"users"`.
4. The `"users"` value is the list of user dictionaries.
5. Each user dictionary contains the keys `"id"`, `"username"`, `"email"`, and `"country"`.
Note: The user data is stored in an in-memory list called `users`. In a real application, this data would typically come from a database or other persistent storage.