http: Give routers access to the core API
This commit is contained in:
parent
b1d9e112fe
commit
5d1f8f2203
@ -50,6 +50,8 @@ class Router(object):
|
||||
extension registry under the ``http:router`` key.
|
||||
|
||||
:param config: dict structure of the entire Mopidy configuration
|
||||
:param core: :class:`pykka.ActorProxy` to the core actor, giving full
|
||||
access to the core API
|
||||
"""
|
||||
|
||||
name = None
|
||||
@ -74,8 +76,9 @@ class Router(object):
|
||||
If you override :meth:`get_request_handlers` this attribute is not used.
|
||||
"""
|
||||
|
||||
def __init__(self, config):
|
||||
def __init__(self, config, core):
|
||||
self.config = config
|
||||
self.core = core
|
||||
self.hostname = config['http']['hostname']
|
||||
self.port = config['http']['port']
|
||||
if not self.name:
|
||||
|
||||
@ -90,7 +90,7 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
|
||||
def _get_extension_request_handlers(self):
|
||||
request_handlers = []
|
||||
for router_class in self.routers:
|
||||
router = router_class(self.config)
|
||||
router = router_class(self.config, self.core)
|
||||
request_handlers.extend(router.get_request_handlers())
|
||||
logger.info(
|
||||
'Loaded HTTP extension: %s', router_class.__name__)
|
||||
|
||||
@ -6,7 +6,7 @@ import unittest
|
||||
import mock
|
||||
|
||||
from mopidy import http
|
||||
from mopidy.http import actor, handlers
|
||||
from mopidy.http import handlers
|
||||
|
||||
|
||||
class TestRouter(http.Router):
|
||||
@ -32,10 +32,16 @@ class HttpRouterTest(unittest.TestCase):
|
||||
'zeroconf': '',
|
||||
}
|
||||
}
|
||||
self.http = actor.HttpFrontend(config=self.config, core=mock.Mock())
|
||||
self.core = mock.Mock()
|
||||
|
||||
def test_default_router(self):
|
||||
router = TestRouter(self.config)
|
||||
def test_keeps_reference_to_config_and_core(self):
|
||||
router = TestRouter(self.config, self.core)
|
||||
|
||||
self.assertIs(router.config, self.config)
|
||||
self.assertIs(router.core, self.core)
|
||||
|
||||
def test_default_request_handlers(self):
|
||||
router = TestRouter(self.config, self.core)
|
||||
|
||||
(pattern, handler_class, kwargs) = router.get_request_handlers()[0]
|
||||
|
||||
@ -46,13 +52,15 @@ class HttpRouterTest(unittest.TestCase):
|
||||
|
||||
def test_default_router_missing_name(self):
|
||||
with self.assertRaises(ValueError):
|
||||
TestRouterMissingName(self.config)
|
||||
TestRouterMissingName(self.config, self.core)
|
||||
|
||||
def test_default_router_missing_path(self):
|
||||
router = TestRouterMissingPath(self.config, self.core)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
TestRouterMissingPath(self.config).get_request_handlers()
|
||||
router.get_request_handlers()
|
||||
|
||||
def test_get_root_url(self):
|
||||
router = TestRouter(self.config)
|
||||
router = TestRouter(self.config, self.core)
|
||||
|
||||
self.assertEqual('http://127.0.0.1:6680/test/', router.get_root_url())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user