Skip to content

Commit

Permalink
added custom url path functionality (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
chikko80 committed Dec 2, 2021
1 parent 01681ce commit 9691d42
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
10 changes: 5 additions & 5 deletions bit/network/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def set_service_timeout(seconds):


class RPCHost:
def __init__(self, user, password, host, port, use_https):
def __init__(self, user, password, host, port, use_https, path):
self._session = requests.Session()
self._url = "http{s}://{user}:{password}@{host}:{port}/".format(
s="s" if use_https else "", user=user, password=password, host=host, port=port,
self._url = "http{s}://{user}:{password}@{host}:{port}/{path}".format(
s="s" if use_https else "", user=user, password=password, host=host, port=port, path=path
)
self._headers = {"content-type": "application/json"}
self._session.verify = use_https
Expand Down Expand Up @@ -1017,7 +1017,7 @@ class NetworkAPI:
]

@classmethod
def connect_to_node(cls, user, password, host='localhost', port=8332, use_https=False, testnet=False):
def connect_to_node(cls, user, password, host='localhost', port=8332, use_https=False, testnet=False, path=""):
"""Connect to a remote Bitcoin node instead of using web APIs.
Allows to connect to a testnet and mainnet Bitcoin node simultaneously.
Expand All @@ -1039,7 +1039,7 @@ def connect_to_node(cls, user, password, host='localhost', port=8332, use_https=
:returns: The node exposing its RPCs for direct interaction.
:rtype: ``RPCHost``
"""
node = RPCHost(user=user, password=password, host=host, port=port, use_https=use_https)
node = RPCHost(user=user, password=password, host=host, port=port, use_https=use_https, path=path)

# Inject remote node into NetworkAPI
if testnet is False:
Expand Down
76 changes: 38 additions & 38 deletions tests/network/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,39 +277,39 @@ class n(NetworkAPI):
n.connect_to_node(user="user", password="password")
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False))
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False, ""))
for call in n.GET_BALANCE_MAIN
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_BALANCE_MAIN)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False))
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False, ""))
for call in n.GET_TRANSACTIONS_MAIN
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTIONS_MAIN)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False))
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False, ""))
for call in n.GET_TRANSACTION_BY_ID_MAIN
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTION_BY_ID_MAIN)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False))
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False, ""))
for call in n.GET_UNSPENT_MAIN
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_UNSPENT_MAIN)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False))
both_rpchosts_equal(call.__self__, RPCHost("user", "password", "localhost", 8332, False, ""))
for call in n.BROADCAST_TX_MAIN
)
== 1
Expand All @@ -330,39 +330,39 @@ class n(NetworkAPI):
n.connect_to_node(user="usr", password="pass", host="host", port=18443, use_https=True, testnet=True)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True))
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True, ""))
for call in n.GET_BALANCE_TEST
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_BALANCE_TEST)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True))
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True, ""))
for call in n.GET_TRANSACTIONS_TEST
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTIONS_TEST)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True))
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True, ""))
for call in n.GET_TRANSACTION_BY_ID_TEST
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_TRANSACTION_BY_ID_TEST)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True))
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True, ""))
for call in n.GET_UNSPENT_TEST
)
== 1
)
assert all(isinstance(call.__self__, RPCHost) for call in n.GET_UNSPENT_TEST)
assert (
sum(
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True))
both_rpchosts_equal(call.__self__, RPCHost("usr", "pass", "host", 18443, True, ""))
for call in n.BROADCAST_TX_TEST
)
== 1
Expand Down Expand Up @@ -645,13 +645,13 @@ def test_get_unspent_test_unused(self):

class TestRPCHost:
def test_init(self):
node = RPCHost("user", "password", "host", 8333, True)
node = RPCHost("user", "password", "host", 8333, True, "")
assert node._url == "https://user:password@host:8333/"
assert isinstance(node._session, requests.Session)
assert node._session.verify is True
assert node._headers == {"content-type": "application/json"}

node = RPCHost("usr", "pass", "other", 18443, False)
node = RPCHost("usr", "pass", "other", 18443, False, "")
assert node._url == "http://usr:pass@other:18443/"
assert isinstance(node._session, requests.Session)
assert node._session.verify is False
Expand All @@ -663,87 +663,87 @@ def test_rpc_method_call(self):
assert RPCHost.__getattr__(None, "rpc_method")._host is None

