Passed
Push — master ( 622bd5...daf764 )
by Mingyu
57s
created

{{cookiecutter.project_slug}}.app.views.route()   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
rs 9.9
c 0
b 0
f 0
cc 1
nop 1
1
from flask import Blueprint, Flask
2
from flask_restful import Api
3
4
5
def route(flask_app: Flask):
6
    from app.views.sample.api import SampleAPI
7
8
    handle_exception_func = flask_app.handle_exception
9
    handle_user_exception_func = flask_app.handle_user_exception
10
    # register_blueprint 시 defer되었던 함수들이 호출되며, flask-restful.Api._init_app()이 호출되는데
11
    # 해당 메소드가 app 객체의 에러 핸들러를 오버라이딩해서, 별도로 적용한 handler의 HTTPException 관련 로직이 동작하지 않음
12
    # 따라서 두 함수를 임시 저장해 두고, register_blueprint 이후 함수를 재할당하도록 함
13
14
    # - blueprint, api object initialize
15
    api_blueprint = Blueprint("api", __name__)
16
    api = Api(api_blueprint)
17
18
    # - route
19
    api.add_resource(SampleAPI, "/sample")
20
21
    # - register blueprint
22
    flask_app.register_blueprint(api_blueprint)
23
24
    flask_app.handle_exception = handle_exception_func
25
    flask_app.handle_user_exception = handle_user_exception_func
26