fixes for named parameter changes on api

This commit is contained in:
jobevers 2017-02-01 20:40:03 -08:00
parent 4ab5ace668
commit 8e349f17c1
2 changed files with 29 additions and 56 deletions

View file

@ -115,11 +115,6 @@ class NoValidSearch(Exception):
pass pass
class Parameters(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
class CheckInternetConnection(object): class CheckInternetConnection(object):
def __init__(self, daemon): def __init__(self, daemon):
self.daemon = daemon self.daemon = daemon
@ -490,9 +485,9 @@ class Daemon(AuthJSONRPCServer):
def _stop_server(self): def _stop_server(self):
try: try:
if self.lbry_server_port is not None: if self.lbry_server_port is not None:
self.lbry_server_port, p = None, self.lbry_server_port self.lbry_server_port, old_port = None, self.lbry_server_port
log.info('Stop listening to %s', p) log.info('Stop listening to %s', old_port)
return defer.maybeDeferred(p.stopListening) return defer.maybeDeferred(old_port.stopListening)
else: else:
return defer.succeed(True) return defer.succeed(True)
except AttributeError: except AttributeError:
@ -1471,62 +1466,40 @@ class Daemon(AuthJSONRPCServer):
d.addCallback(lambda r: self._render_response(r)) d.addCallback(lambda r: self._render_response(r))
return d return d
def _process_get_parameters(self, p):
"""Extract info from input parameters and fill in default values for `get` call."""
# TODO: this process can be abstracted s.t. each method
# can spec what parameters it expects and how to set default values
timeout = p.get('timeout', self.download_timeout)
download_directory = p.get('download_directory', self.download_directory)
file_name = p.get(FileID.FILE_NAME)
stream_info = p.get('stream_info')
sd_hash = get_sd_hash(stream_info)
wait_for_write = p.get('wait_for_write', True)
name = p.get(FileID.NAME)
return Parameters(
timeout=timeout,
download_directory=download_directory,
file_name=file_name,
stream_info=stream_info,
sd_hash=sd_hash,
wait_for_write=wait_for_write,
name=name
)
@AuthJSONRPCServer.auth_required @AuthJSONRPCServer.auth_required
@defer.inlineCallbacks @defer.inlineCallbacks
def jsonrpc_get(self, **kwargs): def jsonrpc_get(
self, name, file_name=None, stream_info=None, timeout=None,
download_directory=None, wait_for_write=True):
""" """
Download stream from a LBRY uri. Download stream from a LBRY uri.
Args: Args:
'name': name to download, string 'name': name to download, string
'download_directory': optional, path to directory where file will be saved, string
'file_name': optional, a user specified name for the downloaded file 'file_name': optional, a user specified name for the downloaded file
'stream_info': optional, specified stream info overrides name 'stream_info': optional, specified stream info overrides name
'timeout': optional 'timeout': optional
'download_directory': optional, path to directory where file will be saved, string
'wait_for_write': optional, defaults to True. When set, waits for the file to 'wait_for_write': optional, defaults to True. When set, waits for the file to
only start to be written before returning any results. only start to be written before returning any results.
Returns: Returns:
'stream_hash': hex string 'stream_hash': hex string
'path': path of download 'path': path of download
""" """
params = self._process_get_parameters(kwargs) timeout = timeout if timeout is not None else self.download_timeout
if not params.name: download_directory = download_directory or self.download_directory
# TODO: return a useful error message here, like "name argument is required" sd_hash = get_sd_hash(stream_info)
defer.returnValue(server.failure) if name in self.waiting_on:
if params.name in self.waiting_on:
# TODO: return a useful error message here, like "already # TODO: return a useful error message here, like "already
# waiting for name to be resolved" # waiting for name to be resolved"
defer.returnValue(server.failure) defer.returnValue(server.failure)
name = params.name
stream_info = params.stream_info
# first check if we already have this # first check if we already have this
lbry_file = yield self._get_lbry_file(FileID.NAME, name, return_json=False) lbry_file = yield self._get_lbry_file(FileID.NAME, name, return_json=False)
if lbry_file: if lbry_file:
log.info('Already have a file for %s', name) log.info('Already have a file for %s', name)
message = { message = {
'stream_hash': params.sd_hash if params.stream_info else lbry_file.sd_hash, 'stream_hash': sd_hash if stream_info else lbry_file.sd_hash,
'path': os.path.join(lbry_file.download_directory, lbry_file.file_name) 'path': os.path.join(lbry_file.download_directory, lbry_file.file_name)
} }
response = yield self._render_response(message) response = yield self._render_response(message)
@ -1539,18 +1512,18 @@ class Daemon(AuthJSONRPCServer):
while tries <= max_tries: while tries <= max_tries:
try: try:
log.info( log.info(
'Making try %s / %s to start download of %s', tries, max_tries, params.name) 'Making try %s / %s to start download of %s', tries, max_tries, name)
sd_hash, file_path = yield self._download_name( new_sd_hash, file_path = yield self._download_name(
name=params.name, name=name,
timeout=params.timeout, timeout=timeout,
download_directory=params.download_directory, download_directory=download_directory,
stream_info=params.stream_info, stream_info=stream_info,
file_name=params.file_name, file_name=file_name,
wait_for_write=params.wait_for_write wait_for_write=wait_for_write
) )
break break
except Exception as e: except Exception as e:
log.exception('Failed to get %s', params.name) log.exception('Failed to get %s', name)
if tries == max_tries: if tries == max_tries:
self.analytics_manager.send_download_errored(download_id, name, stream_info) self.analytics_manager.send_download_errored(download_id, name, stream_info)
response = yield self._render_response(str(e)) response = yield self._render_response(str(e))
@ -1558,7 +1531,7 @@ class Daemon(AuthJSONRPCServer):
tries += 1 tries += 1
# TODO: should stream_hash key be changed to sd_hash? # TODO: should stream_hash key be changed to sd_hash?
message = { message = {
'stream_hash': params.sd_hash if params.stream_info else sd_hash, 'stream_hash': sd_hash if stream_info else new_sd_hash,
'path': file_path 'path': file_path
} }
stream = self.streams.get(name) stream = self.streams.get(name)
@ -1602,7 +1575,7 @@ class Daemon(AuthJSONRPCServer):
if status not in ['start', 'stop']: if status not in ['start', 'stop']:
raise Exception('Status must be "start" or "stop".') raise Exception('Status must be "start" or "stop".')
search_type, value = get_lbry_file_search_value(**kwargs) search_type, value = get_lbry_file_search_value(kwargs)
lbry_file = yield self._get_lbry_file(search_type, value, return_json=False) lbry_file = yield self._get_lbry_file(search_type, value, return_json=False)
if not lbry_file: if not lbry_file:
raise Exception('Unable to find a file for {}:{}'.format(search_type, value)) raise Exception('Unable to find a file for {}:{}'.format(search_type, value))
@ -1618,11 +1591,11 @@ class Daemon(AuthJSONRPCServer):
defer.returnValue(response) defer.returnValue(response)
@AuthJSONRPCServer.auth_required @AuthJSONRPCServer.auth_required
def jsonrpc_delete_lbry_file(self, p): def jsonrpc_delete_lbry_file(self, **kwargs):
""" """
DEPRECATED. Use `file_delete` instead DEPRECATED. Use `file_delete` instead
""" """
return self.jsonrpc_file_delete(p) return self.jsonrpc_file_delete(**kwargs)
@AuthJSONRPCServer.auth_required @AuthJSONRPCServer.auth_required
def jsonrpc_file_delete(self, delete_target_file=True, **kwargs): def jsonrpc_file_delete(self, delete_target_file=True, **kwargs):
@ -2730,12 +2703,12 @@ def report_bug_to_slack(message, lbry_id, platform_name, app_version):
requests.post(webhook, json.dumps(payload)) requests.post(webhook, json.dumps(payload))
def get_lbry_file_search_value(p): def get_lbry_file_search_value(search_fields):
for searchtype in (FileID.SD_HASH, FileID.NAME, FileID.FILE_NAME): for searchtype in (FileID.SD_HASH, FileID.NAME, FileID.FILE_NAME):
value = p.get(searchtype) value = search_fields.get(searchtype)
if value: if value:
return searchtype, value return searchtype, value
raise NoValidSearch('{} is missing a valid search type'.format(p)) raise NoValidSearch('{} is missing a valid search type'.format(search_fields))
def run_reflector_factory(factory): def run_reflector_factory(factory):

View file

@ -83,7 +83,7 @@ class HostedEncryptedFile(resource.Resource):
if 'name' in request.args.keys(): if 'name' in request.args.keys():
if self.is_valid_request_name(request): if self.is_valid_request_name(request):
name = request.args['name'][0] name = request.args['name'][0]
d = self._api.jsonrpc_get({'name': name}) d = self._api.jsonrpc_get(name=name)
d.addCallback(lambda response: response['stream_hash']) d.addCallback(lambda response: response['stream_hash'])
d.addCallback(lambda sd_hash: self._api._get_lbry_file_by_sd_hash(sd_hash)) d.addCallback(lambda sd_hash: self._api._get_lbry_file_by_sd_hash(sd_hash))
d.addCallback(lambda lbry_file: self._make_stream_producer(request, lbry_file)) d.addCallback(lambda lbry_file: self._make_stream_producer(request, lbry_file))