From 26d390b66ea08f6ff43838034183dfe12a407b40 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Tue, 16 Jan 2018 12:20:04 +0100 Subject: [PATCH] Add GET param support (#314) --- hassio/api/__init__.py | 4 ++- hassio/api/proxy.py | 74 +++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/hassio/api/__init__.py b/hassio/api/__init__.py index 39b01cfc8..80df80edf 100644 --- a/hassio/api/__init__.py +++ b/hassio/api/__init__.py @@ -100,12 +100,14 @@ class RestAPI(CoreSysAttributes): '/homeassistant/api/websocket', api_proxy.websocket) self.webapp.router.add_get( '/homeassistant/websocket', api_proxy.websocket) + self.webapp.router.add_get( + '/homeassistant/api/stream', api_proxy.stream) self.webapp.router.add_post( '/homeassistant/api/{path:.+}', api_proxy.api) self.webapp.router.add_get( '/homeassistant/api/{path:.+}', api_proxy.api) self.webapp.router.add_get( - '/homeassistant/api', api_proxy.api) + '/homeassistant/api/', api_proxy.api) def _register_addons(self): """Register homeassistant function.""" diff --git a/hassio/api/proxy.py b/hassio/api/proxy.py index 4ea7cb58a..9b25ec618 100644 --- a/hassio/api/proxy.py +++ b/hassio/api/proxy.py @@ -25,6 +25,7 @@ class APIProxy(CoreSysAttributes): data = None headers = {} method = getattr(self._websession_ssl, request.method.lower()) + params = request.query or None # read data with async_timeout.timeout(30, loop=self._loop): @@ -42,7 +43,8 @@ class APIProxy(CoreSysAttributes): headers = None client = await method( - url, data=data, headers=headers, timeout=timeout + url, data=data, headers=headers, timeout=timeout, + params=params ) return client @@ -55,48 +57,46 @@ class APIProxy(CoreSysAttributes): raise HTTPBadGateway() + async def stream(self, request): + """Proxy HomeAssistant EventStream Requests.""" + _LOGGER.info("Home-Assistant EventStream start") + client = await self._api_client(request, 'stream', timeout=None) + + response = web.StreamResponse() + response.content_type = request.headers.get(CONTENT_TYPE) + try: + await response.prepare(request) + while True: + data = await client.content.read(10) + if not data: + await response.write_eof() + break + response.write(data) + + except aiohttp.ClientError: + await response.write_eof() + + except asyncio.CancelledError: + pass + + finally: + client.close() + _LOGGER.info("Home-Assistant EventStream close") + async def api(self, request): """Proxy HomeAssistant API Requests.""" path = request.match_info.get('path', '') - # API stream - if path.startswith("stream"): - _LOGGER.info("Home-Assistant Event-Stream start") - client = await self._api_client(request, path, timeout=None) - - response = web.StreamResponse() - response.content_type = request.headers.get(CONTENT_TYPE) - try: - await response.prepare(request) - while True: - data = await client.content.read(10) - if not data: - await response.write_eof() - break - response.write(data) - - except aiohttp.ClientError: - await response.write_eof() - - except asyncio.CancelledError: - pass - - finally: - client.close() - - _LOGGER.info("Home-Assistant Event-Stream close") - # Normal request - else: - _LOGGER.info("Home-Assistant '/api/%s' request", path) - client = await self._api_client(request, path) + _LOGGER.info("Home-Assistant /api/%s request", path) + client = await self._api_client(request, path) - data = await client.read() - return web.Response( - body=data, - status=client.status, - content_type=client.content_type - ) + data = await client.read() + return web.Response( + body=data, + status=client.status, + content_type=client.content_type + ) async def _websocket_client(self): """Initialize a websocket api connection."""