在所有方法上实现通用装饰器

时间:2019-08-05 11:52:43

标签: python rest flask flask-restplus

尝试为该类上的所有方法创建自定义装饰器(该类为flask-restplus资源)

from flask_restplus import Resource, Api

test_blueprint = Blueprint('test', __name__)

test_api = Api(test_blueprint)

test_namespace = test_api.namespace('tests', description='Tests operations')

doc_messages = {
    200: "Success",
    400: "Validation Error",
    500: "Server Error"
}


@test_namespace.route("")

class TestApi(Resource):

      method_decorators = [test_namespace.doc(responses=doc_messages)]

      def get(self):
        return {"name": 2}


Not work like:

@test_namespace.route("")
class TestApi(Resource):
@test_namespace.doc(responses=doc_messages)
     def get(self):
        return {"name": 2}

结果令人不快,在第一个示例中我没有显示响应的描述,但在第二个示例中我看到了尽管它们应该相等。 我的目标是使用第二个示例创建应该在所有类方法中使用的更复杂的装饰器,但我发现没有重复的应对方法

0 个答案:

没有答案
相关问题