-
Notifications
You must be signed in to change notification settings - Fork 7
/
Simple restful api demo.py
181 lines (141 loc) · 4.75 KB
/
Simple restful api demo.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
General steps
1. import necessary libraries
2. initialize app and api
use app = Flask(__name__)
and api = Api(app)
3. define class ToDo(Resource), inherit Resouce, in the class we can further defien methods such as post, get...
class Todo(Resource)>
def get():
def post():
4. use api.add_resource ass routes, corresponding to url
api.add_resource(ToDo, "/todo")
1. import necessary libraries
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
2. initialize app and api
app = Flask(__name__)
api = Api(app)
def checkPostedData(postedData, functionName):
if (functionName == "add" or functionName == "subtract" or functionName == "multiply"):
if "x" not in postedData or "y" not in postedData:
return 301 #Missing parameter
else:
return 200
elif (functionName == "division"):
if "x" not in postedData or "y" not in postedData:
return 301
elif int(postedData["y"])==0:
return 302
else:
return 200
3. define class ToDo(Resource), inherit Resouce, in the class we can further defien methods such as post, get...
class Add(Resource):
def post(self):
#If I am here, then the resouce Add was requested using the method POST
#Step 1: Get posted data:
postedData = request.get_json()
#Steb 1b: Verify validity of posted data
status_code = checkPostedData(postedData, "add")
if (status_code!=200):
retJson = {
"Message": "An error happened",
"Status Code":status_code
}
return jsonify(retJson)
#If i am here, then status_code == 200
x = postedData["x"]
y = postedData["y"]
x = int(x)
y = int(y)
#Step 2: Add the posted data
ret = x+y
retMap = {
'Message': ret,
'Status Code': 200
}
return jsonify(retMap)
class Subtract(Resource):
def post(self):
#If I am here, then the resouce Subtract was requested using the method POST
#Step 1: Get posted data:
postedData = request.get_json()
#Steb 1b: Verify validity of posted data
status_code = checkPostedData(postedData, "subtract")
if (status_code!=200):
retJson = {
"Message": "An error happened",
"Status Code":status_code
}
return jsonify(retJson)
#If i am here, then status_code == 200
x = postedData["x"]
y = postedData["y"]
x = int(x)
y = int(y)
#Step 2: Subtract the posted data
ret = x-y
retMap = {
'Message': ret,
'Status Code': 200
}
return jsonify(retMap)
class Multiply(Resource):
def post(self):
#If I am here, then the resouce Multiply was requested using the method POST
#Step 1: Get posted data:
postedData = request.get_json()
#Steb 1b: Verify validity of posted data
status_code = checkPostedData(postedData, "multiply")
if (status_code!=200):
retJson = {
"Message": "An error happened",
"Status Code":status_code
}
return jsonify(retJson)
#If i am here, then status_code == 200
x = postedData["x"]
y = postedData["y"]
x = int(x)
y = int(y)
#Step 2: Multiply the posted data
ret = x*y
retMap = {
'Message': ret,
'Status Code': 200
}
return jsonify(retMap)
class Divide(Resource):
def post(self):
#If I am here, then the resouce Divide was requested using the method POST
#Step 1: Get posted data:
postedData = request.get_json()
#Steb 1b: Verify validity of posted data
status_code = checkPostedData(postedData, "division")
if (status_code!=200):
retJson = {
"Message": "An error happened",
"Status Code":status_code
}
return jsonify(retJson)
#If i am here, then status_code == 200
x = postedData["x"]
y = postedData["y"]
x = int(x)
y = int(y)
#Step 2: Multiply the posted data
ret = (x*1.0)/y
retMap = {
'Message': ret,
'Status Code': 200
}
return jsonify(retMap)
4. use api.add_resource ass routes, corresponding to url
api.add_resource(Add, "/add")
api.add_resource(Subtract, "/subtract")
api.add_resource(Multiply, "/multiply")
api.add_resource(Divide, "/division")
@app.route('/')
def hello_world():
return "Hello World!"
if __name__=="__main__":
app.run(debug=True)