made localcommand pep8 compliant

This commit is contained in:
Kevin 2020-07-17 13:49:18 -05:00
parent 782c980b69
commit 9fc741106a
9 changed files with 34 additions and 28 deletions

View File

@ -39,7 +39,8 @@ if __name__ == "__main__": ran_as_script = True
try: try:
from etc import dependencycheck # noqa from etc import dependencycheck # noqa
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
print('Onionr needs ' + str(e) + ' installed') print('Missing requirement: ' + str(e) + ' installed')
sys.exit(1)
# Import 3rd party libraries # Import 3rd party libraries

View File

@ -123,7 +123,7 @@ def download_blocks_from_communicator(comm_inst: "OnionrCommunicatorDaemon"):
f'/daemon-event/upload_event', f'/daemon-event/upload_event',
post=True, post=True,
is_json=True, is_json=True,
postData={'block': blockHash} post_data={'block': blockHash}
) )
else: else:
logger.warn('POW failed for block %s.' % (blockHash,)) logger.warn('POW failed for block %s.' % (blockHash,))

View File

@ -46,7 +46,7 @@ def accept_upload(request):
f'/daemon-event/upload_event', f'/daemon-event/upload_event',
post=True, post=True,
is_json=True, is_json=True,
postData={'block': b_hash} post_data={'block': b_hash}
).get(timeout=10) ).get(timeout=10)
resp = 'success' resp = 'success'
else: else:

View File

@ -30,5 +30,5 @@ def rebuild():
f'/daemon-event/restart_tor', f'/daemon-event/restart_tor',
post=True, post=True,
is_json=True, is_json=True,
postData={} post_data={}
).get(10) ).get(10)

View File

@ -46,7 +46,7 @@ def _check_upload_queue():
raises OverflowError if max, false if api not running raises OverflowError if max, false if api not running
""" """
max_upload_queue: int = 5000 max_upload_queue: int = 5000
queue = localcommand.local_command('/gethidden', maxWait=10) queue = localcommand.local_command('/gethidden', max_wait=10)
up_queue = False up_queue = False
try: try:
@ -231,7 +231,7 @@ def insert_block(data: Union[str, bytes], header: str = 'txt',
'/daemon-event/upload_event', '/daemon-event/upload_event',
post=True, post=True,
is_json=True, is_json=True,
postData={'block': retData} post_data={'block': retData}
).get(timeout=5) ).get(timeout=5)
coredb.blockmetadb.add.add_to_block_DB( coredb.blockmetadb.add.add_to_block_DB(
retData, selfInsert=True, dataSaved=True) retData, selfInsert=True, dataSaved=True)
@ -268,7 +268,7 @@ def insert_block(data: Union[str, bytes], header: str = 'txt',
localcommand.local_command, localcommand.local_command,
'/daemon-event/remove_from_insert_queue_wrapper', '/daemon-event/remove_from_insert_queue_wrapper',
post=True, post=True,
postData={'block_hash': retData}, post_data={'block_hash': retData},
is_json=True is_json=True
).get(timeout=5) ).get(timeout=5)
return retData return retData

View File

@ -52,7 +52,7 @@ def restart():
with open(filepaths.restarting_indicator, 'w') as f: with open(filepaths.restarting_indicator, 'w') as f:
f.write('t') f.write('t')
daemonlaunch.kill_daemon() daemonlaunch.kill_daemon()
while localcommand.local_command('ping', maxWait=8) == 'pong!': while localcommand.local_command('ping', max_wait=8) == 'pong!':
time.sleep(0.3) time.sleep(0.3)
time.sleep(15) time.sleep(15)
while (os.path.exists(filepaths.private_API_host_file) or while (os.path.exists(filepaths.private_API_host_file) or

View File

@ -28,8 +28,8 @@ def do_runtime_test():
f'daemon-event/test_runtime', f'daemon-event/test_runtime',
post=True, post=True,
is_json=True, is_json=True,
postData={}, post_data={},
maxWait=300 max_wait=300
).get(300) ).get(300)

View File

@ -4,7 +4,6 @@ send a command to the local API server
""" """
import urllib import urllib
import time import time
from typing import TYPE_CHECKING, Callable
import requests import requests
import deadsimplekv import deadsimplekv
@ -37,7 +36,7 @@ cache = deadsimplekv.DeadSimpleKV(filepaths.cached_storage,
def get_hostname(): def get_hostname():
hostname = '' hostname = ''
waited = 0 waited = 0
maxWait = 3 max_wait = 3
while True: while True:
if cache.get('client_api') is None: if cache.get('client_api') is None:
try: try:
@ -51,22 +50,22 @@ def get_hostname():
hostname = cache.get('hostname') hostname = cache.get('hostname')
if hostname == '' or hostname is None: if hostname == '' or hostname is None:
time.sleep(1) time.sleep(1)
if waited == maxWait: if waited == max_wait:
return False return False
else: else:
return hostname return hostname
def local_command(command, data='', silent = True, post=False, def local_command(command, data='', silent=True, post=False,
postData = {}, maxWait=20, post_data={}, max_wait=20,
is_json=False is_json=False
): ):
""" """Send a command to the local http API server, securely.
Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers. Intended for local clients, DO NOT USE for remote peers."""
"""
# TODO: URL encode parameters, just as an extra measure. May not be needed, but should be added regardless.
hostname = get_hostname() hostname = get_hostname()
if hostname == False: return False # if the api host cannot be reached, return False
if not hostname:
return False
if data != '': if data != '':
data = '&data=' + urllib.parse.quote_plus(data) data = '&data=' + urllib.parse.quote_plus(data)
@ -79,21 +78,26 @@ def local_command(command, data='', silent = True, post=False,
if is_json: if is_json:
ret_data = requests.post( ret_data = requests.post(
payload, payload,
json=postData, json=post_data,
headers={'token': config.get('client.webpassword'), headers={'token': config.get('client.webpassword'),
'Connection': 'close'}, 'Connection': 'close'},
timeout=(maxWait, maxWait)).text timeout=(max_wait, max_wait)).text
else: else:
ret_data = requests.post( ret_data = requests.post(
payload, payload,
data=postData, data=post_data,
headers={'token': config.get('client.webpassword'), headers={'token': config.get('client.webpassword'),
'Connection': 'close'}, 'Connection': 'close'},
timeout=(maxWait, maxWait)).text timeout=(max_wait, max_wait)).text
else: else:
ret_data = requests.get(payload, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text ret_data = requests.get(payload,
headers={'token':
config.get('client.webpassword'),
'Connection': 'close'},
timeout=(max_wait, max_wait)).text
except Exception as error: except Exception as error:
if not silent: if not silent:
logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True) logger.error('Failed to make local request (command: %s):%s' %
(command, error), terminal=True)
ret_data = False ret_data = False
return ret_data return ret_data

View File

@ -55,10 +55,11 @@ def sendto():
msg = '' msg = ''
else: else:
msg = json.dumps(msg) msg = json.dumps(msg)
localcommand.local_command('/chat/addrec/%s' % (g.peer,), post=True, postData=msg) localcommand.local_command('/chat/addrec/%s' % (g.peer,), post=True, post_data=msg)
return Response('success') return Response('success')
@direct_blueprint.route('/chat/poll') @direct_blueprint.route('/chat/poll')
def poll_chat(): def poll_chat():
"""Endpoints peers get new messages from""" """Endpoints peers get new messages from"""
return Response(localcommand.local_command('/chat/gets/%s' % (g.peer,))) return Response(localcommand.local_command('/chat/gets/%s' % (g.peer,)))