fix: API endpoints input type safety

This commit is contained in:
Alec.Schmidt
2025-03-12 21:11:29 +01:00
parent 34e24304fa
commit be3db6e605

View File

@@ -45,30 +45,43 @@ global_data = {'username': 'no_user'}
# incrementation route # incrementation route
@app.route('/inc') @app.route('/inc')
def plus_one(): def plus_one():
x = int(request.args.get('x', 1)) try:
x = int(request.args.get('x', 1))
except:
return json.dumps({"error": "Type error"})
return json.dumps({'x': operators.addition(x, 1)}) return json.dumps({'x': operators.addition(x, 1)})
# addition route, the parameters will be passed with 'x' and 'y' # addition route, the parameters will be passed with 'x' and 'y'
@app.route('/add') @app.route('/add')
def plus_y(): def plus_y():
x = int(request.args.get('x', 1)) try:
y = int(request.args.get('y', 1)) x = int(request.args.get('x', 1))
y = int(request.args.get('y', 1))
except:
return json.dumps({"error": "Type error"})
return json.dumps({'result': operators.addition(x, y)}) return json.dumps({'result': operators.addition(x, y)})
# multiplication route, the parameters will be passed with 'x' and 'y' # multiplication route, the parameters will be passed with 'x' and 'y'
@app.route('/mul') @app.route('/mul')
def multiply_y(): def multiply_y():
x = int(request.args.get('x', 1)) try:
y = int(request.args.get('y', 1)) x = int(request.args.get('x', 1))
y = int(request.args.get('y', 1))
except:
return json.dumps({"error": "Type error"})
return json.dumps({'result': operators.multiplication(x, y)}) return json.dumps({'result': operators.multiplication(x, y)})
# division route, the parameters will be passed with 'x' and 'y' # division route, the parameters will be passed with 'x' and 'y'
@app.route('/div') @app.route('/div')
def division_y(): def division_y():
x = int(request.args.get('x', 1)) try:
y = int(request.args.get('y', 1)) x = int(request.args.get('x', 1))
y = int(request.args.get('y', 1))
except:
return json.dumps({"error": "Type error"})
return json.dumps({'result': operators.division(x, y)}) return json.dumps({'result': operators.division(x, y)})