예제 #1
0
    async def route_req(self, table_name: str, key: K, web: Web,
                        request: Request) -> Response:
        """Route request to worker having key in table.

        Arguments:
            table_name: Name of the table.
            key: The key that we want.
            web: The currently sued web driver,
            request: The web request currently being served.
        """
        app = self.app
        try:
            dest_url: URL = app.router.key_store(table_name, key)
        except KeyError:
            raise ServiceUnavailable()
        dest_ident = (host, port) = self._urlident(dest_url)
        if dest_ident == self._urlident(app.conf.canonical_url):
            raise SameNode()
        routed_url = request.url.with_host(host).with_port(int(port))
        async with app.http_client.get(routed_url) as response:
            return web.text(
                await response.text(),
                content_type=response.content_type,
                status=response.status,
            )
예제 #2
0
파일: router.py 프로젝트: zhouyuking/faust
 async def route_req(self, table_name: str, key: K, web: Web,
                     request: Request) -> Response:
     app = self.app
     dest_url: URL = app.router.key_store(table_name, key)
     dest_ident = (host, port) = self._urlident(dest_url)
     if dest_ident == self._urlident(app.conf.canonical_url):
         raise SameNode()
     routed_url = request.url.with_host(host).with_port(int(port))
     async with app.http_client.get(routed_url) as response:
         return web.text(await response.text(),
                         content_type=response.content_type)
예제 #3
0
파일: router.py 프로젝트: taybin/faust
 async def _route_req(self, dest_url: URL, web: Web, request: Request) -> Response:
     app = self.app
     dest_ident = (host, port) = self._urlident(dest_url)
     if dest_ident == self._urlident(app.conf.canonical_url):
         raise SameNode()
     routed_url = request.url.with_host(host).with_port(int(port))
     async with app.http_client.request(request.method, routed_url) as response:
         return web.text(
             await response.text(),
             content_type=response.content_type,
             status=response.status,
         )
예제 #4
0
파일: test_base.py 프로젝트: bingtel/faust
    async def test_table_route__exact_key(self, *, app):
        table = app.Table('foo')
        view = Mock()
        request = Mock()
        app.router.route_req = AsyncMock()

        @app.table_route(table, exact_key='active')
        async def routed(self, request):
            return 42

        ret = await routed(view, request)
        assert ret is app.router.route_req.coro.return_value

        app.router.route_req.coro.side_effect = SameNode()
        ret = await routed(view, request)
        assert ret == 42
예제 #5
0
파일: test_base.py 프로젝트: taybin/faust
    async def test_topic_route__exact_key(self, *, app):
        topic = app.topic("foo")
        view = Mock()
        request = Mock()
        app.router.route_topic_req = AsyncMock()

        @app.topic_route(topic, exact_key="active")
        async def routed(self, request):
            return 42

        ret = await routed(view, request)
        assert ret is app.router.route_topic_req.coro.return_value

        app.router.route_topic_req.coro.side_effect = SameNode()
        ret = await routed(view, request)
        assert ret == 42
예제 #6
0
파일: test_base.py 프로젝트: bingtel/faust
    async def test_table_route__match_info(self, *, app):
        table = app.Table('foo')
        view = Mock()
        request = Mock()
        app.router.route_req = AsyncMock()

        @app.table_route(table, match_info='q')
        async def routed(self, request):
            return 42

        request.match_info = {'q': 'KEY'}

        ret = await routed(view, request)
        assert ret is app.router.route_req.coro.return_value

        app.router.route_req.coro.side_effect = SameNode()
        ret = await routed(view, request)
        assert ret == 42
예제 #7
0
    async def test_table_route__query_param(self, *, app):
        table = app.Table("foo")
        view = Mock()
        request = Mock()
        app.router.route_req = AsyncMock()

        @app.table_route(table, query_param="q")
        async def routed(self, request):
            return 42

        request.query = {"q": "KEY"}

        ret = await routed(view, request)
        assert ret is app.router.route_req.coro.return_value

        app.router.route_req.coro.side_effect = SameNode()
        ret = await routed(view, request)
        assert ret == 42
예제 #8
0
파일: test_base.py 프로젝트: taybin/faust
    async def test_topic_route__match_info(self, *, app):
        topic = app.topic("foo")
        view = Mock()
        request = Mock()
        app.router.route_topic_req = AsyncMock()

        @app.topic_route(topic, match_info="q")
        async def routed(self, request):
            return 42

        request.match_info = {"q": "KEY"}

        ret = await routed(view, request)
        assert ret is app.router.route_topic_req.coro.return_value

        app.router.route_topic_req.coro.side_effect = SameNode()
        ret = await routed(view, request)
        assert ret == 42
예제 #9
0
def router_same(app):
    app.router.route_req = Mock(name='app.router.route_req')
    app.router.route_req.side_effect = SameNode()
    return app.router