def send_request(self, method, url, data=None, data_type="json"): method = method.upper() has_payload = method == self.POST or method == self.PUT is_CUD = has_payload or method == self.DELETE full_url = urlparse.urlunparse((controller_address['scheme'], controller_address['host'] + ':' + str(controller_address['port']), url, None, None, None)) headers = { 'Content-Type': HttpClient.set_content_type(data_type) } request = HTTPRequest(url=full_url, method=method, headers=headers, auth_username=controller_auth['username'], auth_password=controller_auth['password'], connect_timeout=http_client_settings.get("timeouts", {}).get("connect", 3), request_timeout=http_client_settings.get("timeouts", {}).get("request", 10)) if has_payload: if data_type == "json": request.body = json.dumps(data) if is_CUD: if self.dry_run: logger.info("\nDRY RUN") logger.debug("\n\nSending {} request.\nUrl: {}\nBody: {}\n".format(method, full_url, request.body)) if is_CUD and self.dry_run: response = json.dumps({ "status": "ok", "msg": "dry_run" }) else: try: response = HTTPClient().fetch(request) if not self.fail_silently and not self.is_ok(response.code): raise HttpClientException(response) logger.debug("\n\nResponse ({}).\nUrl: {}\nBody: {}\n".format(response.code, full_url, response.body)) return response except HTTPError as e: logger.debug("HttpClient error: {}".format(e.message)) if not self.fail_silently: raise HttpClientException(e) return None return response
def connect_apps(main_app): apps = {} for app_name in list(map(format_app_name, INSTALLED_APPS)): try: importlib.import_module('.'.join([base_package_name, 'apps', app_name])) # init app topology_module = importlib.import_module('.'.join([base_package_name, 'apps', app_name, 'topology_parser'])) handler_module = importlib.import_module('.'.join([base_package_name, 'apps', app_name, 'handler'])) except ImportError as e: error = "Failed connecting app {}. Reason: {}".format(app_name, e.message) logger.error(error) else: app = { 'topology_parser': topology_module.Topology, 'handler': handler_module.Handler } apps[app_name] = app logger.info("Connected app: {}".format(app_name)) main_app.set_global_vars({'connected_apps': apps})
def connect_apps(main_app): apps = {} for app_name in list(map(format_app_name, INSTALLED_APPS)): try: importlib.import_module('.'.join( [base_package_name, 'apps', app_name])) # init app topology_module = importlib.import_module('.'.join( [base_package_name, 'apps', app_name, 'topology_parser'])) handler_module = importlib.import_module('.'.join( [base_package_name, 'apps', app_name, 'handler'])) except ImportError as e: error = "Failed connecting app {}. Reason: {}".format( app_name, e.message) logger.error(error) else: app = { 'topology_parser': topology_module.Topology, 'handler': handler_module.Handler } apps[app_name] = app logger.info("Connected app: {}".format(app_name)) main_app.set_global_vars({'connected_apps': apps})
from backend import app_connector from backend.handlers import MainHandler, TopologyHandler, AppRoutingHandler from backend.settings import settings, logger class BackendApp(tornado.web.Application): def __init__(self, handlers, **kwargs): super(BackendApp, self).__init__(handlers, **kwargs) def set_global_vars(self, variables): for key, value in variables.items(): setattr(self, key, value) def make_app(): return BackendApp([ (r"/(?P<app>[^\/]+)/topology/?$", TopologyHandler), (r"/(?P<app>[^\/]+)/(?P<path>.*)$", AppRoutingHandler), (r"/(.*)$", MainHandler), ], **settings) if __name__ == "__main__": logger.info("Starting app") app = make_app() app_connector.connect_apps(app) app.listen(9900) tornado.ioloop.IOLoop.current().start()