Skip to content

Commit

Permalink
feat: HTTPClient health check
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrl-Felix committed Feb 15, 2024
1 parent e611fd6 commit ea07705
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "mospy-wallet"
version = "0.5.3"
version = "0.5.4"
description = "This package is a fork of cosmospy and is a light framework for the cosmos ecosystem"
authors = [
{ name = "ctrl-felix", email = "[email protected]" },
Expand Down
29 changes: 26 additions & 3 deletions src/mospy/clients/HTTPClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mospy.Transaction import Transaction

from mospy.exceptions.clients import NodeException
from mospy.exceptions.clients import NodeTimeoutException, TransactionNotFound, TransactionTimeout
from mospy.exceptions.clients import NodeTimeoutException, TransactionNotFound, TransactionTimeout, NotAnApiNode


class HTTPClient:
Expand All @@ -15,10 +15,33 @@ class HTTPClient:
Args:
api (str): URL to a Api node
check_api (bool): Check if the Api node is reachable. Returns either NotAnApiNode or NodeException if the node is not healthy.
"""

def __init__(self, *, api: str = "https://rest.cosmos.directory/cosmoshub"):
self._api = api
def __init__(self, *, api: str = "https://rest.cosmos.directory/cosmoshub", check_api: bool = False):
self._api = api.rstrip("/")

# Check API if reachable
if check_api:
self.check_api()

def check_api(self):
"""
Checks if the API is reachable. Returns a NodeException or a NotAnApiNode Exception when the check failed.
"""
try:
check_response = httpx.get(self._api + "/node_info")
except httpx.HTTPError:
raise NodeException(
"The passed url is not reachable. To disable the health check pass check_api=false to the HTTPClient constructor.")

if check_response.status_code != 200:
# Check if the returned text matches the one returned by an RPC
if "404 page not found" in check_response.text:
raise NotAnApiNode("Please pass an API endpoint to the HTTPClient. You probably passed an RPC.")
else:
raise NodeException(
"Health couldn't be verified. To disable the health check pass check_api=false to the HTTPClient constructor.")

def _make_post_request(self, path, payload, timeout):
try:
Expand Down
5 changes: 4 additions & 1 deletion src/mospy/exceptions/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ class TransactionTimeout(Exception):

class TransactionNotFound(Exception):
"""Raised when the transaction couldn't be found on chain."""
pass
pass

class NotAnApiNode(Exception):
"""The url passed to the constructor couldn't be detected as API node. This might be because you passed the rpc endpoint instead."""
19 changes: 19 additions & 0 deletions tests/clients/test_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from mospy import Transaction
from mospy.clients import HTTPClient

from mospy.exceptions.clients import NotAnApiNode, NodeException

API = "https://cosmos-rest.publicnode.com"

class TestHTTPClientClass:
Expand All @@ -21,6 +23,23 @@ def test_account_data_loading(self):

assert account.next_sequence >= 0

def test_node_health_check(self):
try:
client = HTTPClient(api="https://cosmos-rpc.publicnode.com", check_api=True)
except NotAnApiNode:
assert True
else:
assert False

try:
client = HTTPClient(api="https://dead-url-awdbnawdbauiowd.com", check_api=True)
except NodeException:
assert True
else:
assert False

client = HTTPClient(api="https://dead-url-awdbnawdbauiowd.com", check_api=False)

def test_transaction_submitting(self):
account = Account(
seed_phrase=self.seed_phrase,
Expand Down

0 comments on commit ea07705

Please sign in to comment.