-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (58 loc) · 2.4 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import random
from flask import Flask, request, make_response, jsonify
app = Flask(__name__, instance_relative_config=True)
@app.route('/add')
def add():
a = request.args.get('a', type=float)
b = request.args.get('b', type=float)
if a and b:
return make_response(jsonify(s=a+b), 200) #HTTP 200 OK
else:
return make_response('Invalid input\n', 400) #HTTP 400 BAD REQUEST
#Endpoint /sub for subtraction which takes a and b as query parameters.
@app.route('/sub')
def sub():
a = request.args.get('a', type=float)
b = request.args.get('b', type=float)
if a and b:
return make_response(jsonify(s=a-b), 200) #HTTP 200 OK
else:
return make_response('Invalid input\n', 400) #HTTP 400 BAD REQUEST
#Endpoint /mul for multiplication which takes a and b as query parameters.
@app.route('/mul')
def mul():
a = request.args.get('a', type=float)
b = request.args.get('b', type=float)
if a and b:
return make_response(jsonify(s=a*b), 200) #HTTP 200 OK
else:
return make_response('Invalid input\n', 400) #HTTP 400 BAD REQUEST
#Endpoint /div for division which takes a and b as query parameters. Returns HTTP 400 BAD REQUEST also for division by zero.
@app.route('/div')
def div():
a = request.args.get('a', type=float)
b = request.args.get('b', type=float)
if a and b and b!=0:
return make_response(jsonify(s=a/b), 200) #HTTP 200 OK
else:
return make_response('Invalid input\n', 400) #HTTP 400 BAD REQUEST
#Endpoint /mod for modulo which takes a and b as query parameters. Returns HTTP 400 BAD REQUEST also for division by zero.
@app.route('/mod')
def mod():
a = request.args.get('a', type=float)
b = request.args.get('b', type=float)
if a and b:
return make_response(jsonify(s=a%b), 200) #HTTP 200 OK
else:
return make_response('Invalid input\n', 400) #HTTP 400 BAD REQUEST
#Endpoint /random which takes a and b as query parameters and returns a random number between a and b included. Returns HTTP 400 BAD REQUEST if a is greater than b.
@app.route('/random')
def random():
a = request.args.get('a', type=float)
b = request.args.get('b', type=float)
if a and b and (a>b):
return make_response(jsonify(random.randrange(a, b)), 200) #HTTP 200 OK
else:
return make_response('Invalid input\n', 400) #HTTP 400 BAD REQUEST
if __name__ == '__main__':
app.run(debug=True)