Search code examples
pythonflaskflask-restplus

getting error 'function' object has no attribute 'as_view' while trying to run flask app


I started writing flask app after a long time more than a year, guess I have forgot something. This below code results in an error:

from flask import Flask
from flask import jsonify

from flask_restplus import Resource, Api

from home_iot.config import reader
from download_audio.ydla import download


app = Flask(__name__)

_api = Api(app, catch_all_404s=True, version=0.1,
          title="REST HTTP API's Gateway",
          descrition="REST API gateway")


api_ns = _api.namespace("iot", description="API.")


@api_ns.route("/tcpserver", methods=["GET"])
def advertise_tcpserver():
    ip = reader.get_server_ip()
    return jsonify({"tcpserver": ip})


if __name__ == "__main__":
    app.run(host='127.0.0.1')

Error is:

$ python app.py

Traceback (most recent call last):
  File "app.py", line 29, in <module>
    @api_ns.route("/tcpserver", methods=["GET"])
  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/namespace.py", line 98, in wrapper
    self.add_resource(cls, *urls, **kwargs)
  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/namespace.py", line 87, in add_resource
    api.register_resource(self, resource, *ns_urls, **kwargs)
  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/api.py", line 264, in register_resource
    self._register_view(self.app, resource, namespace, *urls, **kwargs)
  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/api.py", line 287, in _register_view
    resource_func = self.output(resource.as_view(endpoint, self, *resource_class_args,
AttributeError: 'function' object has no attribute 'as_view'

Solution

  • Hope this can helps those who have this same error and have not found the solution

    To complete the answer given by @v25 you must provide ressources to your namespace by inheriting from Ressource class in flask_restplus.

    The following example works for me

    Environment:

    • ubuntu 18.04
    • python 3.7.1

    python requirements:

    • flask==1.1.2
    • flask-restplus==0.13.0
    • werkzeug==0.16.1

    Source code: iot.py

    from flask_restplus import Namespace,Resource
    
    api_ns = Namespace("iot", description="API.")
    
    @api_ns.route("/tcpserver")
    class AdvertiseTcpserver(Resource):
        def get(self):
            #TODO return the correct ip value
            return {"tcpserver": "ip"}
    

    app.py

    from .iot import api_ns
    from flask import Flask
    from flask_restplus import Api
    
    app = Flask(__name__)
    
    _api = Api(app, catch_all_404s=True, version=0.1,
          title="REST HTTP API's Gateway",
          descrition="REST API gateway")
    
    _api.add_namespace(api_ns, path='/some/prefix')
    
    app.run()
    

    Test command:

    #!/bin/sh
    wget localhost:5000/some/prefix/tcpserver
    

    Please let me know if this helped.