Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: client api endpoint methods #126

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 95 additions & 22 deletions client/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import inspect
import pkgutil
from importlib import import_module
from pathlib import Path

from client.connection import Connection
from client.exceptions import ArkParameterException
from client.resource import Resource
from client.api.api_nodes import ApiNodes
from client.api.blockchain import Blockchain
from client.api.blocks import Blocks
from client.api.commits import Commits
from client.api.delegates import Delegates
from client.api.node import Node
from client.api.peers import Peers
from client.api.rounds import Rounds
from client.api.transactions import Transactions
from client.api.votes import Votes
from client.api.wallets import Wallets

class ArkClient(object):

Expand All @@ -16,22 +20,91 @@ def __init__(self, hostname):
on whatever url they want.
"""
self.connection = Connection(hostname)
self._import_api()

def _import_api(self):
@property
def api_nodes(self):
"""
:return: Nodes API
:rtype: client.api.api_nodes.ApiNodes
"""
return ApiNodes(self.connection)

@property
def blockchain(self):
"""
:return: Blockchain API
:rtype: client.api.blockchain.Blockchain
"""
return Blockchain(self.connection)

@property
def blocks(self):
"""
:return: Blocks API
:rtype: client.api.blocks.Blocks
"""
return Blocks(self.connection)

@property
def commits(self):
"""
:return: Commits API
:rtype: client.api.commits.Commits
"""
return Commits(self.connection)

@property
def delegates(self):
"""
:return: Delegates API
:rtype: client.api.delegates.Delegates
"""
return Delegates(self.connection)

@property
def node(self):
"""
:return: Node API
:rtype: client.api.node.Node
"""
return Node(self.connection)

@property
def peers(self):
"""
Dynamically imports API endpoints.
:return: Peers API
:rtype: client.api.peers.Peers
"""
modules = pkgutil.iter_modules([str(Path(__file__).parent / 'api')])
for _, name, _ in modules:
module = import_module('client.api.{}'.format(name))
for attr in dir(module):
# If attr name is `Resource`, skip it as it's a class and also has a
# subclass of Resource
if attr == 'Resource':
continue
return Peers(self.connection)

attribute = getattr(module, attr)
if inspect.isclass(attribute) and issubclass(attribute, Resource):
# Set module class as a property on the client
setattr(self, name, attribute(self.connection))
@property
def rounds(self):
"""
:return: Rounds API
:rtype: client.api.rounds.Rounds
"""
return Rounds(self.connection)

@property
def transactions(self):
"""
:return: Transactions API
:rtype: client.api.transactions.Transactions
"""
return Transactions(self.connection)

@property
def votes(self):
"""
:return: Votes API
:rtype: client.api.votes.Votes
"""
return Votes(self.connection)

@property
def wallets(self):
"""
:return: Wallets API
:rtype: client.api.wallets.Wallets
"""
return Wallets(self.connection)
19 changes: 13 additions & 6 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import pytest

from client.client import ArkClient
from client.connection import Connection


def test_client_creation_calls_import_api(mocker):
import_mock = mocker.patch.object(ArkClient, '_import_api')

def test_client():
client = ArkClient('http://127.0.0.1:4002')

assert isinstance(client.connection, Connection)
assert import_mock.call_count == 1
assert hasattr(client, 'connection') == True
assert hasattr(client, 'api_nodes') == True
assert hasattr(client, 'blockchain') == True
assert hasattr(client, 'blocks') == True
assert hasattr(client, 'commits') == True
assert hasattr(client, 'delegates') == True
assert hasattr(client, 'node') == True
assert hasattr(client, 'peers') == True
assert hasattr(client, 'rounds') == True
assert hasattr(client, 'transactions') == True
assert hasattr(client, 'votes') == True
assert hasattr(client, 'wallets') == True
Loading