mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-18 14:44:21 +01:00
Adds support for command line api host and port.
Defaults to __init__ parameters if missing
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
PISA_API_SERVER = 'localhost'
|
DEFAULT_PISA_API_SERVER = 'localhost'
|
||||||
PISA_API_PORT = 2222
|
DEFAULT_PISA_API_PORT = 2222
|
||||||
@@ -8,9 +8,9 @@ import logging
|
|||||||
from conf import CLIENT_LOG_FILE
|
from conf import CLIENT_LOG_FILE
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
from binascii import unhexlify
|
from binascii import unhexlify
|
||||||
|
|
||||||
from apps.blob import Blob
|
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']
|
commands = ['add_appointment']
|
||||||
@@ -51,9 +51,11 @@ def show_usage():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
debug = False
|
debug = False
|
||||||
|
pisa_api_server = DEFAULT_PISA_API_SERVER
|
||||||
|
pisa_api_port = DEFAULT_PISA_API_PORT
|
||||||
command = None
|
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:
|
for opt, arg in opts:
|
||||||
if opt in ['-a', '--add_appointment']:
|
if opt in ['-a', '--add_appointment']:
|
||||||
if arg:
|
if arg:
|
||||||
@@ -67,6 +69,14 @@ if __name__ == '__main__':
|
|||||||
if opt in ['-d', '--debug']:
|
if opt in ['-d', '--debug']:
|
||||||
debug = True
|
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
|
# Configure logging
|
||||||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, handlers=[
|
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, handlers=[
|
||||||
logging.FileHandler(CLIENT_LOG_FILE),
|
logging.FileHandler(CLIENT_LOG_FILE),
|
||||||
@@ -79,7 +89,7 @@ if __name__ == '__main__':
|
|||||||
valid_locator = check_txid_format(appointment_data.get('tx_id'))
|
valid_locator = check_txid_format(appointment_data.get('tx_id'))
|
||||||
|
|
||||||
if valid_locator:
|
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 = build_appointment(appointment_data.get('tx'), appointment_data.get('tx_id'),
|
||||||
appointment_data.get('start_time'), appointment_data.get('end_time'),
|
appointment_data.get('start_time'), appointment_data.get('end_time'),
|
||||||
appointment_data.get('dispute_delta'), debug, logging)
|
appointment_data.get('dispute_delta'), debug, logging)
|
||||||
@@ -87,10 +97,16 @@ if __name__ == '__main__':
|
|||||||
if debug:
|
if debug:
|
||||||
logging.info("[Client] sending appointment to PISA")
|
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:
|
else:
|
||||||
raise ValueError("The provided locator is not valid.")
|
raise ValueError("The provided locator is not valid.")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user