utils: Simplify python RPC Response implementation

Reduce bloat in the python RPC Response class by making better use of
superclass methods and recursion. As part of this change, add tests for
the Response class.
This commit is contained in:
iamamyth 2025-01-31 14:26:05 -08:00
parent 90359e31fd
commit ad782ded1d
2 changed files with 146 additions and 22 deletions

View file

@ -33,32 +33,22 @@ import json
class Response(dict):
def __init__(self, d):
for k in d.keys():
if type(d[k]) == dict:
self[k] = Response(d[k])
elif type(d[k]) == list:
self[k] = []
for i in range(len(d[k])):
if type(d[k][i]) == dict:
self[k].append(Response(d[k][i]))
else:
self[k].append(d[k][i])
else:
self[k] = d[k]
for k, v in d.items():
self[k] = self._decode(v)
@staticmethod
def _decode(o):
if isinstance(o, dict):
return Response(o)
elif isinstance(o, list):
return [Response._decode(i) for i in o]
else:
return o
def __getattr__(self, key):
return self[key]
def __setattr__(self, key, value):
self[key] = value
def __eq__(self, other):
if type(other) == dict:
return self == Response(other)
if self.keys() != other.keys():
return False
for k in self.keys():
if self[k] != other[k]:
return False
return True
class JSONRPC(object):
def __init__(self, url, username=None, password=None):
@ -73,7 +63,7 @@ class JSONRPC(object):
headers={'content-type': 'application/json'},
auth=HTTPDigestAuth(self.username, self.password) if self.username is not None else None)
res = res.json()
assert 'error' not in res, res
if result_field: