"""Application. The API application is a `flask` application. It provides simple features such as registering a url for a specific handlers. """ from unittest.mock import MagicMock import connector_neo4j from flask import Flask from owslogger import flask_logger from owsrequest import flask_request from pythonfeatures import pythonfeatures from pythonfeatures.constants import split as split_constants from snowflake_connector.snowflake_conn import set_default_sessionmaker from socials import config app = Flask(config.SERVICE_NAME) flask_logger.setup( app, config.ENVIRONMENT, config.LOGGER_NAME, config.LOGGER_LEVEL, config.SERVICE_NAME, config.SERVICE_VERSION, exclude_paths=[config.HEALTH_CHECK], ) flask_request.setup(app, config.ENVIRONMENT) sf_config = { 'role': config.SNOWFLAKE_ROLE, 'account': config.SNOWFLAKE_ACCOUNT, 'user': config.SNOWFLAKE_USER, 'password': 'dummy_password', # we're using key pair auth, pk is passed in config.SNOWFLAKE_CONNECT_ARGS 'database': config.SNOWFLAKE_DATABASE, 'schema': config.SNOWFLAKE_SCHEMA, 'warehouse': config.SNOWFLAKE_WAREHOUSE, 'client_session_keep_alive': True, } if config.ENVIRONMENT == config.TEST_ENVIRONMENT: set_default_sessionmaker = MagicMock() else: # service-wide Snowflake connection pool settings set_default_sessionmaker( pool_size=config.SNOWFLAKE_POOL_SIZE, pool_recycle=config.SNOWFLAKE_POOL_RECYCLE, pool_pre_ping=config.SNOWFLAKE_POOL_PRE_PING, pool_reset_on_return=config.SNOWFLAKE_POOL_RESET_ON_RETURN, max_overflow=config.SNOWFLAKE_POOL_MAX_OVERFLOW, connect_args=config.SNOWFLAKE_CONNECT_ARGS, sf_config=sf_config ) if config.ENVIRONMENT == config.DEV_ENVIRONMENT: from flasgger import Swagger app.config['SWAGGER'] = { 'title': 'ows-socials Documentation', 'uiversion': 2, } try: Swagger(app, template_file=config.SWAGGER_FILE_PATH) except FileNotFoundError: print('Could not find swagger file at {}'.format( config.SWAGGER_FILE_PATH)) is_enabled = (pythonfeatures.get_single_feature_by_attributes( 'neo4j_aura', {'service': config.SERVICE_NAME} )).message == split_constants.FEATURE_ENABLED if is_enabled: neo4j_username = config.NEO4J_AURA_USERNAME neo4j_password = config.NEO4J_AURA_PASSWORD neo4j_url = config.NEO4J_AURA_URL else: neo4j_username = config.NEO4J_USERNAME neo4j_password = config.NEO4J_PASSWORD neo4j_url = config.NEO4J_URL connector_neo4j.configure( neo4j_url, neo4j_username, neo4j_password )