From 2c6473ec3df06c831b6229b6acac99a9d8598fc5 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 2 Oct 2026 10:02:32 +0000 Subject: [PATCH] http: new `RemoteJSONClient` class --- mmgen/http.py | 9 +++++++++ mmgen/proto/rune/rpc/remote.py | 23 ++++++----------------- mmgen/swap/proto/thorchain/thornode.py | 16 +++++----------- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/mmgen/http.py b/mmgen/http.py index 2f2bb3da..9349a1ef 100755 --- a/mmgen/http.py +++ b/mmgen/http.py @@ -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) diff --git a/mmgen/proto/rune/rpc/remote.py b/mmgen/proto/rune/rpc/remote.py index 3d853291..d00d873c 100755 --- a/mmgen/proto/rune/rpc/remote.py +++ b/mmgen/proto/rune/rpc/remote.py @@ -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" diff --git a/mmgen/swap/proto/thorchain/thornode.py b/mmgen/swap/proto/thorchain/thornode.py index 442c9196..3480dbe6 100755 --- a/mmgen/swap/proto/thorchain/thornode.py +++ b/mmgen/swap/proto/thorchain/thornode.py @@ -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):