简体   繁体   中英

Adding multiple endpoints into a Flask-RESTplus namespace from multiple files

I am working with 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_ns.route("/test1")
class Test(Resource):
    def put(self):
        pass

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.

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_ns1 = Namespace("test", "Namespace for test")

test_controller2.py

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

Add both the namespaces to the blueprint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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