I am trying to do trigonometric calculations using python flask application by using query parameters. There are 2 query parameters angle and unit. I want to make unit an optional parameter so that suppose if I hit http://localhost:5000/trig/sin?angle=3 instead of http://localhost:5000/trig/sin?angle=3&unit=degree, it will also return the result sin(3).
@app.route("/trig/<func>")
def trig(func):
if str(func) == "sin":
try:
angle = request.args["angle"]
unit = request.args["unit"]
if str(angle) == "" or str(unit) != "degree" or str(unit) != "radian":
return "Invalid query parameter value(s)", 500
else:
sine = math.sin((math.radians(float(angle))))
return "{0}".format(sine)
except KeyError:
return "Missing query parameter: angle", 500"