diff --git a/deluge/tests/test_web_api.py b/deluge/tests/test_web_api.py index 814fecf8c9..4dfa58d7c3 100644 --- a/deluge/tests/test_web_api.py +++ b/deluge/tests/test_web_api.py @@ -78,27 +78,28 @@ def test_set_config(self): @defer.inlineCallbacks def get_host_status(self): - host = list(self.deluge_web.web_api._get_host(self.host_id)) + host = list(self.deluge_web.web_api.hostlist.get_host_info(self.host_id)) host[3] = 'Online' host[4] = '2.0.0.dev562' status = yield self.deluge_web.web_api.get_host_status(self.host_id) assert status == tuple(status) - def test_get_host(self): - assert not self.deluge_web.web_api._get_host('invalid_id') - conn = list(self.deluge_web.web_api.hostlist.get_hosts_info()[0]) - assert self.deluge_web.web_api._get_host(conn[0]) == conn[0:4] + def test_get_hosts(self): + hosts = self.deluge_web.web_api.hostlist.get_hosts_info() + assert self.deluge_web.web_api.get_hosts() == hosts def test_add_host(self): conn = ['abcdef', '10.0.0.1', 0, 'user123', 'pass123'] - assert not self.deluge_web.web_api._get_host(conn[0]) + assert not self.deluge_web.web_api.hostlist.get_host_info(conn[0]) # Add valid host result, host_id = self.deluge_web.web_api.add_host( conn[1], conn[2], conn[3], conn[4] ) assert result conn[0] = host_id - assert self.deluge_web.web_api._get_host(conn[0]) == conn[0:4] + assert ( + list(self.deluge_web.web_api.hostlist.get_host_info(conn[0])) == conn[0:4] + ) # Add already existing host ret = self.deluge_web.web_api.add_host(conn[1], conn[2], conn[3], conn[4]) @@ -112,10 +113,10 @@ def test_add_host(self): def test_remove_host(self): conn = ['connection_id', '', 0, '', ''] self.deluge_web.web_api.hostlist.config['hosts'].append(conn) - assert self.deluge_web.web_api._get_host(conn[0]) == conn[0:4] + assert self.deluge_web.web_api.hostlist.get_host_info(conn[0]) == conn[0:4] # Remove valid host assert self.deluge_web.web_api.remove_host(conn[0]) - assert not self.deluge_web.web_api._get_host(conn[0]) + assert not self.deluge_web.web_api.hostlist.get_host_info(conn[0]) # Remove non-existing host assert not self.deluge_web.web_api.remove_host(conn[0]) diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index 5f4b3dcb8a..18c370a269 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -22,9 +22,10 @@ from deluge import component, httpdownloader from deluge.common import AUTH_LEVEL_DEFAULT, get_magnet_info, is_magnet from deluge.configmanager import get_config_dir +from deluge.decorators import maybe_coroutine from deluge.error import NotAuthorizedError from deluge.i18n import get_languages -from deluge.ui.client import Client, client +from deluge.ui.client import client from deluge.ui.common import FileTree2, TorrentInfo from deluge.ui.coreconfig import CoreConfig from deluge.ui.hostlist import HostList @@ -744,18 +745,6 @@ def add_torrents(self, torrents): deferreds.append(d) return DeferredList(deferreds, consumeErrors=False) - def _get_host(self, host_id): - """Information about a host from supplied host id. - - Args: - host_id (str): The id of the host. - - Returns: - list: The host information, empty list if not found. - - """ - return list(self.hostlist.get_host_info(host_id)) - @export def get_hosts(self): """ @@ -838,39 +827,18 @@ def start_daemon(self, port): client.start_daemon(port, get_config_dir()) @export - def stop_daemon(self, host_id): - """ - Stops a running daemon. - - :param host_id: the hash id of the host - :type host_id: string - """ - main_deferred = Deferred() - host = self._get_host(host_id) - if not host: - main_deferred.callback((False, _('Daemon does not exist'))) - return main_deferred - + @maybe_coroutine + async def stop_daemon(self, host_id): try: + await self.hostlist.connect_host(host_id) + except Exception as err: + msg = f'Error occurred stopping daemon: {err}' + result = (False, msg) + else: + client.daemon.shutdown() + result = (True,) - def on_connect(connected, c): - if not connected: - main_deferred.callback((False, _('Daemon not running'))) - return - c.daemon.shutdown() - main_deferred.callback((True,)) - - def on_connect_failed(reason): - main_deferred.callback((False, reason)) - - host, port, user, password = host[1:5] - c = Client() - d = c.connect(host, port, user, password) - d.addCallback(on_connect, c) - d.addErrback(on_connect_failed) - except Exception: - main_deferred.callback((False, 'An error occurred')) - return main_deferred + return Deferred().callback(result) @export def get_config(self):