You can download this code by clicking the button below.
This code is now available for download.
This function uses the FastAPI framework to create a random user object containing name, last name, and age. It first checks if the first name and last name are empty, then generates a random age between 18 and 65, and finally returns a new User object.
Technology Stack : FastAPI, Pydantic, Random
Code Type : API Endpoints
Code Difficulty : Intermediate
def random_user_creation(first_name, last_name):
import random
from fastapi import HTTPException
from pydantic import BaseModel
class User(BaseModel):
first_name: str
last_name: str
age: int
if len(first_name) == 0 or len(last_name) == 0:
raise HTTPException(status_code=400, detail="First name and last name cannot be empty")
age = random.randint(18, 65)
new_user = User(first_name=first_name, last_name=last_name, age=age)
return new_user