fix: API endpoints input type safety
This commit is contained in:
13
src/app.py
13
src/app.py
@@ -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():
|
||||||
|
try:
|
||||||
x = int(request.args.get('x', 1))
|
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():
|
||||||
|
try:
|
||||||
x = int(request.args.get('x', 1))
|
x = int(request.args.get('x', 1))
|
||||||
y = int(request.args.get('y', 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():
|
||||||
|
try:
|
||||||
x = int(request.args.get('x', 1))
|
x = int(request.args.get('x', 1))
|
||||||
y = int(request.args.get('y', 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():
|
||||||
|
try:
|
||||||
x = int(request.args.get('x', 1))
|
x = int(request.args.get('x', 1))
|
||||||
y = int(request.args.get('y', 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)})
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user