def test_get_balance_return_type(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert isinstance(node.get_balance(MAIN_ADDRESS_USED1), int)

def test_get_balance_main_unused(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert node.get_balance(MAIN_ADDRESS_UNUSED) == 0

def test_get_balance_test_unused(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert node.get_balance_testnet(TEST_ADDRESS_UNUSED) == 0

def test_get_balance_main_used(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert node.get_balance(MAIN_ADDRESS_USED1) == 123456789

def test_get_balance_test_used(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert node.get_balance_testnet(TEST_ADDRESS_USED2) == 123456789

def test_get_transactions_return_type(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert iter(node.get_transactions(MAIN_ADDRESS_USED1))

def test_get_transactions_main_used(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert len(node.get_transactions(MAIN_ADDRESS_USED1)) == 2

def test_get_transactions_main_unused(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert len(node.get_transactions(MAIN_ADDRESS_UNUSED)) == 0

def test_get_transactions_test_used(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert len(node.get_transactions_testnet(TEST_ADDRESS_USED2)) == 3

def test_get_transactions_test_unused(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert len(node.get_transactions_testnet(TEST_ADDRESS_UNUSED)) == 0

def test_get_transaction_by_id_main(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert node.get_transaction_by_id(MAIN_TX_VALID)

def test_get_transaction_by_id_test(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert node.get_transaction_by_id_testnet(TEST_TX_VALID)

def test_get_unspent_return_type(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert iter(node.get_unspent(MAIN_ADDRESS_USED1))

def test_get_unspent_used(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert len(node.get_unspent(MAIN_ADDRESS_USED1)) == 2

def test_get_unspent_unused(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert len(node.get_unspent(MAIN_ADDRESS_UNUSED)) == 0

def test_get_unspent_test_used(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert len(node.get_unspent_testnet(TEST_ADDRESS_USED2)) == 2

def test_get_unspent_test_unused(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert len(node.get_unspent_testnet(TEST_ADDRESS_UNUSED)) == 0

def test_broadcast_tx(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert node.broadcast_tx("01000000000000000000") is True

def test_broadcast_tx_fail(self):
node = MockRPCHost("user", "password", "host", 8333, True)
node = MockRPCHost("user", "password", "host", 8333, True, "")
assert node.broadcast_tx("00000000000000000000") is False

def test_broadcast_tx_test(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert node.broadcast_tx_testnet("01000000000000000000") is True

def test_broadcast_tx_test_fail(self):
node = MockRPCHost("user", "password", "host", 18443, False)
node = MockRPCHost("user", "password", "host", 18443, False, "")
assert node.broadcast_tx_testnet("00000000000000000000") is False


Expand All @@ -755,7 +755,7 @@ def test_init(self):

@requests_mock.mock()
def test_call_success(self, m):
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False))
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False, ""))
m.register_uri(
'POST',
'http://user:password@host:18443/',
Expand All @@ -767,7 +767,7 @@ def test_call_success(self, m):
)
self.assertEqual(method("arg1", 2), True)

method = RPCMethod("other_rpc_method", RPCHost("user", "password", "host", 18443, False))
method = RPCMethod("other_rpc_method", RPCHost("user", "password", "host", 18443, False, ""))
m.register_uri(
'POST',
'http://user:password@host:18443/',
Expand All @@ -781,7 +781,7 @@ def test_call_success(self, m):

@requests_mock.mock()
def test_call_fails_status_code(self, m):
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False))
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False, ""))
m.register_uri(
'POST', 'http://user:password@host:18443/', status_code=201, reason="testing failing status code"
)
Expand All @@ -790,7 +790,7 @@ def test_call_fails_status_code(self, m):

@requests_mock.mock()
def test_call_fails_unsupported_command(self, m):
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False))
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "host", 18443, False, ""))
m.register_uri(
'POST',
'http://user:password@host:18443/',
Expand All @@ -802,7 +802,7 @@ def test_call_fails_unsupported_command(self, m):
method()

def test_call_fails_connection(self):
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "some_invalid_host", 18443, False))
method = RPCMethod("some_rpc_method", RPCHost("user", "password", "some_invalid_host", 18443, False, ""))
with pytest.raises(ConnectionError):
method()

Expand Down

0 comments on commit 9691d42

Please sign in to comment.