diff --git a/apps/cli/pisa_cli.py b/apps/cli/pisa_cli.py index 440860d..56074de 100644 --- a/apps/cli/pisa_cli.py +++ b/apps/cli/pisa_cli.py @@ -23,7 +23,7 @@ from apps.cli import ( from common.logger import Logger from common.appointment import Appointment from common.cryptographer import Cryptographer -from common.tools import check_sha256_hex_format, compute_locator +from common.tools import check_sha256_hex_format, check_locator_format, compute_locator HTTP_OK = 200 @@ -256,7 +256,7 @@ def check_signature(signature, appointment): def get_appointment(args): if not args: logger.error("No arguments were given") - return False + return None arg_opt = args.pop(0) @@ -264,27 +264,27 @@ def get_appointment(args): sys.exit(help_get_appointment()) else: locator = arg_opt - valid_locator = check_sha256_hex_format(locator) + valid_locator = check_locator_format(locator) if not valid_locator: logger.error("The provided locator is not valid", locator=locator) - return False + return None get_appointment_endpoint = "http://{}:{}/get_appointment".format(pisa_api_server, pisa_api_port) parameters = "?locator={}".format(locator) try: r = requests.get(url=get_appointment_endpoint + parameters, timeout=5) - logger.info("Appointment response returned from server: " + str(r)) - return True + logger.info("Appointment response returned from server: {}".format(r.json())) + return r.json() except ConnectTimeout: logger.error("Can't connect to pisa API. Connection timeout") - return False + return None except ConnectionError: logger.error("Can't connect to pisa API. Server cannot be reached") - return False + return None def get_appointment_signature(appointment): diff --git a/common/tools.py b/common/tools.py index 2ac6d1e..d208272 100644 --- a/common/tools.py +++ b/common/tools.py @@ -15,6 +15,19 @@ def check_sha256_hex_format(value): return isinstance(value, str) and re.match(r"^[0-9A-Fa-f]{64}$", value) is not None +def check_locator_format(value): + """ + Checks if a given value is a 16-byte hex encoded string. + + Args: + value(:mod:`str`): the value to be checked. + + Returns: + :mod:`bool`: Whether or not the value matches the format. + """ + return isinstance(value, str) and re.match(r"^[0-9A-Fa-f]{32}$", value) is not None + + def compute_locator(tx_id): """ Computes an appointment locator given a transaction id. diff --git a/pisa/tools.py b/pisa/tools.py index 534b535..540020c 100644 --- a/pisa/tools.py +++ b/pisa/tools.py @@ -1,4 +1,5 @@ from http.client import HTTPException +from socket import timeout import pisa.conf as conf from pisa.utils.auth_proxy import AuthServiceProxy, JSONRPCException @@ -36,7 +37,7 @@ def can_connect_to_bitcoind(): try: bitcoin_cli().help() - except (ConnectionRefusedError, JSONRPCException, HTTPException): + except (timeout, ConnectionRefusedError, JSONRPCException, HTTPException): can_connect = False return can_connect