I am writting a RESTful API using the Flask framework and the Flask-RESTful plugin. I define my API classes on top of the Resource class that this plugin provides, as in the examples. However, when I want to register my classes using the add_resource method I get the following error:
AttributeError: type object 'UserAPI' has no attribute 'as_view'
The as_view method is part of the Flask Pluggable Views, this is, the View class. The Resource class is built on top of this class, and my UserAPI class on top of the Resource class. For this reason, the as_view method should be inherited, but it is not.
Any ideas on which could be the problem?
Here you can see how I defined the classes:
from app import api, db
from flask.ext.restful import Resource, abort
from models import *
class UserAPI(Resource):
def get(self, user_id):
user = User.query.filter(User.id == user_id)[0]
if user is None:
abort(404, message="User {} doesn't exist".format(user_id))
else:
return {'user' : user}, 201
api.add_resource(UserAPI, '/api/v0.1/users/<int:user_id>', endpoint = 'user')
EDIT:
This the full traceback:
Traceback (most recent call last):
File "/home/app/queries.py", line 35, in <module>
api.add_resource(UserAPI, '/api/v0.1/users/<int:user_id>', endpoint = 'user')
File "/home/app_env/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 344, in add_resource
self._register_view(self.app, resource, *urls, **kwargs)
File "/home/app_env/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 362, in _register_view
resource_func = self.output(resource.as_view(endpoint))
AttributeError: type object 'UserAPI' has no attribute 'as_view'
Here you can see how the View and Resource classes have the as_view method, while my UserAPI class has not:
>>> import inspect
>>> from flask.views import View
>>> inspect.getmembers(View, predicate=inspect.ismethod)
[('as_view', <bound method type.as_view of <class 'flask.views.View'>>), ('dispatch_request', <unbound method View.dispatch_request>)]
>>>
>>> from flask.ext.restful import Resource
>>> inspect.getmembers(Resource, predicate=inspect.ismethod)
[('as_view', <bound method MethodViewType.as_view of <class 'flask_restful.Resource'>>), ('dispatch_request', <unbound method Resource.dispatch_request>)]
>>>
>>> from app.queries import UserAPI
>>> inspect.getmembers(UserAPI, predicate = inspect.ismethod)
[('__init__', <unbound method UserAPI.__init__>), ('__repr__', <unbound method UserAPI.__repr__>), ('get', <unbound method UserAPI.get>)]
print dir(UserAPI)would be a good one, as well asprint UserAPI.mro(). Can you share the output of those two? A full trackback would be helpful too.