Skip to content

Commit a6ddced

Browse files
authored
refactor: client api endpoint methods (#126)
1 parent b4486d3 commit a6ddced

2 files changed

Lines changed: 108 additions & 28 deletions

File tree

client/client.py

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import inspect
2-
import pkgutil
3-
from importlib import import_module
4-
from pathlib import Path
5-
61
from client.connection import Connection
7-
from client.exceptions import ArkParameterException
8-
from client.resource import Resource
2+
from client.api.api_nodes import ApiNodes
3+
from client.api.blockchain import Blockchain
4+
from client.api.blocks import Blocks
5+
from client.api.commits import Commits
6+
from client.api.delegates import Delegates
7+
from client.api.node import Node
8+
from client.api.peers import Peers
9+
from client.api.rounds import Rounds
10+
from client.api.transactions import Transactions
11+
from client.api.votes import Votes
12+
from client.api.wallets import Wallets
913

1014
class ArkClient(object):
1115

@@ -16,22 +20,91 @@ def __init__(self, hostname):
1620
on whatever url they want.
1721
"""
1822
self.connection = Connection(hostname)
19-
self._import_api()
2023

21-
def _import_api(self):
24+
@property
25+
def api_nodes(self):
26+
"""
27+
:return: Nodes API
28+
:rtype: client.api.api_nodes.ApiNodes
29+
"""
30+
return ApiNodes(self.connection)
31+
32+
@property
33+
def blockchain(self):
34+
"""
35+
:return: Blockchain API
36+
:rtype: client.api.blockchain.Blockchain
37+
"""
38+
return Blockchain(self.connection)
39+
40+
@property
41+
def blocks(self):
42+
"""
43+
:return: Blocks API
44+
:rtype: client.api.blocks.Blocks
45+
"""
46+
return Blocks(self.connection)
47+
48+
@property
49+
def commits(self):
50+
"""
51+
:return: Commits API
52+
:rtype: client.api.commits.Commits
53+
"""
54+
return Commits(self.connection)
55+
56+
@property
57+
def delegates(self):
58+
"""
59+
:return: Delegates API
60+
:rtype: client.api.delegates.Delegates
61+
"""
62+
return Delegates(self.connection)
63+
64+
@property
65+
def node(self):
66+
"""
67+
:return: Node API
68+
:rtype: client.api.node.Node
69+
"""
70+
return Node(self.connection)
71+
72+
@property
73+
def peers(self):
2274
"""
23-
Dynamically imports API endpoints.
75+
:return: Peers API
76+
:rtype: client.api.peers.Peers
2477
"""
25-
modules = pkgutil.iter_modules([str(Path(__file__).parent / 'api')])
26-
for _, name, _ in modules:
27-
module = import_module('client.api.{}'.format(name))
28-
for attr in dir(module):
29-
# If attr name is `Resource`, skip it as it's a class and also has a
30-
# subclass of Resource
31-
if attr == 'Resource':
32-
continue
78+
return Peers(self.connection)
3379

34-
attribute = getattr(module, attr)
35-
if inspect.isclass(attribute) and issubclass(attribute, Resource):
36-
# Set module class as a property on the client
37-
setattr(self, name, attribute(self.connection))
80+
@property
81+
def rounds(self):
82+
"""
83+
:return: Rounds API
84+
:rtype: client.api.rounds.Rounds
85+
"""
86+
return Rounds(self.connection)
87+
88+
@property
89+
def transactions(self):
90+
"""
91+
:return: Transactions API
92+
:rtype: client.api.transactions.Transactions
93+
"""
94+
return Transactions(self.connection)
95+
96+
@property
97+
def votes(self):
98+
"""
99+
:return: Votes API
100+
:rtype: client.api.votes.Votes
101+
"""
102+
return Votes(self.connection)
103+
104+
@property
105+
def wallets(self):
106+
"""
107+
:return: Wallets API
108+
:rtype: client.api.wallets.Wallets
109+
"""
110+
return Wallets(self.connection)

tests/test_client.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import pytest
22

33
from client.client import ArkClient
4-
from client.connection import Connection
54

65

7-
def test_client_creation_calls_import_api(mocker):
8-
import_mock = mocker.patch.object(ArkClient, '_import_api')
9-
6+
def test_client():
107
client = ArkClient('http://127.0.0.1:4002')
118

12-
assert isinstance(client.connection, Connection)
13-
assert import_mock.call_count == 1
9+
assert hasattr(client, 'connection') == True
10+
assert hasattr(client, 'api_nodes') == True
11+
assert hasattr(client, 'blockchain') == True
12+
assert hasattr(client, 'blocks') == True
13+
assert hasattr(client, 'commits') == True
14+
assert hasattr(client, 'delegates') == True
15+
assert hasattr(client, 'node') == True
16+
assert hasattr(client, 'peers') == True
17+
assert hasattr(client, 'rounds') == True
18+
assert hasattr(client, 'transactions') == True
19+
assert hasattr(client, 'votes') == True
20+
assert hasattr(client, 'wallets') == True

0 commit comments

Comments
 (0)