简体   繁体   English

flask python 创建 swagger 文档错误

[英]flask python creating swagger document error

I am trying to generate a swagger documentation for the API I am building in flask however when I execute below code I get very staring error, What I am missing here?我正在尝试为我在 flask 中构建的 API 生成 swagger 文档,但是当我执行以下代码时,我得到了非常严重的错误,我在这里缺少什么?

This is what I have tried:这是我尝试过的:

try:
    from flask import Flask, g, request
    from flask_restful import Resource, Api
    from flask_limiter.util import get_remote_address
    from flask_limiter import Limiter
    from flasgger import Swagger
    from flasgger.utils import swag_from
    from flask_restful_swagger import swagger
    from flask_httpauth import HTTPTokenAuth
    import os
    import json
except Exception as e:
    print("some modules are missing {}".format(e))  

app = Flask(__name__)
api = Api(app)
limiter = Limiter(app, key_func=get_remote_address)
limiter.init_app(app)
api = swagger.docs(Api(app), apiVersion='0.1', api_spec_url='/doc')

auth = HTTPTokenAuth(scheme='Token')

tokens = {
    'awserftggyjkk34)ghtyrjrhhye34nnmw': 'cadmin',
    'bwsosjhee(dhj345gtyuioplsertbsjkl': 'dadmin',
    'mnchthas(sjklertyusvfgmbshdls234h': 'eadmin'

}
@auth.verify_token
def verify_token(token):
   if token in tokens:
       g.current_user = tokens[token]
       return True
   return False

class defr(Resource):

    decorators = [limiter.limit("100/day")]
    @swagger.model
    @swagger.operation(notes='my notes ntes')
    @auth.login_required
    def currentuser(self):
        return "Hello, %s!" % g.current_user


api.add_resource(defr, '/user')

error:错误:

  File "C:\Users\codamd\AppData\Local\Continuum\anaconda3\lib\site-packages\flask_restful_swagger\swagger.py", line 294, in extract_operations
    for method in [m.lower() for m in resource.methods]:

TypeError: 'NoneType' object is not iterable

Any help would be great任何帮助都会很棒

The problem here is that you've used a custom named method in your defr class.这里的问题是您在defr class 中使用了自定义命名方法。 The Flask-RESTful documentation specifies defining HTTP methods in your Resource class eg Flask-RESTful 文档指定在您的Resource class 中定义 HTTP 方法,例如

class defr(Resource):

    decorators = [limiter.limit("100/day")]
    @swagger.model
    @swagger.operation(notes='my notes ntes')
    @auth.login_required
    def get(self):
        return "Hello, %s!" % g.current_user

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

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