微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

flask-api-spec标签,用于在swagger

如何解决flask-api-spec标签,用于在swagger

我尝试将我的应用程序服务大范围地分组。在我的项目中,我使用FlaskApiSpec为我的Python-Flask应用程序生成Swagger文档。

有一部分代码,如何使用Flask Blueprint模式为我的应用程序生成详尽的文档:

application / init .py

from flask import Flask
from flask_sqlalchemy import sqlAlchemy
from flask_apispec.extension import FlaskApiSpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec import APISpec

db = sqlAlchemy()
docs = FlaskApiSpec()

def create_app():
   app = Flask(__name__)

   app.config.update({
        'APISPEC_SPEC': APISpec(
            title = 'D&D Master Screen',version = 'v0.1',openapi_version = '2.0',plugins = [MarshmallowPlugin()],),'APISPEC_SWAGGER_URL': '/api/swagger/','APISPEC_SWAGGER_UI_URL': '/api/swagger-ui/'
    })
    # Import Blueprints
    from .characters.views import characters_bp
    from .main.views import main_bp

    # Registrate Blueprints
    app.register_blueprint(characters_bp,url_prefix='/api/characters')
    app.register_blueprint(main_bp,url_prefix='/')

    # Initialize Plugins
    db.init_app(app)
    docs.init_app(app)

    return app

application / characters / views.py

from flask import Blueprint
# from application.characters import characters_bp
from flask import jsonify
from flask_apispec import use_kwargs,marshal_with
from application import docs
from application.models import  db,db_add_objects,db_delete_objects,Character
from application.schemas import CharacterSchema,ErrorSchema,CharacterIdSchema

characters_bp = Blueprint('characters_bp',__name__)


@characters_bp.route("/",methods=['GET'])
@marshal_with(CharacterSchema(many = True),code=200)
def characters():
    characters = Character.get_all()
    if characters is None:
        return {"message": "Characters not found"},404
    return characters


@characters_bp.route("/<character_id>",methods=['GET'])
@marshal_with(CharacterSchema,code=200)
@marshal_with(ErrorSchema,code=404)
def character(character_id):
    character = Character.get(character_id)
    if character is None:
        return {"message": str("Character with id=" + character_id + " not found")},404
    return character
    

@characters_bp.route("/",methods=['POST'])
@use_kwargs(CharacterSchema)
@marshal_with(CharacterIdSchema,code=200)
def create_character(**kwargs):
    new_character = Character(**kwargs)
    db_add_objects(new_character)
    return {"id": new_character.id}
    

@characters_bp.route("/<character_id>",methods=['DELETE'])
@marshal_with(ErrorSchema,code=404)
def delete_character(character_id):
    character = Character.get(character_id)
    if character is None:
        return {"message": str("Character with id=" + character_id + " not found")},404
    db_delete_objects(character)
    return jsonify({"message": "success"})



# Swagger docs for Characters Module services
blueprint_name = characters_bp.name
docs.register(character,blueprint = blueprint_name)
docs.register(characters,blueprint = blueprint_name)
docs.register(create_character,blueprint = blueprint_name)
docs.register(delete_character,blueprint = blueprint_name)

结果是 enter image description here

我想将我的/ api / characters方法分为一组,并正确命名。我尝试在Internet上找到很多东西,并从Swagger了解有关标签的一些知识。但我不明白,如何在FlaskApiSpec中使用此功能

我想可以在此处添加标签

docs.register(character,blueprint = blueprint_name)"

但是不知道如何...

解决方法

我也尝试了很长时间... 对于我有效的方法:只需修饰您的功能

 @docs(tags=['characters'])
,

是的,您可以使用 :

在同一个标​​签下分配多个方法

@docs(tags=['characters'])

对要在标签名称下显示的所有方法使用相同的标签名称。您还可以在标签中分别为每个方法添加说明。

试试这个:

@doc(description='Describing GET method',tags=['charavters'])
def get():
    <code here>


@doc(description='Describing POST method',tags=['charavters'])
def post():
    <code here>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。