From 7ad42c993e296ea2f3f24f36e398d739016a43b0 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Fri, 28 Jun 2024 13:40:52 -0300 Subject: [PATCH] chore: allow checking http fullnodes (#326) --- .env.example | 4 +-- .github/workflows/deploy.yml | 12 ++++----- common/configuration.py | 2 +- gateways/clients/hathor_core_client.py | 26 +++++++++---------- serverless.yml | 2 +- .../clients/test_hathor_core_client.py | 12 ++++----- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/.env.example b/.env.example index 68efd8f..9f89b35 100644 --- a/.env.example +++ b/.env.example @@ -9,8 +9,8 @@ AWS_SUBNET_ID_3=subnet3 API_PORT=3001 LAMBDA_INVOKE_URL=http://explorer-service:3002/ DATA_AGGREGATOR_LAMBDA_NAME=hathor-explorer-service-dev-node_data_aggregator_handler -HATHOR_CORE_DOMAIN=node.explorer.testnet.hathor.network -HATHOR_NODES=node.explorer.testnet.hathor.network +HATHOR_CORE_URL=https://node.explorer.testnet.hathor.network +HATHOR_NODES=https://node.explorer.testnet.hathor.network REDIS_KEY_PREFIX=hathor-explorer-service-dev REDIS_HOST=redis REDIS_PORT=6379 diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5d36ab9..9b0b68d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -90,8 +90,8 @@ jobs: make deploy-lambdas-ci stage=dev env: API_PORT: 3001 - HATHOR_CORE_DOMAIN: node.explorer.testnet.hathor.network - HATHOR_NODES: node1.testnet.hathor.network,node.explorer.testnet.hathor.network + HATHOR_CORE_URL: https://node.explorer.testnet.hathor.network + HATHOR_NODES: https://node1.testnet.hathor.network,https://node.explorer.testnet.hathor.network REDIS_KEY_PREFIX: hathor-explorer-service-dev REDIS_HOST: ${{ secrets.REDIS_HOST }} REDIS_PORT: 6379 @@ -112,8 +112,8 @@ jobs: make deploy-lambdas-ci stage=testnet env: API_PORT: 3001 - HATHOR_CORE_DOMAIN: node.explorer.testnet.hathor.network - HATHOR_NODES: node1.testnet.hathor.network,node.explorer.testnet.hathor.network + HATHOR_CORE_URL: https://node.explorer.testnet.hathor.network + HATHOR_NODES: https://node1.testnet.hathor.network,https://node.explorer.testnet.hathor.network REDIS_KEY_PREFIX: hathor-explorer-service-testnet REDIS_HOST: ${{ secrets.REDIS_HOST }} REDIS_PORT: 6379 @@ -134,8 +134,8 @@ jobs: make deploy-lambdas-ci stage=mainnet env: API_PORT: 3001 - HATHOR_CORE_DOMAIN: node.explorer.hathor.network - HATHOR_NODES: node.explorer.hathor.network,node1.mainnet.hathor.network,node2.mainnet.hathor.network + HATHOR_CORE_URL: https://node.explorer.hathor.network + HATHOR_NODES: https://node.explorer.hathor.network,https://node1.mainnet.hathor.network,https://node2.mainnet.hathor.network REDIS_KEY_PREFIX: hathor-explorer-service-mainnet REDIS_HOST: ${{ secrets.REDIS_HOST }} REDIS_PORT: 6379 diff --git a/common/configuration.py b/common/configuration.py index fc11bca..2a2b555 100644 --- a/common/configuration.py +++ b/common/configuration.py @@ -56,7 +56,7 @@ def default(cls) -> "LogRenderer": DATA_AGGREGATOR_LAMBDA_NAME = config("DATA_AGGREGATOR_LAMBDA_NAME", default=None) -HATHOR_CORE_DOMAIN = config("HATHOR_CORE_DOMAIN", default=None) +HATHOR_CORE_URL = config("HATHOR_CORE_URL", default=None) HATHOR_NODES = config("HATHOR_NODES", default="", cast=Csv()) NODE_CACHE_TTL = config("NODE_CACHE_TTL", default=30) diff --git a/gateways/clients/hathor_core_client.py b/gateways/clients/hathor_core_client.py index f3d7b1a..762ca56 100644 --- a/gateways/clients/hathor_core_client.py +++ b/gateways/clients/hathor_core_client.py @@ -5,7 +5,7 @@ import aiohttp import requests -from common.configuration import HATHOR_CORE_DOMAIN +from common.configuration import HATHOR_CORE_URL from common.errors import HathorCoreTimeout from common.logging import get_logger @@ -33,13 +33,13 @@ class HathorCoreAsyncClient: DEFAULT_TIMEOUT = 60 # seconds - def __init__(self, domain: Optional[str] = None) -> None: + def __init__(self, url: Optional[str] = None) -> None: """Client to make async requests - :param domain: domain where the requests will be made, defaults to config `hathor_core_domain` - :type domain: str, optional + :param url: url where the requests will be made, defaults to config `hathor_core_url` + :type url: str, optional """ - self.domain = domain or HATHOR_CORE_DOMAIN + self.url = url or HATHOR_CORE_URL self.log = logger.new(client="async") async def get( @@ -54,7 +54,7 @@ async def get( :param timeout: timeout in seconds :type timeout: Optional[float] """ - url = parse.urljoin(f"https://{self.domain}", path) + url = parse.urljoin(self.url, path) if not timeout: timeout = self.DEFAULT_TIMEOUT @@ -88,7 +88,7 @@ async def post( :param timeout: timeout in seconds :type timeout: Optional[float] """ - url = parse.urljoin(f"https://{self.domain}", path) + url = parse.urljoin(self.url, path) if not timeout: timeout = self.DEFAULT_TIMEOUT @@ -114,12 +114,12 @@ async def post( class HathorCoreClient: """Client to make requests - :param domain: domain where the requests will be made, defaults to config `hathor_core_domain` - :type domain: str, optional + :param url: url where the requests will be made, defaults to config `hathor_core_url` + :type url: str, optional """ - def __init__(self, domain: Optional[str] = None) -> None: - self.domain = domain or HATHOR_CORE_DOMAIN + def __init__(self, url: Optional[str] = None) -> None: + self.url = url or HATHOR_CORE_URL self.log = logger.new(client="sync") def get_text( @@ -136,7 +136,7 @@ def get_text( :return: request response :rtype: Optional[str] """ - url = parse.urljoin(f"https://{self.domain}", path) + url = parse.urljoin(self.url, path) try: response = requests.get(url, params=params, **kwargs) @@ -172,7 +172,7 @@ def post_text( :return: request response :rtype: Optional[str] """ - url = parse.urljoin(f"https://{self.domain}", path) + url = parse.urljoin(self.url, path) try: response = requests.post(url, json=body, **kwargs) diff --git a/serverless.yml b/serverless.yml index db3d754..01260a4 100644 --- a/serverless.yml +++ b/serverless.yml @@ -31,7 +31,7 @@ provider: - ${env:AWS_SUBNET_ID_2} - ${env:AWS_SUBNET_ID_3} environment: - HATHOR_CORE_DOMAIN: ${env:HATHOR_CORE_DOMAIN} + HATHOR_CORE_URL: ${env:HATHOR_CORE_URL} HATHOR_NODES: ${env:HATHOR_NODES} REDIS_KEY_PREFIX: ${env:REDIS_KEY_PREFIX} REDIS_HOST: ${env:REDIS_HOST} diff --git a/tests/unit/gateways/clients/test_hathor_core_client.py b/tests/unit/gateways/clients/test_hathor_core_client.py index 6c1a9f8..efcc592 100644 --- a/tests/unit/gateways/clients/test_hathor_core_client.py +++ b/tests/unit/gateways/clients/test_hathor_core_client.py @@ -14,7 +14,7 @@ def test_get_text(self, mocked_get): expected = json.dumps({"success": True}) mocked_get.return_value.text = expected - client = HathorCoreClient("mydomain.com") + client = HathorCoreClient("https://mydomain.com") result = client.get_text("/some/path", {"page": 2}) @@ -29,12 +29,12 @@ def test_get(self, mocked_get): mocked_get.return_value.status_code = 200 mocked_get.return_value.text = json.dumps({"success": True}) - client = HathorCoreClient("mydomain.com") + client = HathorCoreClient("http://mydomain.com") result = client.get("/some/path", {"page": 2}) mocked_get.assert_called_once_with( - "https://mydomain.com/some/path", params={"page": 2} + "http://mydomain.com/some/path", params={"page": 2} ) assert result assert result["success"] is True @@ -43,7 +43,7 @@ def test_get(self, mocked_get): def test_get_no_200(self, mocked_get): mocked_get.return_value.status_code = 404 - client = HathorCoreClient("mydomain.com") + client = HathorCoreClient("https://mydomain.com") result = client.get("/some/path", {"id": 42}) @@ -56,7 +56,7 @@ def test_get_no_200(self, mocked_get): def test_get_raises(self, mocked_get): mocked_get.side_effect = Exception("Boom!") - client = HathorCoreClient("mydomain.com") + client = HathorCoreClient("https://mydomain.com") with raises(Exception, match=r"Boom!"): result = client.get("/some/path", {"page": -12}) @@ -72,7 +72,7 @@ def test_get_raises(self, mocked_get): def test_get_timeout(self, mocked_get): mocked_get.side_effect = requests.ReadTimeout("reason") - client = HathorCoreClient("mydomain.com") + client = HathorCoreClient("https://mydomain.com") with raises(Exception, match=r"timeout"): client.get("/some/path", {"page": 69})