def list_behaviors(self): AppLogging.info("behaviors") behaviors = self.behavior_repository.get_available_behaviors() body = json.dumps(behaviors) tron_response = TronResponse(body) return tron_response
def help_message(): AppLogging.info("help") body = ''' API Usage: - GET /api/validateMessage\n - GET /api/inventory\n - GET /api/inventory/<id>\n ''' tron_response = TronResponse(body) return tron_response
def get_inventory(self): BaseHandler(self.app_config).ensure_app_is_started() AppLogging.info("get_inventory") handle_behaviors_pre(self.behavior_repository, self.http_utils.get_demo_http_headers()) data = self.datastore.get_records() tron_response = self.createResponseFunc(data, self.app_config, self.http_utils) urls = self.app_config.get_dependency_endpoint('/api/inventory') if urls is not None: for url in urls: if url is not None: downstream_response = self.http_utils.query_service(url) tron_response.append_trace(self.http_utils, downstream_response) handle_behaviors_post(self.behavior_repository, self.http_utils.get_demo_http_headers()) return tron_response
def inventory_repository_selector(app_config=None): if app_config == None: from dependency_injection_container import Container app_config = Container().app_config() database_options = app_config.get_app_config_value('database') option_values = [ database_options.get(i, '') for i in ['user', 'password', 'host', 'port', 'name'] ] # false if there exists an option with no value, true if all options have values use_database = not ('' in option_values) selection = "database" if use_database else "file" AppLogging.info(f"Using inventory repository type: {selection}") return selection
def validate(self): BaseHandler(self.app_config).ensure_app_is_started() AppLogging.info("validate_message") handle_behaviors_pre(self.behavior_repository, self.http_utils.get_demo_http_headers()) data = {"result": True} tron_response = self.createResponseFunc(data, self.app_config, self.http_utils) urls = self.app_config.get_dependency_endpoint('/api/validateMessage') if urls is not None: for url in urls: if url is not None: downstream_response = self.http_utils.query_service(url) tron_response.append_trace(self.http_utils, downstream_response) handle_behaviors_post(self.behavior_repository, self.http_utils.get_demo_http_headers()) return tron_response
container = Container() app_config = container.app_config() container.config.from_dict(app_config.asdict()) container.wire(modules=[sys.modules[__name__]]) arguments = container.arguments() AppLogging.init(arguments.logging_level) if inventory_repository_selector(app_config) == 'database': container.setup_database_action().execute() database_connector = container.database_connector() http_utils = container.http_utils() inventory = container.inventory_handler() message = container.message_handler() behaviors = container.behaviors_handler() index = container.index_handler() debug_mode = arguments.debug_mode if debug_mode is not None and debug_mode == 'On': os.environ["FLASK_ENV"] = "development" port = int(app_config.get_app_port()) AppLogging.info("Listening on port: " + str(port)) AppLogging.info(index.get_message()) app.run(use_debugger=True, use_reloader=False, threaded=True, host='0.0.0.0', port=port)
def execute(self): DB_NAME = self.__database_connection_info.database # remove this since we dont know if database exists yet. self.__database_connection_info.database = None cnx = self.__database_connector.connect(**self.__database_connection_info.asdict()) cursor = cnx.cursor() def create_database(cursor): try: cursor.execute( "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME)) except mysql.connector.Error as err: AppLogging.error("Failed creating database: {}".format(err)) exit(1) try: cursor.execute("USE {}".format(DB_NAME)) AppLogging.info("Database already exists") except mysql.connector.Error as err: AppLogging.info("Database {} does not exists.".format(DB_NAME)) if err.errno == errorcode.ER_BAD_DB_ERROR: create_database(cursor) AppLogging.info("Database {} created successfully.".format(DB_NAME)) cnx.database = DB_NAME TABLES = {} TABLES['inventory'] = ( "CREATE TABLE `inventory` (" " `id` int(11) NOT NULL AUTO_INCREMENT," " `item` varchar(14) NOT NULL," " `sku` varchar(80) NOT NULL," " `price` varchar(14) NOT NULL," " PRIMARY KEY (`id`)" ") ENGINE=InnoDB") AppLogging.info("Creating inventory table . . .") for table_name in TABLES: table_description = TABLES[table_name] try: AppLogging.info("Creating table {}: ".format(table_name)) cursor.execute(table_description) except mysql.connector.Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: AppLogging.error("table already exists.") else: AppLogging.error(err.msg) else: AppLogging.info("OK") AppLogging.info("Inserting inventory data . . .") add_inventory_item = ("INSERT INTO inventory " "(item, price, sku) " "VALUES (%(item)s, %(price)s, %(sku)s)") for item in self.__inventory_data: cursor.execute(add_inventory_item, item) cnx.commit() cursor.close() cnx.close() else: AppLogging.error(err) exit(1)
def index_message(self): AppLogging.info("index") body = self.get_message() tron_response = TronResponse(body) return tron_response