-
Notifications
You must be signed in to change notification settings - Fork 39
/
api.py
140 lines (102 loc) · 3.92 KB
/
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
from flask import Flask
from flask_restful import Resource, Api
from flask_restful import reqparse
from flask.ext.mysql import MySQL
mysql = MySQL()
app = Flask(__name__)
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'jay'
app.config['MYSQL_DATABASE_PASSWORD'] = 'jay'
app.config['MYSQL_DATABASE_DB'] = 'ItemListDb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
api = Api(app)
class AuthenticateUser(Resource):
def post(self):
try:
# Parse the arguments
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help='Email address for Authentication')
parser.add_argument('password', type=str, help='Password for Authentication')
args = parser.parse_args()
_userEmail = args['email']
_userPassword = args['password']
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_AuthenticateUser',(_userEmail,))
data = cursor.fetchall()
if(len(data)>0):
if(str(data[0][2])==_userPassword):
return {'status':200,'UserId':str(data[0][0])}
else:
return {'status':100,'message':'Authentication failure'}
except Exception as e:
return {'error': str(e)}
class GetAllItems(Resource):
def post(self):
try:
# Parse the arguments
parser = reqparse.RequestParser()
parser.add_argument('id', type=str)
args = parser.parse_args()
_userId = args['id']
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_GetAllItems',(_userId,))
data = cursor.fetchall()
items_list=[];
for item in data:
i = {
'Id':item[0],
'Item':item[1]
}
items_list.append(i)
return {'StatusCode':'200','Items':items_list}
except Exception as e:
return {'error': str(e)}
class AddItem(Resource):
def post(self):
try:
# Parse the arguments
parser = reqparse.RequestParser()
parser.add_argument('id', type=str)
parser.add_argument('item', type=str)
args = parser.parse_args()
_userId = args['id']
_item = args['item']
print _userId;
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_AddItems',(_userId,_item))
data = cursor.fetchall()
conn.commit()
return {'StatusCode':'200','Message': 'Success'}
except Exception as e:
return {'error': str(e)}
class CreateUser(Resource):
def post(self):
try:
# Parse the arguments
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help='Email address to create user')
parser.add_argument('password', type=str, help='Password to create user')
args = parser.parse_args()
_userEmail = args['email']
_userPassword = args['password']
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('spCreateUser',(_userEmail,_userPassword))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return {'StatusCode':'200','Message': 'User creation success'}
else:
return {'StatusCode':'1000','Message': str(data[0])}
except Exception as e:
return {'error': str(e)}
api.add_resource(CreateUser, '/CreateUser')
api.add_resource(AuthenticateUser, '/AuthenticateUser')
api.add_resource(AddItem, '/AddItem')
api.add_resource(GetAllItems, '/GetAllItems')
if __name__ == '__main__':
app.run(debug=True)