How to use the safrs.jsonapi.Api function in safrs

To help you get started, we’ve selected a few safrs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github thomaxxl / safrs / examples / v1_examples / demo_flask_admin.py View on Github external
API_PREFIX = "/api"
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)
    logging.getLogger(__name__).setLevel(logging.DEBUG)
    builtins.log = log

    with app.app_context():
        # Create a user and a book and add the book to the user.books relationship
        user = User(name="thomas", email="em@il")
        book = Book(name="test_book")
        user.books.append(book)
        db.session.add(user)
        db.session.add(book)
        db.session.commit()  # COMMIT because SAFRSBase.db_commit = False

        api = Api(app, api_spec_url=API_PREFIX + "/swagger", host="{}:{}".format(HOST, PORT))
        # Expose the database objects as REST API endpoints
        api.expose_object(User)
        api.expose_object(Book)
        # Set the JSON encoder used for object to json marshalling
        app.json_encoder = SAFRSJSONEncoder

        @app.route("/")
        def index():
            """Create a redirect from / to /api"""
            return """<ul><li><a href="admin">admin</a></li><li><a href="api">api</a></li>"""

        # Register the API at /api/docs
        swaggerui_blueprint = get_swaggerui_blueprint(API_PREFIX, API_PREFIX + "/swagger.json")
        app.register_blueprint(swaggerui_blueprint, url_prefix=API_PREFIX)
        print("Starting API: http://{}:{}{}".format(HOST, PORT, API_PREFIX))
        app.run(host=HOST, port=PORT)
</ul>
github thomaxxl / safrs / examples / v1_examples / demo_json.py View on Github external
def create_api(app):

    api = Api(app, api_spec_url="/api/swagger", host="{}:{}".format(HOST, PORT), schemes=["http"])
    # Expose the User object
    api.expose_object(User)
    user = User(name="test", email="em@il", json={"test": "data"})

    # Set the JSON encoder used for object to json marshalling
    app.json_encoder = SAFRSJSONEncoder
    # Register the API at /api/docs
    swaggerui_blueprint = get_swaggerui_blueprint("/api", "/api/swagger.json")
    app.register_blueprint(swaggerui_blueprint, url_prefix="/api")

    print("Starting API: http://{}:{}/api".format(HOST, PORT))
    app.run(host=HOST, port=PORT)
github thomaxxl / safrs / examples / v1_examples / demo_relationship_mysql.py View on Github external
logging.getLogger(__name__).setLevel(logging.DEBUG)
    builtins.log = log
    # prevent redirects when a trailing slash isn't present
    app.url_map.strict_slashes = False

    with app.app_context():
        # Create a user and a book and add the book to the user.books relationship

        user = User(name="thomas", email="em@il")
        book = Book(name="test_book")

        for i in range(100):
            user = User(name="test_name_" + str(i))
            user.books.append(book)

        api = Api(app, api_spec_url=API_PREFIX + "/swagger", host="{}:{}".format(HOST, PORT))
        # Expose the database objects as REST API endpoints
        api.expose_object(User)
        api.expose_object(Book)
        # Set the JSON encoder used for object to json marshalling
        app.json_encoder = SAFRSJSONEncoder

        @app.route("/")
        def goto_api():
            """Create a redirect from / to /api"""
            return redirect(API_PREFIX)

        # Register the API at /api/docs
        swaggerui_blueprint = get_swaggerui_blueprint(API_PREFIX, API_PREFIX + "/swagger.json")
        app.register_blueprint(swaggerui_blueprint, url_prefix=API_PREFIX)
        print("Starting API: http://{}:{}{}".format(HOST, PORT, API_PREFIX))
        app.run(host=HOST, port=PORT)
github thomaxxl / safrs / examples / v1_examples / demo_relationship_ext.py View on Github external
log.setLevel(logging.DEBUG)
    logging.getLogger(__name__).setLevel(logging.DEBUG)
    builtins.log = log
    # prevent redirects when a trailing slash isn't present
    app.url_map.strict_slashes = False

    with app.app_context():
        # Create a user and a book and add the book to the user.books relationship
        user = User(name="thomas", email="em@il")
        book = Book(name="test_book")

        for i in range(100):
            user = User(name="test_name_" + str(i))
            user.books.append(book)

        api = Api(app, api_spec_url=API_PREFIX + "/swagger", host="{}:{}".format(HOST, PORT))
        # Expose the database objects as REST API endpoints
        api.expose_object(User)
        api.expose_object(Book)
        # Set the JSON encoder used for object to json marshalling
        app.json_encoder = SAFRSJSONEncoder

        @app.route("/")
        def goto_api():
            """Create a redirect from / to /api"""
            return redirect(API_PREFIX)

        # Register the API at /api/docs
        swaggerui_blueprint = get_swaggerui_blueprint(API_PREFIX, API_PREFIX + "/swagger.json")
        app.register_blueprint(swaggerui_blueprint, url_prefix=API_PREFIX)
        print("Starting API: http://{}:{}{}".format(HOST, PORT, API_PREFIX))
        print(dir(api))
github thomaxxl / safrs / examples / v1_examples / demo_cors.py View on Github external
def create_api(app):

    api = Api(app, api_spec_url="/api/swagger", host="{}:{}".format(HOST, PORT), schemes=["http"])
    # Expose the User object
    api.expose_object(User)
    user = User(name="test", email="em@il")

    # Set the JSON encoder used for object to json marshalling
    app.json_encoder = SAFRSJSONEncoder
    # Register the API at /api/docs
    swaggerui_blueprint = get_swaggerui_blueprint("/api", "/api/swagger.json")
    app.register_blueprint(swaggerui_blueprint, url_prefix="/api")

    print("Starting API: http://{}:{}/api".format(HOST, PORT))
    app.run(host=HOST, port=PORT)
github thomaxxl / safrs / examples / v1_examples / demo_hashid.py View on Github external
mailfile.write(content)
        return {"result": "sent {}".format(content)}


HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"
PORT = 5000


# Create the database
db.create_all()

with app.app_context():
    # Create a user
    user = User(name="test", email="em@il")

    api = Api(app, api_spec_url="/api/swagger", host="{}:{}".format(HOST, PORT), schemes=["http"])
    # Expose the User object
    api.expose_object(User)
    # Set the JSON encoder used for object to json marshalling
    app.json_encoder = SAFRSJSONEncoder
    # Register the API at /api/docs
    swaggerui_blueprint = get_swaggerui_blueprint("/api", "/api/swagger.json")
    app.register_blueprint(swaggerui_blueprint, url_prefix="/api")

    @app.route("/")
    def goto_api():
        return redirect("/api")

    print("Starting API: http://{}:{}/api".format(HOST, PORT))
    # app.run(host=HOST, port = PORT)
github thomaxxl / safrs / examples / v1_examples / expose_existing / skype_rest.py View on Github external
api.expose_object(sclass)


HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"
PORT = 5000

ma = Marshmallow(app)

# We need some cross-module global variables to be set
__builtin__.db = db
__builtin__.log = app.logger
__builtin__.ma = ma
# Create the database


api = Api(app, api_spec_url="/api/swagger", host="{}:{}".format(HOST, PORT), schemes=["http"])

with app.app_context():
    expose_tables()
# Expose the objects
# Set the JSON encoder used for object to json marshalling
app.json_encoder = SAFRSJSONEncoder
# Register the API at /api/docs
swaggerui_blueprint = get_swaggerui_blueprint("/api", "/api/swagger.json")
app.register_blueprint(swaggerui_blueprint, url_prefix="/api")


@app.route("/")
def goto_api():
    return redirect("/api")