How to use the safrs.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_full.py View on Github external
publisher = Publisher(name="name" + str(i))
            publisher.books.append(book)
            reader.books_read.append(book)
            author.books_written.append(book)
            db.session.add(reader)
            db.session.add(author)
            db.session.add(book)
            db.session.add(publisher)
            db.session.add(review)

        user = User(username="admin")
        user.hash_password("admin")
        db.session.add(user)
        db.session.commit()

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

        # Flask-Admin Config
        admin = Admin(app, url="/admin", index_view=MyAdminIndexView(), base_template="my_master.html")

        for model in [Person, Book, Review, Publisher, User]:
            # add the flask-admin view
            admin.add_view(sqla.ModelView(model, db.session))
            # Create an API endpoint
            api.expose_object(model)

        # Set the JSON encoder used for object to json marshalling
        app.json_encoder = SAFRSJSONEncoder
        # Register the API at /api
        swaggerui_blueprint = get_swaggerui_blueprint("/api", "/api/swagger.json")
        app.register_blueprint(swaggerui_blueprint, url_prefix="/api")
github thomaxxl / safrs / examples / authentication / demo_jwt.py View on Github external
def start_app(app):

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

    username = "admin"

    item = Item(name="test", email="em@il")
    user = User(username=username)

    api.expose_object(Item)
    api.expose_object(User)

    print("Starting API: http://{}:{}/api".format(HOST, PORT))

    # Identity can be any data that is json serializable
    access_token = create_access_token(identity=username)
    print("Test Authorization header access_token: Bearer", access_token)
    api._swagger_object["securityDefinitions"] = {"Bearer": {"type": "apiKey", "name": "Authorization", "in": "header"}}
github thomaxxl / safrs / examples / v1_examples / expose_existing / expose_sakila.py View on Github external
def start_api(HOST="0.0.0.0", PORT=80):

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

        # Get the SAFRSBase models from sakila
        for name, model in inspect.getmembers(sakila):
            bases = getattr(model, "__bases__", [])
            if SAFRSBase in bases:
                # Create an API endpoint
                api.expose_object(model)

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

        @app.route("/")
        def goto_api():
github thomaxxl / safrs / examples / v1_examples / demo_so.py View on Github external
def create_api(app):
    API_PREFIX = ""
    api = Api(app, api_spec_url=API_PREFIX + "/swagger", host="{}:{}".format(HOST, PORT))
    # Expose the User object
    app.json_encoder = SAFRSJSONEncoder
    swaggerui_blueprint = get_swaggerui_blueprint(API_PREFIX, API_PREFIX + "/swagger.json")
    app.register_blueprint(swaggerui_blueprint, url_prefix=API_PREFIX)

    api.expose_object(User)
    api.expose_object(Item)
    api.expose_object(Category)
    user = User(name="test", email="em@il")

    print("Starting API: http://{}:{}".format(HOST, PORT))
github thomaxxl / safrs / examples / v1_examples / expose_existing / expose_employees.py View on Github external
def start_api(HOST="0.0.0.0", PORT=80):

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

        # Get the SAFRSBase models from employees
        for name, model in inspect.getmembers(employees):
            bases = getattr(model, "__bases__", [])
            if SAFRSBase in bases:
                # Create an API endpoint
                api.expose_object(model)

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

        @app.route("/")
        def goto_api():
github thomaxxl / safrs / examples / v1_examples / demo_custom_decorators.py View on Github external
def start_app(app):

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

    item = Item(name="test", email="em@il")
    user = User(username="admin")

    api.expose_object(Item)
    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")

    print("Starting API: http://{}:{}/api".format(HOST, PORT))
    app.run(host=HOST, port=PORT)
github thomaxxl / safrs / examples / v1_examples / expose_existing / expose_mysql.py View on Github external
def start_api(HOST="0.0.0.0", PORT=80):

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

        # Get the SAFRSBase models from mysql
        for name, model in inspect.getmembers(mysql):
            bases = getattr(model, "__bases__", [])
            if SAFRSBase in bases:
                # Create an API endpoint
                api.expose_object(model)

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

        @app.route("/")
        def goto_api():
github thomaxxl / safrs / examples / v1_examples / demo_search.py View on Github external
def create_api(app):
    API_PREFIX = ""
    api = Api(app, api_spec_url=API_PREFIX + "/swagger", host="{}:{}".format(HOST, PORT))
    # Expose the User object
    app.json_encoder = SAFRSJSONEncoder
    swaggerui_blueprint = get_swaggerui_blueprint(API_PREFIX, API_PREFIX + "/swagger.json")
    app.register_blueprint(swaggerui_blueprint, url_prefix=API_PREFIX)

    api.expose_object(User)
    for i in range(100):
        user = User(name="test" + str(i), email="email@" + str(i))

    print("Starting API: http://{}:{}/".format(HOST, PORT))
github thomaxxl / safrs / examples / v1_examples / expose_existing / expose_models.py View on Github external
def start_api(HOST="0.0.0.0", PORT=80):

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

        # Get the SAFRSBase models from employees
        for name, model in inspect.getmembers(models):
            bases = getattr(model, "__bases__", [])
            if SAFRSBase in bases:
                # Create an API endpoint
                api.expose_object(model)

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

        @app.route("/")
        def goto_api():