Skip to content

Commit

Permalink
Merge pull request #45 from NebraLtd/jsonrpc_exception_enhancement
Browse files Browse the repository at this point in the history
Add customised exceptions for JSON RPC Miner client.
  • Loading branch information
shawaj authored Oct 25, 2021
2 parents f8b2d8c + 4cc1462 commit 8156227
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
26 changes: 23 additions & 3 deletions hm_pyhelper/miner_json_rpc/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import requests
from jsonrpcclient import request
from hm_pyhelper.miner_json_rpc.exceptions import MinerConnectionError
from hm_pyhelper.miner_json_rpc.exceptions import MinerMalformedURL
from hm_pyhelper.miner_json_rpc.exceptions import MinerRegionUnset


class Client(object):
Expand All @@ -7,14 +11,30 @@ def __init__(self, url='http://helium-miner:4467'):
self.url = url

def __fetch_data(self, method, **kwargs):
response = request(self.url, method, **kwargs)
return response.data.result
try:
response = request(self.url, method, **kwargs)
return response.data.result
except requests.exceptions.ConnectionError:
raise MinerConnectionError(
"Unable to connect to miner %s" % self.url
)
except requests.exceptions.MissingSchema:
raise MinerMalformedURL(
"Miner JSONRPC URL '%s' is not a valid URL"
% self.url
)

def get_height(self):
return self.__fetch_data('info_height')

def get_region(self):
return self.__fetch_data('info_region')
region = self.__fetch_data('info_region')
if not region.get('region'):
raise MinerRegionUnset(
"Miner at %s does not have an asserted region"
% self.url
)
return region

def get_summary(self):
return self.__fetch_data('info_summary')
Expand Down
15 changes: 15 additions & 0 deletions hm_pyhelper/miner_json_rpc/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

class MinerJSONRPCException(Exception):
pass


class MinerConnectionError(MinerJSONRPCException):
pass


class MinerMalformedURL(MinerJSONRPCException):
pass


class MinerRegionUnset(MinerJSONRPCException):
pass
52 changes: 52 additions & 0 deletions hm_pyhelper/tests/test_miner_json_rpc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import unittest
import mock

from hm_pyhelper.miner_json_rpc import MinerClient
from hm_pyhelper.miner_json_rpc.exceptions import MinerRegionUnset
from hm_pyhelper.miner_json_rpc.exceptions import MinerMalformedURL
from hm_pyhelper.miner_json_rpc.exceptions import MinerConnectionError


BASE_URL = 'http://helium-miner:4467'
Expand All @@ -23,6 +27,34 @@ def test_instantiation(self):
self.assertIsInstance(client, MinerClient)
self.assertEqual(client.url, BASE_URL)

def test_malformed_url(self):
client = MinerClient(url='fakeurl')

exception_raised = False
exception_type = None
try:
client.get_height()
except Exception as exc:
exception_raised = True
exception_type = exc

self.assertTrue(exception_raised)
self.assertIsInstance(exception_type, MinerMalformedURL)

def test_connection_error(self):
client = MinerClient(url='http://notarealminer:9999')

exception_raised = False
exception_type = None
try:
client.get_height()
except Exception as exc:
exception_raised = True
exception_type = exc

self.assertTrue(exception_raised)
self.assertIsInstance(exception_type, MinerConnectionError)

@mock.patch('hm_pyhelper.miner_json_rpc.client.request')
def test_get_height(self, mock_json_rpc_client):
mock_json_rpc_client.return_value = Response(
Expand All @@ -38,6 +70,26 @@ def test_get_height(self, mock_json_rpc_client):
)
self.assertEqual(result, {'epoch': 25612, 'height': 993640})

@mock.patch('hm_pyhelper.miner_json_rpc.client.request')
def test_get_region_not_asserted(self, mock_json_rpc_client):
mock_json_rpc_client.return_value = Response(
data=Result(
result={'region': None}
)
)
client = MinerClient()
exception_raised = False
exception_type = None

try:
client.get_region()
except Exception as exc:
exception_raised = True
exception_type = exc

self.assertTrue(exception_raised)
self.assertIsInstance(exception_type, MinerRegionUnset)

@mock.patch('hm_pyhelper.miner_json_rpc.client.request')
def test_get_region(self, mock_json_rpc_client):
mock_json_rpc_client.return_value = Response(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='hm_pyhelper',
version='0.8.11',
version='0.8.12',
author="Nebra Ltd",
author_email="[email protected]",
description="Helium Python Helper",
Expand Down

0 comments on commit 8156227

Please sign in to comment.