我正试图为我在flask中构建的API生成一个swagger文档,但当我执行下面的代码时,我会出现一个非常严重的错误,这里缺少什么?
这就是我尝试过的:
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')
错误:
File "C:UserscodamdAppDataLocalContinuumanaconda3libsite-packagesflask_restful_swaggerswagger.py", line 294, in extract_operations
for method in [m.lower() for m in resource.methods]:
TypeError: 'NoneType' object is not iterable
任何帮助都将是伟大的
这里的问题是您在defr
类中使用了自定义命名方法。Flask RESTful文档指定在Resource
类中定义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