简体   繁体   中英

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?

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. The Flask-RESTful documentation specifies defining HTTP methods in your Resource class eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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