for my webapp I want to automatically generate a REST API. I read the of flask_restless docs, tried several tutorials and did everything as described:
my routes are to be located in module app.main.api.pv_tool.py
from . import api
from app.__init__ import db, app
from app.models import University
import flask_restless
manager = flask_restless.APIManager(app, session=db.scoped_session)
manager.create_api(University, methods=['GET'])
@api.route('/')
def index():
return 'asdf'
where University is defined in models.py
from sqlalchemy import String, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
...
class University(Base):
__tablename__ = 'unis'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(64), nullable=False, unique=True)
def __repr__(self):
return '<Uni %r>' % self.name
...
and the connection to the existing and filled database is established in database.py:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from app.models import Base
class Database:
path = 'sqlite:///pv_tool.sqlite'
engine = None
session = None
scoped_session = None
def __init__(self, path=path):
self.engine = create_engine(path, convert_unicode=True)
Base.metadata.bind = self.engine
db_session = sessionmaker(bind=self.engine, autocommit=False)
db_session.bind = self.engine
self.session = db_session()
self.scoped_session = scoped_session(self.session)
Base.metadata.create_all()
Now in my browser or using requests
library module I get 'asdf' response on http://localhost:5000.
However, I cannot reach my University data trying everything like: localhost:5000/unis, localhost:5000/api/unis and so on. I always get a 404 response.
Without any errors this issue is hard to pursue. Does anybody have an idea how I could debug this in general and what my error in the code might be?
In PyCharm Debugger the manager object seems to have some API to create:
But I have no idea what I could look for here next.
Also, I get an error on runtime when I try using create_api_blueprints in pv_tool.py:
...
manager = flask_restless.APIManager(app, session=db.scoped_session)
api_bp = manager.create_api_blueprint('unis', University, methods=['GET'])
app.register_blueprint(api_bp)
...
Traceback (most recent call last): File "run.py", line 5, in <module>
from app import app File "/Users/rich/code/src/github.com/pv-tool-backend/app/__init__.py", line 4, in <module>
from app.main.api import api File "/Users/rich/code/src/github.com/pv-tool-backend/app/main/api/__init__.py", line 5, in <module>
from . import pv_tool File "/Users/rich/code/src/github.com/pv-tool-backend/app/main/api/pv_tool.py", line 11, in <module>
api_bp = manager.create_api_blueprint('unis', University, methods=['GET']) File "/Users/rich/code/src/github.com/pv-tool-backend/pv_tool_backend_venv/lib/python3.6/site-packages/flask_restless/manager.py", line 549, in create_api_blueprint
restlessinfo = app.extensions['restless'] AttributeError: type object 'University' has no attribute 'extensions'
This is how I run the app:
run.py
from app import app
app.run(debug=True, host='0.0.0.0', port=5000)
app module's init.py
from flask import Flask
from flask_cors import CORS
from app.main.api import api
from app.database import Database
db = Database()
app = Flask(__name__)
app.register_blueprint(api)
CORS(app)
Starting in terminal:
rich@local:~ $ python run.py
- Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
- Restarting with stat
- Debugger is active!
- Debugger PIN: 122-170-707