def __init__(self, target, channel=None, for_rs_target=True): util.logger.spam(f"RestStubManager:init target({target})") if channel is None: channel = conf.LOOPCHAIN_DEFAULT_CHANNEL self.__channel_name = channel self.__should_update = True self.__target = target self.__version_urls = {} for version in conf.ApiVersion: if 'https://' in target: url = util.normalize_request_url(target, version, channel) elif for_rs_target: url = util.normalize_request_url( f"{'https' if conf.SUBSCRIBE_USE_HTTPS else 'http'}://{target}", version, channel) else: url = util.normalize_request_url(f"http://{target}", version, channel) self.__version_urls[version] = url self.__method_versions = { "Subscribe": conf.ApiVersion.node, "Unsubscribe": conf.ApiVersion.node, "GetChannelInfos": conf.ApiVersion.node, "AnnounceConfirmedBlock": conf.ApiVersion.node, "GetBlockByHeight": conf.ApiVersion.node, "Status": conf.ApiVersion.v1, "GetLastBlock": conf.ApiVersion.v3 } self.__method_names = { "Subscribe": "node_Subscribe", "Unsubscribe": "node_Unsubscribe", "GetChannelInfos": "node_GetChannelInfos", "AnnounceConfirmedBlock": "node_AnnounceConfirmedBlock", "GetBlockByHeight": "node_GetBlockByHeight", "Status": "/status/peer/", "GetLastBlock": "icx_getLastBlock" } self.__executor = ThreadPoolExecutor( max_workers=1, thread_name_prefix="RestStubThread")
def __init__(self, target, channel=None, for_rs_target=True): util.logger.spam(f"RestStubManager:init target({target})") self._channel_name = channel or conf.LOOPCHAIN_DEFAULT_CHANNEL self._version_urls = {} self._http_clients = {} for version in conf.ApiVersion: if 'https://' in target: url = util.normalize_request_url(target, version, self._channel_name) elif for_rs_target: url = util.normalize_request_url( f"{'https' if conf.SUBSCRIBE_USE_HTTPS else 'http'}://{target}", version, self._channel_name) else: url = util.normalize_request_url(f"http://{target}", version, self._channel_name) self._version_urls[version] = url if version != conf.ApiVersion.v1: self._http_clients[url] = HTTPClient(url) self._method_versions = { "GetChannelInfos": conf.ApiVersion.node, "GetBlockByHeight": conf.ApiVersion.node, "Status": conf.ApiVersion.v1, "GetLastBlock": conf.ApiVersion.v3 } self._method_names = { "GetChannelInfos": "node_getChannelInfos", "GetBlockByHeight": "node_getBlockByHeight", "Status": "/status/peer/", "GetLastBlock": "icx_getLastBlock" } self._executor = ThreadPoolExecutor( max_workers=1, thread_name_prefix="RestStubThread")
async def redirect_request_to_rs(message, rs_target, version=conf.ApiVersion.v3): method_name = 'icx_sendTransaction' rs_url = util.normalize_request_url( f"{'https' if conf.SUBSCRIBE_USE_HTTPS else 'http'}://{rs_target}", version) async with aiohttp.ClientSession() as session: result = await CustomAiohttpClient(session, rs_url).request( method_name, message) util.logger.spam( f"json_rpc_dispatcher:redirect_request_to_rs::{method_name}/{result}" ) return result
def _normalize_target(min_latency_target): normalized_target = utils.normalize_request_url(min_latency_target) return f"{urlparse(normalized_target).scheme}://{urlparse(normalized_target).netloc}"
def _create_jsonrpc_url(self, target: str, method: RestMethod): return utils.normalize_request_url(target, method.value.version, self._channel_name)
def _create_rest_url(self, target: str, method: RestMethod): url = utils.normalize_request_url(target, method.value.version, self._channel_name) url += method.value.name return url