简体   繁体   English

挥舞Flask-Restplus,API和多个蓝图

[英]Swagger with Flask-Restplus, API and multiple Blueprints

I'm building a very complex microservice using Flask and Flask-Restplus. 我正在使用Flask和Flask-Restplus构建一个非常复杂的微服务。
It will have many endpoints, thus I'm organizing each endpoint into a separate Blueprint. 它将有许多端点,因此我将每个端点组织到一个单独的蓝图中。

  • Currently, I'm struggling with the usage of Flask-Restplus and API using multiple Blueprints in combination with swagger 目前,我在将Flask-Restplus和API与多个蓝图结合使用时苦苦挣扎
  • I want to be able to get all the endpoints of my blueprints into the built-in swagger of API, but this doesn't seem to work. 我希望能够将蓝图的所有端点都放入内置的API中,但这似乎不起作用。
  • I can access my endpoints via postman, but the swagger-UI doesn't show anything. 我可以通过邮递员访问端点,但是swagger-UI没有显示任何内容。 :( :(

The following example code and directory structure should give you a hint towards my idea: 以下示例代码和目录结构应向您提示我的想法:

.
├── endpoints
│   ├── endpointa.py
│   ├── endpointb.py
│   ├── endpointc.py
│   └── __init__.py
├── __init__.py
└── run.py

My main init .py looks like this: 我的主要init .py如下所示:

from flask import Flask, Blueprint, logging, jsonify, request, Response
from flask_restplus import Resource, Api   


# create app and api
app = Flask(__name__)
api_prefix  = '/api/v1/'

# register Blueprints
from endpoints.endpointa import endpointa_api
app.register_blueprint(endpointa_api, url_prefix=api_prefix)

from endpoints.endpointb import endpointb_api
app.register_blueprint(endpointb_api, url_prefix=api_prefix)

from endpoints.endpointc import endpointc_api
app.register_blueprint(endpointc_api, url_prefix=api_prefix)


api = Api(app,
          version='1',
          title='Test Service REST-API',
          description='A REST-API for the Test Service, implemented in python')


if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=5060)

endpointa.py with the corresponding Blueprint: 具有相应蓝图的endpointa.py:

from os import environ
import json, ast, syslog
import requests
import gc
from flask import Flask, Blueprint, logging, jsonify, request, Response
from flask_restplus import Resource, Api

endpointa_api = Blueprint('endpointa_api', __name__)

@endpointa_api.route('testa', methods=['GET'])
def testa():
    ...


@endpointa_api.route('testa/<string:testa_id>', methods=['GET', 'POST'])
def testa_id():
    ...

Again: 再次:

I can access my endpoints via postman, but the swagger-UI doesn't show anything: 我可以通过邮递员访问端点,但是swagger-UI没有显示任何内容:

在此处输入图片说明

Normally I would add endpoints to API using something like 通常我会使用类似的方法将端点添加到API

api.add_resource(TestClass, api_prefix + 'test')

but this doesn't seem to be possible with multiple Blueprints. 但这似乎无法通过多个蓝图实现。

Can anyone show me how to add/register these Blueprints (endpointa_api, endpointb_api and endpointc_api) with Api ? 谁能告诉我如何使用Api添加/注册这些蓝图(endpointa_api,endpointb_api和endpointc_api)?

There are 2 possible solutions using Flask-Restplus: 使用Flask-Restplus有2种可能的解决方案:

  • Use Flask-RestPlus namespaces 使用Flask-RestPlus命名空间
  • Turn your blueprints into Flask-RestPlus Api s 将您的蓝图变成Flask-RestPlus Api s

You can read about both in the documentation: https://flask-restplus.readthedocs.io/en/stable/scaling.html 您可以在文档中阅读有关这两个方面的信息: https : //flask-restplus.readthedocs.io/en/stable/scaling.html

Namespaces 命名空间

Flask-RESTPlus provides a way to use almost the same pattern as Flask's blueprint. Flask-RESTPlus提供了一种使用与Flask蓝图几乎相同的模式的方法。 The main idea is to split your app into reusable namespaces. 主要思想是将您的应用拆分为可重用的名称空间。

from flask_restplus import Api

from .namespace1 import api as ns1
from .namespace2 import api as ns2
# ...
from .namespaceX import api as nsX

api = Api(
    title='My Title',
    version='1.0',
    description='A description',
    # All API metadatas
)

api.add_namespace(ns1)
api.add_namespace(ns2)
# ...
api.add_namespace(nsX)

Blueprint Apis 蓝图阿皮斯

Here's an example of how to link an Api up to a Blueprint. 这是一个如何将Api链接到蓝图的示例。

from flask import Blueprint
from flask_restplus import Api

blueprint = Blueprint('api', __name__)
api = Api(blueprint)
# ...

Using a blueprint will allow you to mount your API on any url prefix and/or subdomain in you application: 使用蓝图可让您将API挂载在应用程序中的任何url前缀和/或子域上:

from flask import Flask
from apis import blueprint as api

app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api/1')
app.run(debug=True)

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

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