简体   繁体   English

将多个端点从多个文件添加到 Flask-RESTplus 命名空间中

[英]Adding multiple endpoints into a Flask-RESTplus namespace from multiple files

I am working with Flask-Resplus API我正在使用 Flask-Resplus API

I want to create multiple endpoints into a single namespace.我想在一个命名空间中创建多个端点。 That is simple, but if I want to split the code to add endpoints into multiple files, getting issues there.这很简单,但如果我想拆分代码以将端点添加到多个文件中,则会出现问题。

Following is my app file:以下是我的应用程序文件:

from flask_restplus import Namespace, Api
from flask import Blueprint

from test_controller1 import test_ns


blueprint = Blueprint('api', __name__)

api = Api(blueprint,
          title='Test API',
          version='1.0',
          description='Test API',
          )

api.add_namespace(test_ns, path='/test')

test_controller1.py test_controller1.py

@test_ns.route("/test1")
class Test(Resource):
    def put(self):
        pass

test_controller2.py test_controller2.py

from test_controller1 import test_ns

@test_ns.route("/test2")
class Test(Resource):
    def get(self):
        pass

If I import test_ns from test_controller_1, only test1 endpoint will be added in the namespace.如果我从 test_controller_1 导入 test_ns,则只会在命名空间中添加 test1 端点。

How can I add both the endpoints(available in different files) in the same namespace?如何在同一个命名空间中添加两个端点(在不同的文件中可用)?

This can be done by defining Namespace(with the same name) across the classes.这可以通过跨类定义命名空间(具有相同名称)来完成。

test_controller1.py test_controller1.py

test_ns1 = Namespace("test", "Namespace for test")

test_controller2.py test_controller2.py

test_ns2 = Namespace("test", "Namespace for test")

Add both the namespaces to the blueprint.将这两个命名空间添加到蓝图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM