Created
October 2, 2022 18:08
-
-
Save thomaxxl/7f009114b0c86b7205611a60a0704abf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# run: | |
# $ FLASK_APP=mini_app flask run | |
from flask import Flask, request | |
from flask_sqlalchemy import SQLAlchemy | |
from safrs import SAFRSBase, SAFRSAPI | |
db = SQLAlchemy() | |
def post_hook(func): | |
def post_decorator(*args, **kwargs): | |
print(request.data) | |
result = func(*args, **kwargs) | |
print(result.response) | |
return result | |
if func.__name__ in ("post"): | |
return post_decorator | |
return func | |
class User(SAFRSBase, db.Model): | |
""" | |
description: My User description | |
""" | |
__tablename__ = "Users" | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String) | |
email = db.Column(db.String) | |
custom_decorators = [post_hook] | |
def create_api(app, host="localhost", port=5000, prefix=""): | |
api = SAFRSAPI(app, host=host, port=port, prefix=prefix) | |
api.expose_object(User) | |
user = User(name="test", email="email@x.org") | |
print(f"Starting API: http://{host}:{port}/{prefix}") | |
def create_app(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 | |
app = create_app() | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment