-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
74 lines (62 loc) · 2.11 KB
/
handlers.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
from flask import Flask, jsonify, redirect
from flask_restful import Api, MethodNotAllowed, NotFound
from flask_cors import CORS
from util.common import domain, port, prefix, build_swagger_config_json
from resources.swaggerConfig import SwaggerConfig
from resources.bookResource import BooksGETResource, BookGETResource, BookPOSTResource, BookPUTResource, BookDELETEResource
from flask_swagger_ui import get_swaggerui_blueprint
# ============================================
# Main
# ============================================
application = Flask(__name__)
app = application
app.config['PROPAGATE_EXCEPTIONS'] = True
CORS(app)
api = Api(app, prefix=prefix, catch_all_404s=True)
# ============================================
# Swagger
# ============================================
build_swagger_config_json()
swaggerui_blueprint = get_swaggerui_blueprint(
prefix,
f'http://{domain}:{port}{prefix}/swagger-config',
config={
'app_name': "Flask API",
"layout": "BaseLayout",
"docExpansion": "none"
},
)
app.register_blueprint(swaggerui_blueprint)
# ============================================
# Error Handler
# ============================================
@app.errorhandler(NotFound)
def handle_method_not_found(e):
response = jsonify({"message": str(e)})
response.status_code = 404
return response
@app.errorhandler(MethodNotAllowed)
def handle_method_not_allowed_error(e):
response = jsonify({"message": str(e)})
response.status_code = 405
return response
@app.route('/')
def redirect_to_prefix():
if prefix != '':
return redirect(prefix)
# ============================================
# Add Resource
# ============================================
# GET swagger config
api.add_resource(SwaggerConfig, '/swagger-config')
# GET books
api.add_resource(BooksGETResource, '/books')
api.add_resource(BookGETResource, '/books/<int:id>')
# POST book
api.add_resource(BookPOSTResource, '/books')
# PUT book
api.add_resource(BookPUTResource, '/books/<int:id>')
# DELETE book
api.add_resource(BookDELETEResource, '/books/<int:id>')
if __name__ == '__main__':
app.run(debug=True)