├── README.md └── api.py /README.md: -------------------------------------------------------------------------------- 1 | # RESTfulApiUsingPythonFlask 2 | Creating a RESTful API using Python & MySQL 3 | 4 | Tutorial for this source code can be found at [Code Handbook](http://codehandbook.org/creating-restful-api-using-python-flask-mysql/) 5 | -------------------------------------------------------------------------------- /api.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_restful import Resource, Api 3 | from flask_restful import reqparse 4 | from flask.ext.mysql import MySQL 5 | 6 | 7 | 8 | mysql = MySQL() 9 | app = Flask(__name__) 10 | 11 | # MySQL configurations 12 | app.config['MYSQL_DATABASE_USER'] = 'jay' 13 | app.config['MYSQL_DATABASE_PASSWORD'] = 'jay' 14 | app.config['MYSQL_DATABASE_DB'] = 'ItemListDb' 15 | app.config['MYSQL_DATABASE_HOST'] = 'localhost' 16 | 17 | 18 | mysql.init_app(app) 19 | 20 | api = Api(app) 21 | 22 | class AuthenticateUser(Resource): 23 | def post(self): 24 | try: 25 | # Parse the arguments 26 | 27 | parser = reqparse.RequestParser() 28 | parser.add_argument('email', type=str, help='Email address for Authentication') 29 | parser.add_argument('password', type=str, help='Password for Authentication') 30 | args = parser.parse_args() 31 | 32 | _userEmail = args['email'] 33 | _userPassword = args['password'] 34 | 35 | conn = mysql.connect() 36 | cursor = conn.cursor() 37 | cursor.callproc('sp_AuthenticateUser',(_userEmail,)) 38 | data = cursor.fetchall() 39 | 40 | 41 | if(len(data)>0): 42 | if(str(data[0][2])==_userPassword): 43 | return {'status':200,'UserId':str(data[0][0])} 44 | else: 45 | return {'status':100,'message':'Authentication failure'} 46 | 47 | except Exception as e: 48 | return {'error': str(e)} 49 | 50 | 51 | class GetAllItems(Resource): 52 | def post(self): 53 | try: 54 | # Parse the arguments 55 | parser = reqparse.RequestParser() 56 | parser.add_argument('id', type=str) 57 | args = parser.parse_args() 58 | 59 | _userId = args['id'] 60 | 61 | conn = mysql.connect() 62 | cursor = conn.cursor() 63 | cursor.callproc('sp_GetAllItems',(_userId,)) 64 | data = cursor.fetchall() 65 | 66 | items_list=[]; 67 | for item in data: 68 | i = { 69 | 'Id':item[0], 70 | 'Item':item[1] 71 | } 72 | items_list.append(i) 73 | 74 | return {'StatusCode':'200','Items':items_list} 75 | 76 | except Exception as e: 77 | return {'error': str(e)} 78 | 79 | class AddItem(Resource): 80 | def post(self): 81 | try: 82 | # Parse the arguments 83 | parser = reqparse.RequestParser() 84 | parser.add_argument('id', type=str) 85 | parser.add_argument('item', type=str) 86 | args = parser.parse_args() 87 | 88 | _userId = args['id'] 89 | _item = args['item'] 90 | 91 | print _userId; 92 | 93 | conn = mysql.connect() 94 | cursor = conn.cursor() 95 | cursor.callproc('sp_AddItems',(_userId,_item)) 96 | data = cursor.fetchall() 97 | 98 | conn.commit() 99 | return {'StatusCode':'200','Message': 'Success'} 100 | 101 | except Exception as e: 102 | return {'error': str(e)} 103 | 104 | 105 | 106 | class CreateUser(Resource): 107 | def post(self): 108 | try: 109 | # Parse the arguments 110 | parser = reqparse.RequestParser() 111 | parser.add_argument('email', type=str, help='Email address to create user') 112 | parser.add_argument('password', type=str, help='Password to create user') 113 | args = parser.parse_args() 114 | 115 | _userEmail = args['email'] 116 | _userPassword = args['password'] 117 | 118 | conn = mysql.connect() 119 | cursor = conn.cursor() 120 | cursor.callproc('spCreateUser',(_userEmail,_userPassword)) 121 | data = cursor.fetchall() 122 | 123 | if len(data) is 0: 124 | conn.commit() 125 | return {'StatusCode':'200','Message': 'User creation success'} 126 | else: 127 | return {'StatusCode':'1000','Message': str(data[0])} 128 | 129 | except Exception as e: 130 | return {'error': str(e)} 131 | 132 | 133 | 134 | api.add_resource(CreateUser, '/CreateUser') 135 | api.add_resource(AuthenticateUser, '/AuthenticateUser') 136 | api.add_resource(AddItem, '/AddItem') 137 | api.add_resource(GetAllItems, '/GetAllItems') 138 | 139 | if __name__ == '__main__': 140 | app.run(debug=True) 141 | --------------------------------------------------------------------------------