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).
This commit is contained in:
Scott King 2017-05-29 13:21:20 -06:00 committed by GitHub
parent abaa2c72e5
commit ea4361ad78

View File

@ -13,6 +13,7 @@ if __name__ == "__main__":
sys.exit(0) sys.exit(0)
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
import base64
import json import json
from binascii import hexlify from binascii import hexlify
@ -139,7 +140,10 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
def _decode(self, text, decode_type): def _decode(self, text, decode_type):
try: 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: except Exception as e:
raise APIError(22, "Decode error - " + str(e) + ". Had trouble while decoding string: " + repr(text)) 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): def ListSubscriptions(self, params):
queryreturn = sqlQuery('''SELECT label, address, enabled FROM subscriptions''') queryreturn = sqlQuery('''SELECT label, address, enabled FROM subscriptions''')
data = '{"subscriptions":[' data = {'subscriptions': []}
for row in queryreturn: for row in queryreturn:
label, address, enabled = row label, address, enabled = row
label = shared.fixPotentiallyInvalidUTF8Data(label) label = shared.fixPotentiallyInvalidUTF8Data(label)
if len(data) > 20: data['subscriptions'].append({'label':label.encode('base64'), 'address': address, 'enabled': enabled == 1})
data += ',' return json.dumps(data, indent=4, separators=(',',': '))
data += json.dumps({'label':label.encode('base64'), 'address': address, 'enabled': enabled == 1}, indent=4, separators=(',',': '))
data += ']}'
return data
def HandleDisseminatePreEncryptedMsg(self, params): def HandleDisseminatePreEncryptedMsg(self, params):
# The device issuing this command to PyBitmessage supplies a msg object that has # The device issuing this command to PyBitmessage supplies a msg object that has