/
app.py
156 lines (134 loc) · 5.37 KB
/
app.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
from flask import Flask, jsonify ,request, Response
import Validation as Validation
import json
import Datahandler as datahandler
import login_controller as auth_check
from functools import wraps
import jwt
app= Flask(__name__)
def token_required(f):
@wraps(f)
def decorated (*args, **kwargs):
token = request.headers.get('token')
result = auth_check.validate_token(request,token)
if result:
return f(*args, **kwargs)
else:
return jsonify({'message':'Token is invalid'}), 403
return decorated
@app.route('/books')
@token_required
def welcomerequest():
result = datahandler.get_all_data()
if result is not True:
return Response(json.dumps('try adding data'),status = 400, mimetype = 'application/json')
return jsonify(result)
@app.route('/token')
def get_token():
if not request.authorization:
return Response(json.dumps('Missing autherrization'),status= 401,mimetype = 'application/json')
result = auth_check.login_check(request.authorization)
if result is True:
token = auth_check.create_token(request.authorization)
return Response(json.dumps(token),status= 200,mimetype = 'application/json')
else:
return Response(json.dumps('Wrong credential'),status= 401,mimetype = 'application/json')
@app.route('/register', methods=['POST'])
def registration():
request_model = request.get_json()
result = Validation.validate_registraton_model(request_model)
if result:
status = auth_check.registration_check(request_model)
if status is True:
return Response(json.dumps('User already exist try another username'),status=403,mimetype = 'application/json')
else:
user_status = auth_check.add_user(request_model)
return Response(json.dumps(user_status),status=201,mimetype = 'application/json')
else:
return Response(json.dumps('Request model is wrong'),status=403,mimetype = 'application/json')
@app.route('/books/<int:Isbn>')
@token_required
def get_books_by_id(Isbn):
if(Isbn is False):
return jsonify("error.. please enter book ISDN")
else:
results = datahandler.search_data(Isbn)
if results is not None:
return jsonify(results)
else:
return jsonify("Please try with another id")
@app.route('/books', methods=['POST'])
@token_required
def add_book():
data= request.get_json()
result = Validation.validate_model(data)
if result is True:
duplicate = datahandler.validate_isbn_exist(data["Isbn"])
if duplicate is False:
datahandler.insert_data(data)
response = Response(json.dumps("Added"), 201 ,mimetype = 'application/json')
response.headers['Location']= f'/books/' + str(data['Isbn'])
return response
else:
error_help={
"error":"Invalid data passed",
"help":"ISBN already exist"
}
response = Response(json.dumps(error_help),status= 415 ,mimetype = 'application/json')
return response
else:
error_help={
"error":"Invalid data passed",
"help":"Check the model which is passed"
}
response = Response(json.dumps(error_help),status= 415 ,mimetype = 'application/json')
return response
@app.route('/books/<int:isbn>', methods = ['PUT'])
@token_required
def update_books(isbn):
if isbn is False:
error_help={
"error":"ISBN is missing",
"help":"Check the ISBN which is passed"
}
response = Response(json.dumps(error_help),status= 415 ,mimetype = 'application/json')
return response
else:
data=request.get_json()
result = Validation.validate_update_model(data)
if result is True:
datahandler.update_data_using_isbn(isbn, data)
response = Response(json.dumps("Added"), 201 ,mimetype = 'application/json')
response.headers['Location']= f'/books/' + str(isbn)
return response
else:
error_help={
"error":"Invalid data passed",
"help":"Check the model which is passed"
}
response = Response(json.dumps(error_help),status= 415 ,mimetype = 'application/json')
return response
@app.route('/books/<int:isbn>', methods =['DELETE'])
@token_required
def delete_books_by_id(isbn):
if(isbn is False):
error_help={
"error":"Invalid ISBN passed",
"help":"ISBN should not be empty"
}
response = Response(json.dumps(error_help),status= 415 ,mimetype = 'application/json')
return response
else:
result = datahandler.delete_data_using_isbn(isbn)
if result:
response = Response(json.dumps("Deleted"), 201 ,mimetype = 'application/json')
response.headers['Location']= f'/books/' + str(isbn)
return response
else:
error_message={"error":"Invalid ISBN passed",
"help":"ISBN not found"
}
response = Response(json.dumps(error_message),status= 415 ,mimetype = 'application/json')
return response
if __name__ == "__main__":
app.run(host='0.0.0.0',port=5001,debug=True)