You can download this code by clicking the button below.
This code is now available for download.
This function creates a simple Web API using the Starlette framework to generate and return user information. The function accepts a user ID and returns a JSON object containing the user ID, name, email, and status.
Technology Stack : Starlette, JSONResponse, Request
Code Type : Web API
Code Difficulty : Intermediate
def random_user_info(user_id):
from starlette.responses import JSONResponse
from starlette.requests import Request
import random
def generate_user_info():
return {
"id": user_id,
"name": f"User{user_id}",
"email": f"user{user_id}@example.com",
"status": random.choice(["active", "inactive", "suspended"])
}
def get_user_info(request: Request):
user_info = generate_user_info()
return JSONResponse(content=user_info)
return get_user_info