-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
134 lines (97 loc) · 3.63 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
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flaskext.mysql import MySQL
app = Flask(__name__)
api = Api(app)
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'dbpyapi'
app.config['MYSQL_DATABASE_HOST'] = '127.0.0.1'
mysql.init_app(app)
# Initialize Database connection
conn = mysql.connect()
# create database cursor
cursor = conn.cursor()
# User creation class
class CreateUser(Resource):
def post(self):
try:
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']
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')
# Authentication Class
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']
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)}
api.add_resource(AuthenticateUser, '/AuthenticateUser')
# Add Items Class
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']
cursor.callproc('sp_AddItems',(_userId,_item))
data = cursor.fetchall()
conn.commit()
return {'StatusCode': '200', 'Message': 'Success'}
except Exception as e:
return {'error': str(e)}
# GetAll Items
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']
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)}
if __name__ == '__main__':
app.run(debug=True)