From 738fa0c361b542b1d11efcb45e98a61f76528cae Mon Sep 17 00:00:00 2001 From: Kevin F Date: Fri, 2 Dec 2022 21:41:48 +0000 Subject: [PATCH] Ping the RPC api before using the wot CLI --- .../official-plugins/wot/cli/__init__.py | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/static-data/official-plugins/wot/cli/__init__.py b/static-data/official-plugins/wot/cli/__init__.py index 0bbd12a4..5973a7f7 100644 --- a/static-data/official-plugins/wot/cli/__init__.py +++ b/static-data/official-plugins/wot/cli/__init__.py @@ -1,7 +1,10 @@ import tty import sys import subprocess +import traceback +import ujson as json +import result import requests import requests_unixsocket @@ -23,10 +26,28 @@ def list_idens(): print('Listing identities') payload = dict(rpc_payload) payload['method'] = 'wot.serialize_identity_set' - payload['params'].clear() - print(onionrplugins.pluginapis.plugin_apis['rpc.rpc_client'](payload).text) + del payload['params'] + print(onionrplugins.pluginapis.plugin_apis['rpc.rpc_client'](json=payload).text) +def ping_api() -> result.Result: + payload = dict(rpc_payload) + payload['method'] = 'ping' + del payload['params'] + try: + _ping_res = onionrplugins.pluginapis.plugin_apis['rpc.rpc_client'](json=payload).text + except requests.exceptions.ConnectionError: + logging.debug(traceback.format_exc()) + return result.Err('Could not connect to Onionr RPC server. Please ensure the RPC plugin is enabled and the Onionr daemon is running') + except: + logging.error(traceback.format_exc()) + return result.Err('Unknown error occurred while connecting to Onionr RPC server') + _ping_res = json.loads(_ping_res) + if _ping_res['result'] == 'pong': + return result.Ok() + else: + return result.Err('API not responding. Try restarting Onionr') + main_menu = { 'l': (list_idens, 'list trusted identities'), @@ -40,11 +61,17 @@ def main_ui(): except KeyError: logging.error("Web of trust CLI requires RPC plugin to be enabled") return + + ping_result: result.Result = ping_api() + if not isinstance(ping_result, result.Ok): + logging.error(ping_result) + return + while True: # move cursor to the beginning print('\r', end='') - key = sys.stdin.read(1) try: + key = sys.stdin.read(1) main_menu[key][0]() except KeyError: pass