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:
from etc import dependencycheck # noqa
except ModuleNotFoundError as e:
print('Onionr needs ' + str(e) + ' installed')
print('Missing requirement: ' + str(e) + ' installed')
sys.exit(1)
# Import 3rd party libraries

View File

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

View File

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

View File

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

View File

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

View File

@ -52,7 +52,7 @@ def restart():
with open(filepaths.restarting_indicator, 'w') as f:
f.write('t')
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(15)
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',
post=True,
is_json=True,
postData={},
maxWait=300
post_data={},
max_wait=300
).get(300)

View File

@ -4,7 +4,6 @@ send a command to the local API server
"""
import urllib
import time
from typing import TYPE_CHECKING, Callable
import requests
import deadsimplekv
@ -37,7 +36,7 @@ cache = deadsimplekv.DeadSimpleKV(filepaths.cached_storage,
def get_hostname():
hostname = ''
waited = 0
maxWait = 3
max_wait = 3
while True:
if cache.get('client_api') is None:
try:
@ -51,22 +50,22 @@ def get_hostname():
hostname = cache.get('hostname')
if hostname == '' or hostname is None:
time.sleep(1)
if waited == maxWait:
if waited == max_wait:
return False
else:
return hostname
def local_command(command, data='', silent = True, post=False,
postData = {}, maxWait=20,
def local_command(command, data='', silent=True, post=False,
post_data={}, max_wait=20,
is_json=False
):
"""
Send a command to the local http API server, securely. 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.
"""Send a command to the local http API server, securely.
Intended for local clients, DO NOT USE for remote peers."""
hostname = get_hostname()
if hostname == False: return False
# if the api host cannot be reached, return False
if not hostname:
return False
if data != '':
data = '&data=' + urllib.parse.quote_plus(data)
@ -79,21 +78,26 @@ def local_command(command, data='', silent = True, post=False,
if is_json:
ret_data = requests.post(
payload,
json=postData,
json=post_data,
headers={'token': config.get('client.webpassword'),
'Connection': 'close'},
timeout=(maxWait, maxWait)).text
timeout=(max_wait, max_wait)).text
else:
ret_data = requests.post(
payload,
data=postData,
data=post_data,
headers={'token': config.get('client.webpassword'),
'Connection': 'close'},
timeout=(maxWait, maxWait)).text
timeout=(max_wait, max_wait)).text
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:
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
return ret_data

View File

@ -55,10 +55,11 @@ def sendto():
msg = ''
else:
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')
@direct_blueprint.route('/chat/poll')
def poll_chat():
"""Endpoints peers get new messages from"""
return Response(localcommand.local_command('/chat/gets/%s' % (g.peer,)))