简体   繁体   English

创建 Flask 没有命名空间的 Restx 端点

[英]Create Flask Restx endpoints without namespaces

I'm working in the redesign of an API with Flask using Flask-restx, but I've a problem: We need a legacy version of the API that accepts the old urls, for compatibility reasons, but I'm not understanding how to do this since flask-restx requires a namespace to be declared.我正在使用 Flask-restx 重新设计 API 和 Flask,但我有一个问题:出于兼容性原因,我们需要接受旧网址的 API 的旧版本,但我不明白如何这样做是因为 flask-restx 需要声明一个命名空间。

Urls should be something like this:网址应该是这样的:

{{host}}/api/v1/art/savegallery <- new one
{{host}}/savegallery <- legacy

In Flask I've something like this:在 Flask 我有这样的东西:

app/ init .py应用程序/初始化.py

db = SQLAlchemy()
migrate = Migrate()
cors = CORS()


def create_app(config_class=DevelopmentConfig):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app=app)
    migrate.init_app(app=app, db=db)
    cors.init_app(app=app)

    from app.api import api_bp, legacy_bp
    app.register_blueprint(api_bp, url_prefix='/api/v1')
    app.register_blueprint(legacy_bp)

    return app

/app/api/ init .py /应用程序/API/初始化.py

api_bp = Blueprint('v1', __name__)
legacy_bp = Blueprint('legacy', __name__)

api_v1 = Api(
    app=api_bp,
    version='1.00',
    title='Art',
    description=(
        "API"
    ),
)

api_lgc = Api(
    app=legacy_bp,
    version='1.00',
    title='Art Legacy',
    description=(
        "API Legacy"
    ),
)



from app.art.routes import art_ns
api_v1.add_namespace(art_ns)
api_lgc.add_namespace(art_ns)

app/art/routes.py应用程序/艺术/routes.py

art_ns = Namespace(name='art', description='Art Storage')
#artlegacy_ns = Namespace(name='legacy', description='Art Storage')


@art_ns.route('/savegallery')
class GalleryAPI(Resource):
    def get(self):
        try:
            #data = request.json
            data = {}
            return {"foo":"bar"}, 200
        except Exception as e:
            print(e)
            return {"error": "Something happened"}, 500

With this I can access correctly to {{host}}/api/v1/art/savegallery but I'm not finding a way to declare the legacy one, since creating an url this way would require at least the namespace part of the url. Does Flask-restx has a way to declare those URLs and/or redirect the flow to the new ones?有了这个,我可以正确访问{{host}}/api/v1/art/savegallery但我找不到声明旧版的方法,因为以这种方式创建 url 至少需要 url 的命名空间部分. Flask-restx 是否有办法声明这些 URL 和/或将流重定向到新的 URL?

For anyone wondering how did I solve this, I just made a little trick:对于任何想知道我是如何解决这个问题的人,我只是做了一个小技巧:

/app/api/ init .py /应用程序/API/初始化.py

legacy_bp = Blueprint('legacy', __name__)

api_lgc = Api(
    app=legacy_bp,
    version='1.00',
    title='Art Legacy',
    description=(
        "API Legacy"
    ),
)

@legacy_bp.route('/savegallery/', methods=['POST'])
def saveGallery():
    return redirect('/api/art/savegallery/', code=307)

With this I drop the Namespace from the legacy API, and just declare every URL to redirect to the required one.有了这个,我从遗留的 API 中删除了命名空间,并且只声明每个 URL 以重定向到所需的一个。 It seems to work for now, but I'm not sure if this is the best solution, or if it might be something better, but for the moment this seems to be the answer.它现在似乎工作,但我不确定这是否是最好的解决方案,或者它是否可能更好,但目前这似乎是答案。

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

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