def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) api.expose_object(User) # Create some users User(id=0, name="admin", email="*****@*****.**") User(id=1, name="test_user", email="*****@*****.**") print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def start_api(HOST="0.0.0.0", PORT=5000): OAS_PREFIX = "" # 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 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(): return redirect(OAS_PREFIX)
def start_app(app): custom_swagger = { "securityDefinitions": {"Bearer": {"type": "apiKey", "in": "header", "name": "Authorization"}}, "security": [{"Bearer": []}], } # Customized swagger will be merged api = SAFRSAPI(app, api_spec_url="/api/swagger", host=HOST, port=PORT, schemes=["http"], custom_swagger=custom_swagger) """username = "******" item = Item(name="item test") user = User(username=username, items=[item])""" 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 user = User.query.filter_by(username = "******").first() access_token = create_access_token(identity=user.username) print("Test Authorization header access_token: Bearer", access_token) app.run(host=HOST, port=PORT)
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="*****@*****.**") print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX)) admin = Admin(app, url="/admin") admin.add_view(sqla.ModelView(User, db.session))
def start_app(app): api = SAFRSAPI(app, host=HOST) api.expose_object(User) user = User(username="******") print("Starting API: http://{}:{}/api".format(HOST, PORT)) app.run(host=HOST, port=PORT)
def start_api(HOST="0.0.0.0", PORT=None): with app.app_context(): db.init_app(app) db.create_all() # populate the database for i in range(300): reader = Person(name="Reader " + str(i), email="reader_email" + str(i)) author = Person(name="Author " + str(i), email="author_email" + str(i)) book = Book(title="book_title" + str(i)) 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() swagger_host = HOST if PORT and PORT != 80: swagger_host += ":{}".format(PORT) custom_swagger = { "info": { "title": "New Title" }, "securityDefinitions": { "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "My-Cookie" } } } # 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)) # Create an API endpoint api.expose_object(model)
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX="", schemes=["http"]): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) api.expose_object(Server) print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def start_app(app): api = SAFRSAPI(app, host=HOST) # The method_decorators will be applied to all API endpoints api.expose_object(User, method_decorators=[auth.login_required]) user = User(username="******") print(f"Starting API: http://{HOST}:{PORT}/api") app.run(host=HOST, port=PORT)
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX, json_encoder=GeoJSONEncoder) api.expose_object(City) print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) api.expose_object(User) api.expose_object(Book) for i in range(200): user = User(name=f"user{i}", email=f"email{i}@dev.to") book = Book(name="test_book") user.books.append(book) print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def start_api(swagger_host="0.0.0.0", PORT=None): # Add startswith methods so we can perform lookups from the frontend SAFRSBase.startswith = startswith # Needed because we don't want to implicitly commit when using flask-admin SAFRSBase.db_commit = False with app.app_context(): db.init_app(app) db.create_all() # populate the database NR_INSTANCES = 200 for i in range(NR_INSTANCES): reader = Person(name="Reader " + str(i), email="reader@email" + str(i), password=hashlib.sha256(bytes(i)).hexdigest()) author = Person(name="Author " + str(i), email="author@email" + str(i)) book = Book(title="book_title" + str(i)) review = Review(reader_id=reader.id, book_id=book.id, review="review " + str(i)) publisher = Publisher(name="publisher" + str(i)) publisher.books.append(book) reader.books_read.append(book) author.books_written.append(book) reader.friends.append(author) author.friends.append(reader) if i % 20 == 0: reader.comment = "" 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=API_PREFIX, api_spec_url=API_PREFIX + "/swagger", custom_swagger=custom_swagger, schemes=["http", "https"], description=description, ) for model in [Person, Book, Review, Publisher]: # Create an API endpoint api.expose_object(model) # see if we can add the flask-admin views try: admin = Admin(app, url="/admin") for model in [Person, Book, Review, Publisher]: admin.add_view(sqla.ModelView(model, db.session)) except Exception as exc: print(f"Failed to add flask-admin view {exc}")
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) mesage1 = Message(receiver="user1", sender="user2", content="Hello") mesage2 = Message(receiver="user1", sender="user2", content="Hello") mesage3 = Message(receiver="user1", sender="user2", content="Hello") api.expose_object(Message) api.expose_object(User) user = User(name="test1") user = User(name="test2") user = User(name="test3") user = User(name="test4") print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def start_api(HOST='0.0.0.0', PORT=None): with app.app_context(): db.init_app(app) db.create_all() # populate the database for i in range(300): reader = Person(name='Reader ' + str(i), email='reader_email' + str(i)) author = Person(name='Author ' + str(i), email='author_email' + str(i)) book = Book(title='book_title' + str(i)) 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() swagger_host = HOST if PORT and PORT != 80: swagger_host += ':{}'.format(PORT) custom_swagger = { "info": { "title": "New Title" } } # 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)) # Create an API endpoint api.expose_object(model)
def start_app(app): OAS_PREFIX = "/api" # swagger location api = SAFRSAPI(app, host=HOST, 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='******') # user.hash_password('password') print("Starting API: http://{}:{}/api".format(HOST, PORT)) app.run(host=HOST, port=PORT)
def create_api(app, host="localhost", port=5000, api_prefix="", custom_swagger={}): """ The custom_swagger dictionary will be merged """ api = SAFRSAPI(app, host=host, port=port, prefix=api_prefix, custom_swagger=custom_swagger) api.expose_object(User) api.expose_object(Book) print(f"Created API: http://{host}:{port}/{api_prefix}")
def create_app(config_filename=None, host="localhost"): app = Flask("demo_app") app.secret_key = "not so secret" app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://") db.init_app(app) with app.app_context(): db.create_all() api = SAFRSAPI(app, host=host, port=5000) api.expose_object(Person) # Populate the db with users and a books and add the book to the user.books relationship for i in range(20): user = Person(name=f"user{i}", email=f"email{i}@email.com") return app
def start_api(swagger_host="0.0.0.0", PORT=None): with app.app_context(): db.init_app(app) db.create_all() # populate the database NR_INSTANCES = 200 for i in range(NR_INSTANCES): reader = Person(name="Reader " + str(i), email="reader@email" + str(i), password=str(i)) author = Person(name="Author " + str(i), email="author@email" + str(i), password=str(i)) book = Book(title="book_title" + str(i)) review = Review(reader_id=2 * i + 1, book_id=book.id, review=f"review {i}") publisher = Publisher(name="publisher" + str(i)) publisher.books.append(book) reader.books_read.append(book) author.books_written.append(book) reader.friends.append(author) author.friends.append(reader) if i % 20 == 0: reader.comment = "" for obj in [reader, author, book, publisher, review]: db.session.add(obj) db.session.commit() custom_swagger = { "info": {"title": "My Customized Title"}, "securityDefinitions": {"ApiKeyAuth": {"type": "apiKey", "in": "header", "name": "My-ApiKey"}}, } # Customized swagger will be merged api = SAFRSAPI( app, host=swagger_host, port=PORT, prefix=API_PREFIX, custom_swagger=custom_swagger, schemes=["http", "https"], description=description, ) for model in [Person, Book, Review, Publisher]: # Create an API endpoint api.expose_object(model) # add the flask-admin views admin = Admin(app, url="/admin") for model in [Person, Book, Review, Publisher]: admin.add_view(sqla.ModelView(model, db.session))
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='******') #user.hash_password('password') print('Starting API: http://{}:{}/api'.format(HOST, PORT)) app.run(host=HOST, port=PORT)
def create_api(app, HOST="localhost", PORT=5000, prefix=""): api_spec_url = f"/my_swagger" api = SAFRSAPI(app, host=HOST, port=PORT, prefix=prefix, swaggerui_blueprint=False, api_spec_url=api_spec_url) swaggerui_blueprint = get_swaggerui_blueprint( prefix, f"{prefix}/{api_spec_url}.json", config={ "docExpansion": "none", "defaultModelsExpandDepth": -1 }) app.register_blueprint(swaggerui_blueprint, url_prefix=prefix) api.expose_object(User) user = User(name="test", email="*****@*****.**") print(f"Starting API: http://{HOST}:{PORT}/{prefix}")
def create_api(app): with app.app_context(): api = SAFRSAPI(app) from src import models for model_name in models.__all__: api.expose_object(getattr(models, model_name)) # expect API objects to be accessed via HTTPS when in production if app.config.get("ENV").lower() == "production": api._swagger_object["schemes"] = ["https"] api._swagger_object["securityDefinitions"] = { "Bearer": { "type": "apiKey", "name": "Authorization", "in": "header" } }
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=api_prefix): # Add startswith methods so we can perform lookups from the frontend SAFRSBase.startswith = startswith # Needed because we don't want to implicitly commit when using flask-admin SAFRSBase.db_commit = False api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) for model in Models: dir(model) api.expose_object(model) # see if we can add the flask-admin views try: admin = Admin(app, name=app_environment, url="/admin", template_mode=app_bootstrap) for model in Models: admin.add_view(sqla.ModelView(model, db.session)) except Exception as exc: print(f"Failed to add flask-admin view {exc}") print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX="/api"): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX, json_encoder=SAFRSJSONEncoderExt) api.expose_object(models.User) api.expose_object(models.Book) api.expose_object(models.StoreModel) api.expose_object(models.ItemModel) print("Created API: http://{}:{}{}".format(HOST, PORT, API_PREFIX))
def start_api(swagger_host="0.0.0.0", PORT=None): # Add startswith methods so we can perform lookups from the frontend SAFRSBase.startswith = startswith # Needed because we don't want to implicitly commit when using flask-admin # SAFRSBase.db_commit = False with app.app_context(): db.init_app(app) db.create_all() custom_swagger = { "info": { "title": "My Customized Title" }, "securityDefinitions": { "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "My-ApiKey" } }, } # Customized swagger will be merged api = SAFRSAPI( app, host=swagger_host, port=PORT, prefix=API_PREFIX, custom_swagger=custom_swagger, schemes=["http", "https"], description=description, ) for model in [Person, Book, Review, Publisher]: # Create an API endpoint api.expose_object(model, method_decorators=[auth.login_required]) populate_db() print_db()
def create_api(app, swagger_host=None, swagger_port=5000): 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=swagger_port, custom_swagger=custom_swagger) api.expose_object(Thing) api.expose_object(SubThing) for i in range(30): secret = hashlib.sha256(bytes(i)).hexdigest() reader = Person(name="Reader " + str(i), email="reader_email" + str(i), password=secret) author = Person(name="Author " + str(i), email="author_email" + str(i)) book = Book(title="book_title" + str(i)) review = Review( reader_id=reader.id, book_id=book.id, review="review " + str(i) ) if i % 4 == 0: 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() for model in [Person, Book, Review, Publisher]: # Create an API endpoint api.expose_object(model)
def start_api(swagger_host="0.0.0.0", PORT=None): # Add startswith methods so we can perform lookups from the frontend SAFRSBase.startswith = startswith # Needed because we don't want to implicitly commit when using flask-admin SAFRSBase.db_commit = False with app.app_context(): db.init_app(app) db.create_all() # populate the database api = SAFRSAPI( app, host=swagger_host, port=PORT, prefix=API_PREFIX, api_spec_url=API_PREFIX + "/swagger", schemes=["http", "https"], description=description, ) api.expose_object(Servers) api.expose_object(Uploads)
@jsonapi_attr def name(self): return "My Name" @jsonapi_attr def my_custom_field(self): return -1 HOST = sys.argv[1] if len(sys.argv) > 1 else "localhost" PORT = 5000 app = Flask("SAFRS Demo Application") app.config.update(SQLALCHEMY_DATABASE_URI="sqlite:///", DEBUG=True) if __name__ == "__main__": db.init_app(app) db.app = app # Create the database db.create_all() API_PREFIX = "" with app.app_context(): test_obj = Test(id=1) api = SAFRSAPI(app, host=f"{HOST}", port=PORT, prefix=API_PREFIX) # Expose the database objects as REST API endpoints api.expose_object(Test) # Register the API at /api/docs print(f"Starting API: http://{HOST}:{PORT}{API_PREFIX}") app.run(host=HOST, port=PORT)
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="*****@*****.**") print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) api.expose_object(User) api.expose_object(Book) print("Created API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
def create_api(app, HOST="localhost", PORT=5010, API_PREFIX=""): api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) api.expose_object(Person) api.expose_object(Computer) print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX))
user0.books.append(book0) user1 = User(name='user1', email='em@il1') book1 = Book(name='test_book1', user=user1) book2 = Book(name='test_book2') 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)