Skip to content

Instantly share code, notes, and snippets.

@thomaxxl
Last active March 16, 2021 11:36
Show Gist options
  • Save thomaxxl/def9cb5a849f3b508ec4aaac66f477c9 to your computer and use it in GitHub Desktop.
Save thomaxxl/def9cb5a849f3b508ec4aaac66f477c9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from safrs import SAFRSBase, SAFRSAPI, jsonapi_attr
db = SQLAlchemy()
class User(SAFRSBase, db.Model):
"""
description: User description
"""
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
email = db.Column(db.String)
@jsonapi_attr
def test_attr(self):
return 'blah'
@test_attr.setter
def test_attr(self, val):
print(f"Setting test_attr: '{val}'")
User.test_attr = test_attr
def create_api(app, host="localhost", port=5000, api_prefix=""):
api = SAFRSAPI(app, host=host, port=port, prefix=api_prefix)
api.expose_object(User)
user = User(name="test", email="email@x.org")
print(f"Created API: http://{host}:{port}/{api_prefix}")
def create_app(config_filename=None, host="localhost"):
app = Flask("demo_app")
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://")
db.init_app(app)
with app.app_context():
db.create_all()
create_api(app, host)
return app
host = "localhost"
app = create_app(host=host)
if __name__ == "__main__":
app.run(host=host)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment