-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rest-API.py
154 lines (136 loc) · 4 KB
/
Rest-API.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# install flask-restful
from flask import Flask, request, jsonify
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
def checkPosted(data, functionName):
if (functionName in ("add", "subtract", "multiply")):
if "x" not in data or "y" not in data:
return 301
else:
return 200
if (functionName == "divide"):
if "x" not in data or "y" not in data:
return 301
elif data["x"] == 0 or data["y"] == 0:
return 302
else:
return 200
class Add(Resource):
def post(self):
# passing json into post requests
data = request.get_json()
# validate posted data
status_code = checkPosted(data, "add")
if (status_code !=200):
mapping = {
'Message': "Err: One or more params are missing",
'Status Code': 301
}
return jsonify(mapping)
# if (isinstance(x,int) and isinstance(y,int)):
x = data["x"]
y = data["y"]
x = int(x)
y = int(y)
# result
res = x+y
mapping = {
'Sum': res,
'Status Code': 200
}
return jsonify(mapping)
return app
# if im here requests something
def get(self):
pass
class Subtract(Resource):
def post(self):
# passing json into post requests
data = request.get_json()
# validate posted data
status_code = checkPosted(data, "subtract")
if (status_code !=200):
mapping = {
'Message': "Err: One or more params are missing",
'Status Code': 301
}
return jsonify(mapping)
# if (isinstance(x,int) and isinstance(y,int)):
x = data["x"]
y = data["y"]
x = int(x)
y = int(y)
# result
res = x-y
mapping = {
'Difference': res,
'Status Code': 200
}
return jsonify(mapping)
return app
def get(self):
pass
class Divide(Resource):
def post(self):
# passing json into post requests
data = request.get_json()
# validate posted data
status_code = checkPosted(data, "divide")
if (status_code !=200):
mapping = {
'Message': "Err: One or more params are missing",
'Status Code': 301
}
if (status_code == 302):
mapping = {
'Message': "Err: One or more params is 0",
'Status Code': 302
}
return jsonify(mapping)
# if (isinstance(x,int) and isinstance(y,int)):
x = data["x"]
y = data["y"]
x = int(x)
y = int(y)
# result
res = x // y
mapping = {
'Quotient': res,
'Status Code': 200
}
return jsonify(mapping)
return app
class Multiply(Resource):
def post(self):
# passing json into post requests
data = request.get_json()
# validate posted data
status_code = checkPosted(data, "multiply")
if (status_code !=200):
mapping = {
'Message': "Err: One or more params are missing",
'Status Code': 301
}
return jsonify(mapping)
# if (isinstance(x,int) and isinstance(y,int)):
x = data["x"]
y = data["y"]
x = int(x)
y = int(y)
# result
res = x*y
mapping = {
'Product': res,
'Status Code': 200
}
return jsonify(mapping)
return app
# if im here requests something
def get(self):
pass
# add resource and setup listening API
api.add_resource(Add, "/add")
api.add_resource(Subtract, "/subtract")
api.add_resource(Divide, "/divide")
api.add_resource(Multiply, "/multiply")