Adds support for command line api host and port.

Defaults to __init__ parameters if missing
This commit is contained in:
Sergi Delgado Segura
2019-07-05 17:33:16 +01:00
parent 0c1ce056eb
commit 5a1d7911f3
2 changed files with 25 additions and 9 deletions

View File

@@ -1,2 +1,2 @@
PISA_API_SERVER = 'localhost'
PISA_API_PORT = 2222
DEFAULT_PISA_API_SERVER = 'localhost'
DEFAULT_PISA_API_PORT = 2222

View File

@@ -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.")