From 5a1d7911f3193691b95bdcc05708c69bde0fa11b Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Fri, 5 Jul 2019 17:33:16 +0100 Subject: [PATCH] Adds support for command line api host and port. Defaults to __init__ parameters if missing --- pisa-btc/apps/__init__.py | 4 ++-- pisa-btc/apps/pisa-cli.py | 30 +++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pisa-btc/apps/__init__.py b/pisa-btc/apps/__init__.py index bf87110..452f264 100644 --- a/pisa-btc/apps/__init__.py +++ b/pisa-btc/apps/__init__.py @@ -1,2 +1,2 @@ -PISA_API_SERVER = 'localhost' -PISA_API_PORT = 2222 \ No newline at end of file +DEFAULT_PISA_API_SERVER = 'localhost' +DEFAULT_PISA_API_PORT = 2222 \ No newline at end of file diff --git a/pisa-btc/apps/pisa-cli.py b/pisa-btc/apps/pisa-cli.py index af14068..d27c974 100644 --- a/pisa-btc/apps/pisa-cli.py +++ b/pisa-btc/apps/pisa-cli.py @@ -8,9 +8,9 @@ import logging from conf import CLIENT_LOG_FILE from hashlib import sha256 from binascii import unhexlify - from apps.blob import Blob -from apps import PISA_API_SERVER, PISA_API_PORT +from requests import ConnectTimeout +from apps import DEFAULT_PISA_API_SERVER, DEFAULT_PISA_API_PORT commands = ['add_appointment'] @@ -51,9 +51,11 @@ def show_usage(): if __name__ == '__main__': debug = False + pisa_api_server = DEFAULT_PISA_API_SERVER + pisa_api_port = DEFAULT_PISA_API_PORT command = None - opts, args = getopt(argv[1:], 'a:d', ['add_appointment, debug']) + opts, args = getopt(argv[1:], 'a:dh:p:', ['add_appointment, debug, host, port']) for opt, arg in opts: if opt in ['-a', '--add_appointment']: if arg: @@ -67,6 +69,14 @@ if __name__ == '__main__': if opt in ['-d', '--debug']: debug = True + if opt in ['-h', 'host']: + if arg: + pisa_api_server = arg + + if opt in ['-p', '--port']: + if arg: + pisa_api_port = int(arg) + # Configure logging logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, handlers=[ logging.FileHandler(CLIENT_LOG_FILE), @@ -79,7 +89,7 @@ if __name__ == '__main__': valid_locator = check_txid_format(appointment_data.get('tx_id')) if valid_locator: - pisa_url = "http://{}:{}".format(PISA_API_SERVER, PISA_API_PORT) + pisa_url = "http://{}:{}".format(pisa_api_server, pisa_api_port) appointment = build_appointment(appointment_data.get('tx'), appointment_data.get('tx_id'), appointment_data.get('start_time'), appointment_data.get('end_time'), appointment_data.get('dispute_delta'), debug, logging) @@ -87,10 +97,16 @@ if __name__ == '__main__': if debug: logging.info("[Client] sending appointment to PISA") - r = requests.post(url=pisa_url, json=json.dumps(appointment)) + try: + r = requests.post(url=pisa_url, json=json.dumps(appointment), timeout=5) + + if debug: + logging.info("[Client] {} (code: {})".format(r.text, r.status_code)) + + except ConnectTimeout: + if debug: + logging.info("[Client] can't connect to pisa API. Connection timeout") - if debug: - logging.info("[Client] {} (code: {})".format(r.text, r.status_code)) else: raise ValueError("The provided locator is not valid.")