def _handle_check_bridge_for_update(self, req: Request): """ Checks whether the remote, the bridge is currently running on, is an older version :param req: empty request :return: None """ try: self._validator.validate(req.get_payload(), "api_empty_request") except ValidationError: self._respond_with_error(req, "ValidationError", f"Request validation error at {ApiURIs.bridge_update_check.uri}") return encoder = ApiEncoder() try: updater = BridgeUpdateManager(os.getcwd()) bridge_meta = updater.check_for_update() except UpdateNotPossibleException: self._respond_with_error(req, "UpdateNotPossibleException", "bridge could not be updated") except NoUpdateAvailableException: self._respond_with_status(req, True, "Bridge is up to date") else: payload = encoder.encode_bridge_update_info(bridge_meta) req.respond(payload) return
def _handle_get_all_configs(self, req: Request): """ Responds with the names and descriptions of all available configs :param req: empty Request :return: None """ try: self._validator.validate(req.get_payload(), "api_empty_request") except ValidationError: self._respond_with_error(req, "ValidationError", f"Request validation error at '{ApiURIs.config_storage_get_all.uri}'") return manager = ClientConfigManager() config_names = manager.get_config_names() all_configs = {} self._logger.info("fetching all configs") for config in config_names: if not config == "Example": try: conf = manager.get_config(config) all_configs[config] = conf["description"] except ConfigDoesNotExistException: self._logger.error("congratz, something went wrong, abort, abort, return to the moon base") pass else: pass payload = {"configs": all_configs} req.respond(payload)
def receive(self, req: Request): if req.get_receiver() == self._network.get_hostname(): # Normal Request self._logger.info(f"Received Request at '{req.get_path()}'") elif req.get_receiver() is None: # Broadcast self._logger.info(f"Received Broadcast at '{req.get_path()}'") else: return req.respond(req.get_payload())
def _handle_get_config(self, req: Request): """ Responds with the config for a given name, if there is none an error is returned :param req: Request containing the name of the requested config :return: None """ try: self._validator.validate(req.get_payload(), "api_config_delete_get") except ValidationError: self._respond_with_error(req, "ValidationError", f"Request validation error at '{ApiURIs.config_storage_get.uri}'") return try: name = req.get_payload()["name"] config = ClientConfigManager().get_config(name) payload = {"config": config} req.respond(payload) except ConfigDoesNotExistException as err: self._respond_with_error(req=req, err_type="ConfigDoesNotExistException", message=err.args[0])
def _respond_with_status(self, req: Request, ack: bool, message: Optional[str] = None): out_payload = {"ack": ack, "message": message} req.respond(out_payload) self._logger.info(f"Responding with status: {ack} to request: {req.get_path()}")
def _respond_with_error(self, req: Request, err_type: str, message: str): message = message.replace("\"", "'") req.respond({"error_type": err_type, "message": message}) self._logger.error(f"{err_type}: {message}")
def _handle_info_clients(self, req: Request): data = self._delegate.get_client_info() encoder = ApiEncoder() resp_data = encoder.encode_all_clients_info(data) req.respond(resp_data)
def _handle_info_bridge(self, req: Request): data = self._delegate.get_bridge_info() encoder = ApiEncoder() resp_data = encoder.encode_bridge_info(data) req.respond(resp_data)