1

Good Evening,

I am having some serious troubles getting flask-restful to work for me, It should be very simple and has been in the past but I am trying to load my libraries in a different format and I keep running into this error.

I am very new to python so I am sure I am making some simple mistake.

I am basing my structure and loading dynamic off this skeleton https://github.com/imwilsonxu/fbone

The basics are this In my extensions file I have this defined

from flask.ext import restful
api= restful.Api()

Then within my app.py file am doing this

app = Flask(app_name, instance_path=INSTANCE_FOLDER_PATH, instance_relative_config=True)
configure_app(app, config)
configure_blueprints(app, blueprints)
configure_extensions(app)


def configure_extensions(app):
  # Flask-restful
  api.init_app(app)

Then finally within a given blue print I am importing the api and trying there hello world example

from sandbox.extensions import api

class HelloWorld(restful.Resource):
def get(self):
    return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

This is the error I am getting.

AttributeError: 'Api' object has no attribute 'endpoints'

any help would be greatly appreciated.

1
  • 1
    I dont think you have posted all of your code... where do you reference endpoints? Commented May 16, 2013 at 0:25

1 Answer 1

3

The reason you are getting this error is because you are trying to add the flask-restful resources to the api object before it has reference to a valid instance of the Flask app.

One way to get around this would be to wrap all your add_resource calls in a separate function and then call this function after the app and the extensions have been initialized.

In your blueprint -

from sandbox.extensions import api

class HelloWorld(restful.Resource):
def get(self):
    return {'hello': 'world'}

def add_resources_to_helloworld():
    """ add all resources for helloworld blueprint """
    api.add_resource(HelloWorld, '/')

In app.py

def configure_extensions(app):
  # initialize api object with Flask app
  api.init_app(app)

  # add all resources for helloworld blueprint
  add_resources_to_helloworld()

This will make sure the resources are added to your app only after the api object has reference to an initialized Flask app ie. after init_app(app) is called.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.