-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
400 lines (343 loc) · 11.5 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import os
from flask import (
Flask,
request,
abort,
jsonify
)
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from models import db, setup_db, Movie, Actor
from auth import AuthError, requires_auth
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__)
moment = Moment(app)
app.secret_key = "mysecretkey"
setup_db(app)
'''
Set up CORS(Cross Origin Resource Sharing).
Allow '*' for all origins.
Delete the sample route after implementing the API endpoints
'''
CORS(app, resources={"/": {"origin": "*"}})
'''
CORS Headers. Use the after_request decorator
to set Access-Control-Allow
'''
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Headers',
'Content-Type,Authorization,true')
response.headers.add('Access-Control-Allow-Methods',
'GET,PUT,PATCH,POST,DELETE,OPTIONS')
return response
@app.route('/')
def index():
return jsonify({
'message': 'Casting Agency',
})
# Movie Routes
'''
@Implement endpoint
GET /movies
it should require the 'get:movies' permission
returns status code 200 and json {"success": True, "movies": movies}
where movies is the list of movies
or appropriate status code indicating reason for failure
'''
@app.route('/movies', methods=['GET'])
@requires_auth('get:movies')
def get_movies(jwt):
try:
movies = Movie.query.all()
return jsonify({
'success': True,
'movies': [movie.format() for movie in movies]
}), 200
except BaseException:
abort(404)
'''
@Implement endpoint
GET /movies/<id>
where <id> is the existing movie id
it should require the 'get:movies' permission
it should respond with a 404 error if <id> is not found
returns status code 200 and json {"success": True, "movie": movie}
where movie is the movie with specific id
or appropriate status code indicating reason for failure
'''
@app.route('/movies/<int:id>', methods=['GET'])
@requires_auth('get:movies')
def get_movie_by_specific_id(jwt, id):
movie = Movie.query.get(id)
if movie is None:
abort(404)
else:
return jsonify({
'success': True,
'movie': movie.format(),
}), 200
'''
@Implement endpoint
POST /movies
it should create a new row in the movie table
it should require the 'post:movies' permission
returns status code 200 and json {"success": True, "movie": movie}
where movie is an array containing only the newly created movie
or appropriate status code indicating reason for failure
'''
@app.route('/movies', methods=['POST'])
@requires_auth('post:movies')
def create_movie(jwt):
body = request.get_json()
if not('title' in body and 'release_date' in body):
abort(422)
title = body.get('title')
release_date = body .get('release_date')
try:
movie = Movie(title=title, release_date=release_date)
movie.insert()
return jsonify({
'success': True,
'movie': movie.format(),
})
except BaseException:
abort(422)
'''
@Implement endpoint
PATCH /movies/<id>
where <id> is the existing movie id
it should respond with a 404 error if <id> is not found
it should update the corresponding row for <id>
it should require the 'patch:movies' permission
returns status code 200 and json {"success": True, "movie": movie}
where movie an array containing only the updated movie
or appropriate status code indicating reason for failure
'''
@app.route('/movies/<int:id>', methods=['PATCH'])
@requires_auth('patch:movies')
def update_movie(jwt, id):
movie = Movie.query.get(id)
if movie:
try:
body = request.get_json()
title = body.get('title')
if title:
movie .title = title
release_date = body.get('release_date')
if release_date:
movie.release_date = release_date
movie.update()
return jsonify({
'success': True,
'movie': [movie.format()],
})
except BaseException:
abort(422)
else:
abort(404)
'''
@Implement endpoint
DELETE /movies/<id>
where <id> is the existing movie id
it should respond with a 404 error if <id> is not found
it should delete the corresponding row for <id>
it should require the 'delete:movies' permission
returns status code 200 and json {"success": True, "delete": id}
where id is the id of the deleted record
or appropriate status code indicating reason for failure
'''
@app.route('/movies/<int:id>', methods=['DELETE'])
@requires_auth('delete:movies')
def delete_movie(jwt, id):
movie = Movie.query.filter(Movie.id == id).one_or_none()
if movie:
try:
movie.delete()
return jsonify({
'success': True,
'delete': id,
})
except BaseException:
abort(422)
else:
abort(404)
# Actor Routes
'''
@Implement endpoint
GET /actors
it should require the 'get:actors' permission
returns status code 200 and json {"success": True, "actors": actors}
where actors is the list of actors
or appropriate status code indicating reason for failure
'''
@app.route('/actors', methods=['GET'])
@requires_auth('get:actors')
def get_actors(jwt):
try:
actors = Actor.query.all()
return jsonify({
'success': True,
'actors': [actor.format() for actor in actors]
}), 200
except BaseException:
abort(404)
'''
@Implement endpoint
GET /actors/<id>
where <id> is the existing actor id
it should require the 'get:actors' permission
it should respond with a 404 error if <id> is not found
returns status code 200 and json {"success": True, "actor": actor}
where actor is the actor with specific id
or appropriate status code indicating reason for failure
'''
@app.route('/actors/<int:id>', methods=['GET'])
@requires_auth('get:actors')
def get_actor_by_specific_id(jwt, id):
actor = Actor.query.get(id)
if actor is None:
abort(404)
else:
return jsonify({
'success': True,
'actor': actor.format(),
}), 200
'''
@Implement endpoint
POST /actors
it should create a new row in the actor table
it should require the 'post:actors' permission
returns status code 200 and json {"success": True, "actor": actor}
where actor is an array containing only the newly created actor
or appropriate status code indicating reason for failure
'''
@app.route('/actors', methods=['POST'])
@requires_auth('post:actors')
def create_actor(jwt):
body = request.get_json()
if not('name' in body and 'gender' in body and 'age' in body):
abort(422)
name = body.get('name')
gender = body .get('gender')
age = body .get('age')
try:
actor = Actor(name=name, gender=gender, age=age)
actor.insert()
return jsonify({
'success': True,
'actor': actor.format(),
})
except BaseException:
abort(422)
'''
@Implement endpoint
PATCH /actors/<id>
where <id> is the existing actor id
it should respond with a 404 error if <id> is not found
it should update the corresponding row for <id>
it should require the 'patch:actors' permission
returns status code 200 and json {"success": True, "actor": actor}
where movie an array containing only the updated actor
or appropriate status code indicating reason for failure
'''
@app.route('/actors/<int:id>', methods=['PATCH'])
@requires_auth('patch:actors')
def update_actor(jwt, id):
actor = Actor.query.get(id)
if actor:
try:
body = request.get_json()
name = body.get('name')
if name:
actor.name = name
gender = body.get('gender')
if gender:
actor.gender = gender
age = body.get('age')
if age:
actor.age = age
actor.update()
return jsonify({
'success': True,
'actor': [actor.format()],
})
except BaseException:
abort(422)
else:
abort(404)
'''
@Implement delete endpoint
DELETE /actors/<id>
where <id> is the existing actor id
it should respond with a 404 error if <id> is not found
it should delete the corresponding row for <id>
it should require the 'delete:actors' permission
returns status code 200 and json {"success": True, "delete": id}
where id is the id of the deleted record
or appropriate status code indicating reason for failure
'''
@app.route('/actors/<int:id>', methods=['DELETE'])
@requires_auth('delete:actors')
def delete_actor(jwt, id):
actor = Actor.query.filter(Actor.id == id).one_or_none()
if actor:
try:
actor.delete()
return jsonify({
'success': True,
'delete': id,
})
except BaseException:
abort(422)
else:
abort(404)
# Error Handling
'''
Create error handlers for all expected errors
including 400, 404, 422 and 500.
'''
@app.errorhandler(400)
def bad_request(error):
return jsonify({
"success": False,
"error": 400,
"message": "bad request"
}), 400
@app.errorhandler(404)
def not_found(error):
return jsonify({
"success": False,
"error": 404,
"message": "resource not found"
}), 404
@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"error": 422,
"message": "unprocessable"
}), 422
@app.errorhandler(500)
def internal_server_error(error):
return jsonify({
"success": False,
"error": 500,
"message": "internal server error"
}), 500
'''
Implement error handler for AuthError
'''
@app.errorhandler(AuthError)
def handle_auth_error(exception):
return jsonify({
"success": False,
"error": exception.status_code,
"message": exception.error,
}), 401
return app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)