From ea4361ad78e4ae8d64a38b898e083cb1ad06dff2 Mon Sep 17 00:00:00 2001 From: Scott King Date: Mon, 29 May 2017 13:21:20 -0600 Subject: [PATCH] Update api.py ListSubscriptions isn't as messed up as it was json-wise, and is more readable. ( HandleListAddresses still needs fixed with json as well. ) base64 is imported and used in _decode because .decode() appends a newline character to each encoded string. _decode now doesn't use .decode() and instead uses hexlify(text) and base64.b64decode(text). --- src/api.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/api.py b/src/api.py index 212cd278..1563ebcd 100644 --- a/src/api.py +++ b/src/api.py @@ -13,6 +13,7 @@ if __name__ == "__main__": sys.exit(0) from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer +import base64 import json from binascii import hexlify @@ -139,7 +140,10 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): def _decode(self, text, decode_type): try: - return text.decode(decode_type) + if decode_type == 'hex': + return hexlify(text) + elif decode_type == 'base64': + return base64.b64decode(text) except Exception as e: raise APIError(22, "Decode error - " + str(e) + ". Had trouble while decoding string: " + repr(text)) @@ -816,15 +820,12 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): def ListSubscriptions(self, params): queryreturn = sqlQuery('''SELECT label, address, enabled FROM subscriptions''') - data = '{"subscriptions":[' + data = {'subscriptions': []} for row in queryreturn: label, address, enabled = row label = shared.fixPotentiallyInvalidUTF8Data(label) - if len(data) > 20: - data += ',' - data += json.dumps({'label':label.encode('base64'), 'address': address, 'enabled': enabled == 1}, indent=4, separators=(',',': ')) - data += ']}' - return data + data['subscriptions'].append({'label':label.encode('base64'), 'address': address, 'enabled': enabled == 1}) + return json.dumps(data, indent=4, separators=(',',': ')) def HandleDisseminatePreEncryptedMsg(self, params): # The device issuing this command to PyBitmessage supplies a msg object that has