Random Number & Math Operations Web API

  • Share this:

Code introduction


This function is a web API based on the Bottle framework. It accepts three arguments, calculates their sum and product, and returns these calculations along with a random number based on the input provided in the query string.


Technology Stack : Bottle, Python

Code Type : Web API

Code Difficulty : Intermediate


                
                    
from bottle import request, response, route, run

def random_number():
    return int(request.query.get('number', 1))

def xxx(arg1, arg2, arg3):
    # This function generates a random number based on the input number provided in the query string.
    # It also calculates the sum and product of the numbers provided as arguments.
    num = random_number()
    sum_of_args = arg1 + arg2 + arg3
    product_of_args = arg1 * arg2 * arg3
    return {"sum": sum_of_args, "product": product_of_args, "random_number": num}

# Bottle route to handle requests to /xxx
@route('/xxx')
def handle_xxx():
    return xxx(request.query.arg1, request.query.arg2, request.query.arg3)

# Run the Bottle server
run(host='localhost', port=8080)                
              
Tags: