Updates cli and api accordingly

This commit is contained in:
Sergi Delgado
2019-04-24 16:59:20 +01:00
committed by Sergi Delgado Segura
parent d34b94a858
commit b4d058fc62
4 changed files with 16 additions and 16 deletions

View File

@@ -1 +1 @@
wrong_txid = "You should provide the 16 MSB (in base58 hex) of the txid you'd like to be monitored." wrong_txid = "You should provide the 16 MSB (in hex) of the txid you'd like to be monitored."

View File

@@ -3,25 +3,22 @@ from getopt import getopt
from sys import argv from sys import argv
from apps import PISA_API_SERVER, PISA_API_PORT from apps import PISA_API_SERVER, PISA_API_PORT
import apps.messages as msg import apps.messages as msg
from base58 import b58decode import re
commands = ['add_appointment']
commands = ['register_tx']
def check_txid_format(txid): def check_txid_format(txid):
if len(txid) != 32: if len(txid) != 32:
raise Exception("txid does not matches the expected size (16-byte / 32 hex chars). " + msg.wrong_txid) raise Exception("txid does not matches the expected size (16-byte / 32 hex chars). " + msg.wrong_txid)
try:
b58decode(txid) return re.search(r'^[0-9A-Fa-f]+$', txid) is not None
except ValueError:
raise Exception("The provided txid is not in base58. " + msg.wrong_txid)
def show_usage(): def show_usage():
print("usage: python pisa-cli.py argument [additional_arguments]." print("usage: python pisa-cli.py argument [additional_arguments]."
"\nArguments:" "\nArguments:"
"\nregister_tx half_txid: \tregisters a txid to be monitored by PISA using the 16 MSB of the txid (in hex)." "\nadd_appointment appointment: \tregisters a json formatted appointment "
"\nhelp: \t\tshows this message.") "\nhelp: \t\tshows this message.")
@@ -36,17 +33,20 @@ if __name__ == '__main__':
if command in commands: if command in commands:
if command == 'register_tx': if command in commands:
if len(args) != 2: if len(args) != 2:
raise Exception("txid missing. " + msg.wrong_txid) raise Exception("txid missing. " + msg.wrong_txid)
arg = args[1] arg = args[1]
check_txid_format(arg) valid_locator = check_txid_format(arg)
conn = Client((PISA_API_SERVER, PISA_API_PORT)) if valid_locator:
conn = Client((PISA_API_SERVER, PISA_API_PORT))
# Argv could be undefined, but we only have one command for now so safe # Argv could be undefined, but we only have one command so it's safe for now
conn.send((command, arg)) conn.send((command, arg))
else:
raise ValueError("The provided locator is not valid. " + msg.wrong_txid)
else: else:
show_usage() show_usage()

View File

@@ -35,7 +35,7 @@ def manage_request(conn, remote_addr, remote_port, inspector, watcher, debug, lo
command, arg = msg command, arg = msg
if command == "add_appointment": if command == "add_appointment":
appointment = inspector.inspect(msg, debug) appointment = inspector.inspect(arg, debug)
if appointment: if appointment:
appointment_added = watcher.add_appointment(appointment, debug, logging) appointment_added = watcher.add_appointment(appointment, debug, logging)