Ping the RPC api before using the wot CLI

This commit is contained in:
Kevin F 2022-12-02 21:41:48 +00:00
parent b3eb0caffd
commit 738fa0c361

View File

@ -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