I used Flask-Restful in a project where i also use the Factory pattern to create Flask objects. The problem now is that Flask give me 404 error when i try to reach http://localhost:5000/api/v1/user/ but when i explore (via the debugger) the Flask app object's url_map, my API rule is there. So, if someone ever had the same issue, i'm taking whatever possible solution.
I have the following function creating the API app:
def create_app(settings_override=None):
"""
Returns the API :class:`Flask` instance.
:param settings_override: dictionary of settings to override.
"""
app = factory.create_app(__name__, __path__, settings_override)
api = Api(app, prefix='/api/v1', catch_all_404s=True)
# API endpoints connected to the User model.
api.add_resource(UserAPI, '/user/', endpoint='user')
return app
The code of UserAPI class (used by Flask-Restful):
class UserAPI(Resource):
"""
API :class:`Resource` for returning the details of a user.
This endpoint can be used to verify a user login credentials.
"""
def get(self):
return {'hello': 'world'}, 200
def post(self):
pass
The factory.create_app function:
def create_app(package_name, package_path, settings_override=None):
"""
Returns an instance of Flask configured with common functionnalities for
Cubbyhole.
:param package_name: application package name
:param package_path: application package path
:param settings_override: a dictionnary of settings to override
"""
app = Flask(package_name, instance_relative_config=True)
app.config.from_object('cubbyhole.settings')
app.config.from_pyfile('settings.cfg', silent=True)
if settings_override is not None:
app.config.update(settings_override)
db.init_app(app)
register_blueprints(app, package_name, package_path)
return app
Python version 2.7 Flask v. Flask-Restful version
python 2.7,flask,werkzeug,flask-restful. Maybe you have environment orfactory.create_appissues.Flask(__name__)instead offactory.create_appand it works for me too. I'm now debugging thefactory.create_appfunction but have no idea where the issue could come from, the function does nothing special ... (I added the function's code to the post)__path__variable, but probably issue in your config, database or blueprints initialization.