You can download this code by clicking the button below.
This code is now available for download.
This function is a Web API that accepts two query parameters (min and max) and returns a random integer between min and max.
Technology Stack : Starlette, random
Code Type : Web API
Code Difficulty : Intermediate
from starlette.responses import JSONResponse
from starlette.routing import Route, Router
import random
def generate_random_number(min_value, max_value):
return random.randint(min_value, max_value)
def my_custom_function(request):
min_val = int(request.query_params.get('min', '1'))
max_val = int(request.query_params.get('max', '100'))
result = generate_random_number(min_val, max_val)
return JSONResponse({"random_number": result})
router = Router(
[
Route("/random", my_custom_function),
]
)