Skip to content
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
30 changes: 29 additions & 1 deletion steem/steem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .commit import Commit
from .steemd import Steemd

from steembase.exceptions import RPCError

class Steem:
""" Connect to the Steem network.
Expand Down Expand Up @@ -57,9 +57,37 @@ def __getattr__(self, item):
return getattr(self.steemd, item)
if hasattr(self.commit, item):
return getattr(self.commit, item)
if item.endswith("_api"):
return Steem.Api(api_name=item, exec_method=self.steemd.exec)

raise AttributeError('Steem has no attribute "%s"' % item)

class Api(object):
def __init__(self, api_name="", exec_method=None):
self.api_name = api_name
self.exec_method = exec_method
return

def __getattr__(self, method_name):
return Steem.Method(
api_name=self.api_name,
method_name=method_name,
exec_method=self.exec_method,
)

class Method(object):
def __init__(self, api_name="", method_name="", exec_method=None):
self.api_name = api_name
self.method_name = method_name
self.exec_method = exec_method
return

def __call__(self, *args, **kwargs):
if len(kwargs) > 0:
if len(args) > 0:
raise RPCError("Cannot specify both args and kwargs in RPC")
return self.exec_method(self.method_name, kwargs=kwargs, api=self.api_name)
return self.exec_method(self.method_name, *args, api=self.api_name)

if __name__ == '__main__':
s = Steem()
Expand Down
10 changes: 6 additions & 4 deletions steembase/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def hostname(self):
return urlparse(self.url).hostname

@staticmethod
def json_rpc_body(name, *args, api=None, as_json=True, _id=0):
def json_rpc_body(name, *args, api=None, as_json=True, _id=0, kwargs=None):
""" Build request body for steemd RPC requests.

Args:
Expand All @@ -123,7 +123,9 @@ def json_rpc_body(name, *args, api=None, as_json=True, _id=0):
Otherwise, a Python dictionary is returned.
"""
headers = {"jsonrpc": "2.0", "id": _id}
if api:
if kwargs is not None:
body_dict = {**headers, "method": "call", "params": [api, name, kwargs]}
elif api:
body_dict = {**headers, "method": "call", "params": [api, name, args]}
else:
body_dict = {**headers, "method": name, "params": args}
Expand All @@ -132,15 +134,15 @@ def json_rpc_body(name, *args, api=None, as_json=True, _id=0):
else:
return body_dict

def exec(self, name, *args, api=None, return_with_args=None, _ret_cnt=0):
def exec(self, name, *args, api=None, return_with_args=None, _ret_cnt=0, kwargs=None):
""" Execute a method against steemd RPC.

Warnings:
This command will auto-retry in case of node failure, as well as handle
node fail-over, unless we are broadcasting a transaction.
In latter case, the exception is **re-raised**.
"""
body = HttpClient.json_rpc_body(name, *args, api=api)
body = HttpClient.json_rpc_body(name, *args, api=api, kwargs=kwargs)
response = None
try:
response = self.request(body=body)
Expand Down