http: new RemoteJSONClient class

This commit is contained in:
The MMGen Project 2026-10-02 10:02:32 +00:00
commit 2c6473ec3d
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 20 additions and 28 deletions

View file

@ -70,3 +70,12 @@ class HTTPClient:
'HTTP POST failed with status code {s}\n URL: {u}\n DATA: {d}',
timeout,
data = data)
class RemoteJSONClient(HTTPClient):
http_hdrs = {'Content-Type': 'application/json'}
def __init__(self, cfg, proto, *, network_proto=None, host=None):
for k, v in getattr(proto, self.params).items():
setattr(self, k, v)
super().__init__(cfg, network_proto=network_proto, host=host)

View file

@ -14,7 +14,7 @@ proto.rune.rpc.remote: THORChain base protocol remote RPC client for the MMGen P
import json
from ....http import HTTPClient
from ....http import RemoteJSONClient
from ....rpc.remote import RemoteRPCClient
# throws exception on error:
@ -26,25 +26,14 @@ def process_response(json_response, errmsg):
return data['result']
# HTTP POST, JSON-RPC response:
class ThornodeRemoteRPCClient(HTTPClient):
class ThornodeRemoteRPCClient(RemoteJSONClient):
params = 'rpc_remote_rpc_params'
timeout = 30
def __init__(self, cfg, proto, *, network_proto=None, host=None):
for k, v in proto.rpc_remote_rpc_params.items():
setattr(self, k, v)
super().__init__(cfg, network_proto=network_proto, host=host)
# HTTP GET, params in query string, JSON-RPC response:
class ThornodeRemoteRESTClient(HTTPClient):
http_hdrs = {'Content-Type': 'application/json'}
timeout = 5
def __init__(self, cfg, proto, *, network_proto=None, host=None):
for k, v in proto.rpc_remote_rest_params.items():
setattr(self, k, v)
super().__init__(cfg, network_proto=network_proto, host=host)
class ThornodeRemoteRESTClient(RemoteJSONClient):
params = 'rpc_remote_rest_params'
timeout = 15
class THORChainRemoteRPCClient(RemoteRPCClient):
"communicate with remote THORChain JSON-RPC endpoint"

View file

@ -17,7 +17,7 @@ from collections import namedtuple
from ....protocol import init_proto
from ....amt import UniAmt
from ....http import HTTPClient
from ....http import RemoteJSONClient
_gd = namedtuple('gas_unit_data', ['code', 'disp'])
gas_unit_data = {
@ -25,23 +25,17 @@ gas_unit_data = {
'gwei': _gd('G', 'Gwei'),
}
class ThornodeSwapClient(HTTPClient):
http_hdrs = {'Content-Type': 'application/json'}
class ThornodeSwapClient(RemoteJSONClient):
params = 'rpc_remote_swap_params'
timeout = 5
def __init__(self, tx, *, network_proto=None, host=None):
rune_proto = init_proto(tx.cfg, 'rune', network=tx.cfg._proto.network)
for k, v in rune_proto.rpc_remote_swap_params.items():
setattr(self, k, v)
super().__init__(tx.cfg, network_proto=network_proto, host=host)
class Thornode:
def __init__(self, tx, amt):
self.tx = tx
self.in_amt = UniAmt(f'{amt:.8f}')
self.rpc = ThornodeSwapClient(tx)
proto = init_proto(tx.cfg, 'rune', network=tx.cfg._proto.network)
self.rpc = ThornodeSwapClient(tx.cfg, proto)
def get_quote(self, swap_cfg):