0

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

John
  • 147
  • 1
  • 10

1 Answers1

0

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 e.g.

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

mtshaikh
  • 618
  • 4
  • 16
  • NOw I get below error after changing to HTTP method as `get` `TypeError: super(type, obj): obj must be an instance or subtype of type` – John Apr 09 '20 at 17:19
  • Can you show the complete stack trace for this error as I ran your code and I did not face any errors like this.. – mtshaikh Apr 09 '20 at 17:55
  • This is the new error : `AttributeError: 'Flask' object has no attribute 'cli'` – John Apr 09 '20 at 18:18
  • Also how can I pass the authorization token here ? – John Apr 09 '20 at 19:19
  • for the first error, installation of `click` or `flask-cli` might've been errorneous depending on the version oi Flask you're using. Check this [page](https://flask.palletsprojects.com/en/1.1.x/cli/) of Flask's documentation – mtshaikh Apr 10 '20 at 07:54
  • As for the authorization token, you can pass it using the `Authorization` Header e.g. `Authorization Token ` – mtshaikh Apr 10 '20 at 07:55
  • Thanks , Yup but for authorization how in swagger I can pass, so if you see in my code `localhost:5000/doc.html` is my swagger ui there how can I pass the Authorization – John Apr 10 '20 at 08:02
  • I don't think `flask-rest-swagger` provides that functionality as far as I know. The only relevant documentation I could find regarding this is [this issue](https://github.com/rantav/flask-restful-swagger/issues/100) and [this one](https://github.com/rantav/flask-restful-swagger/issues/73). However I would recommend using a REST client like Postman to access your APIs as swagger is just a specification document – mtshaikh Apr 10 '20 at 08:21
  • Agreed but Swagger is more that documentation :-) Why not use that as rest client as well along with documentation – John Apr 10 '20 at 08:27