How to use the safrs.SAFRSAPI 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 / tests / relationship / demo_relationship.py View on Github external
user1 = User(name="user2", email="em@il2", books=[book2])


if __name__ == "__main__":
    HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"
    PORT = 5000
    app = Flask("SAFRS Demo Application")
    app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://", DEBUG=True)
    db.init_app(app)
    db.app = app
    # Create the database
    db.create_all()
    API_PREFIX = ""

    with app.app_context():
        api = SAFRSAPI(app, host="{}:{}".format(HOST, PORT), port=PORT, prefix=API_PREFIX)
        populate_db()
        # Expose the database objects as REST API endpoints
        api.expose_object(User)
        api.expose_object(Book)
        # Register the API at /api/docs
        print("Starting API: http://{}:{}{}".format(HOST, PORT, API_PREFIX))
        app.run(host=HOST, port=PORT)
github thomaxxl / safrs / examples / demo_pythonanywhere_com.py View on Github external
review = Review(reader_id=reader.id, book_id=book.id, review="review " + str(i))
            publisher = Publisher(name="name" + str(i))
            publisher.books.append(book)
            reader.books_read.append(book)
            author.books_written.append(book)
            for obj in [reader, author, book, publisher, review]:
                db.session.add(obj)

            db.session.commit()

        custom_swagger = {
            "info": {"title": "New Title"},
            "securityDefinitions": {"ApiKeyAuth": {"type": "apiKey", "in": "header", "name": "My-ApiKey"}},
        }  # Customized swagger will be merged

        api = SAFRSAPI(
            app,
            host=swagger_host,
            port=PORT,
            prefix=OAS_PREFIX,
            api_spec_url=OAS_PREFIX + "/swagger",
            custom_swagger=custom_swagger,
            schemes=["http", "https"],
            description=description,
        )

        # Flask-Admin Config
        admin = Admin(app, url="/admin")

        for model in [Person, Book, Review, Publisher]:
            # add the flask-admin view
            admin.add_view(sqla.ModelView(model, db.session))
github thomaxxl / safrs / examples / authentication / demo_auth.py View on Github external
def start_app(app):

    OAS_PREFIX = "/api"  # swagger location
    api = SAFRSAPI(app, host="{}:{}".format(HOST, PORT), schemes=["http"], prefix=OAS_PREFIX, api_spec_url=OAS_PREFIX + "/swagger")

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

    item = Item(name="test", email="em@il")
    # user = User(username='admin')
    # user.hash_password('password')

    print("Starting API: http://{}:{}/api".format(HOST, PORT))
    app.run(host=HOST, port=PORT)
github thomaxxl / safrs / examples / mini_app.py View on Github external
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("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
github thomaxxl / safrs / expose_existing / expose_existing.py View on Github external
def start_api(HOST="0.0.0.0", PORT=5000):

    OAS_PREFIX = "/api"  # swagger prefix
    with app.app_context():
        api = SAFRSAPI(
            app,
            host=HOST,
            port=PORT,
            prefix=OAS_PREFIX,
            api_spec_url=OAS_PREFIX + "/swagger",
            schemes=["http", "https"],
            description="exposed app",
        )

        for name, model in inspect.getmembers(models):
            bases = getattr(model, "__bases__", [])

            if SAFRSBase in bases:
                # Create an API endpoint
                # Add search method so we can perform lookups from the frontend
                model.search = search
github thomaxxl / safrs / examples / demo_http_method.py View on Github external
# Server configuration variables:
HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"
PORT = 5000
API_PREFIX = ""

# App initialization
app = Flask("SAFRS Demo Application")
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://", DEBUG=True)
db.init_app(app)
# Create the database

with app.app_context():
    db.create_all()
    # Create a user
    api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX)
    # Create a user, data from this user will be used to fill the swagger example
    user = User(name="thomas", email="em@il")
    # Expose the database objects as REST API endpoints
    api.expose_object(User)

with open("examples/jsonapi-schema.json") as sf:
    schema = json.load(sf)


@app.after_request
def per_request_callbacks(response):
    if response.headers["Content-Type"] != "application/json":
        return response
    try:
        data = json.loads(response.data.decode("utf8"))
        validate(data, schema)
github thomaxxl / safrs / examples / demo_relationship.py View on Github external
HOST = sys.argv[1] if len(sys.argv) > 1 else "0.0.0.0"
    PORT = 5000
    app = Flask("SAFRS Demo Application")
    app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://", DEBUG=True)
    db.init_app(app)
    db.app = app
    # Create the database
    db.create_all()
    API_PREFIX = ""

    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)
        api = SAFRSAPI(app, host="{}".format(HOST), port=PORT, prefix=API_PREFIX)
        # Expose the database objects as REST API endpoints
        api.expose_object(User)
        api.expose_object(Book)
        # Register the API at /api/docs
        print("Starting API: http://{}:{}{}".format(HOST, PORT, API_PREFIX))
        app.run(host=HOST, port=PORT)