Add GET param support (#314)

This commit is contained in:
Pascal Vizeli 2018-01-16 12:20:04 +01:00 committed by GitHub
parent f443d3052b
commit 26d390b66e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 38 deletions

View File

@ -100,12 +100,14 @@ class RestAPI(CoreSysAttributes):
'/homeassistant/api/websocket', api_proxy.websocket) '/homeassistant/api/websocket', api_proxy.websocket)
self.webapp.router.add_get( self.webapp.router.add_get(
'/homeassistant/websocket', api_proxy.websocket) '/homeassistant/websocket', api_proxy.websocket)
self.webapp.router.add_get(
'/homeassistant/api/stream', api_proxy.stream)
self.webapp.router.add_post( self.webapp.router.add_post(
'/homeassistant/api/{path:.+}', api_proxy.api) '/homeassistant/api/{path:.+}', api_proxy.api)
self.webapp.router.add_get( self.webapp.router.add_get(
'/homeassistant/api/{path:.+}', api_proxy.api) '/homeassistant/api/{path:.+}', api_proxy.api)
self.webapp.router.add_get( self.webapp.router.add_get(
'/homeassistant/api', api_proxy.api) '/homeassistant/api/', api_proxy.api)
def _register_addons(self): def _register_addons(self):
"""Register homeassistant function.""" """Register homeassistant function."""

View File

@ -25,6 +25,7 @@ class APIProxy(CoreSysAttributes):
data = None data = None
headers = {} headers = {}
method = getattr(self._websession_ssl, request.method.lower()) method = getattr(self._websession_ssl, request.method.lower())
params = request.query or None
# read data # read data
with async_timeout.timeout(30, loop=self._loop): with async_timeout.timeout(30, loop=self._loop):
@ -42,7 +43,8 @@ class APIProxy(CoreSysAttributes):
headers = None headers = None
client = await method( client = await method(
url, data=data, headers=headers, timeout=timeout url, data=data, headers=headers, timeout=timeout,
params=params
) )
return client return client
@ -55,14 +57,10 @@ class APIProxy(CoreSysAttributes):
raise HTTPBadGateway() raise HTTPBadGateway()
async def api(self, request): async def stream(self, request):
"""Proxy HomeAssistant API Requests.""" """Proxy HomeAssistant EventStream Requests."""
path = request.match_info.get('path', '') _LOGGER.info("Home-Assistant EventStream start")
client = await self._api_client(request, 'stream', timeout=None)
# 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 = web.StreamResponse()
response.content_type = request.headers.get(CONTENT_TYPE) response.content_type = request.headers.get(CONTENT_TYPE)
@ -83,12 +81,14 @@ class APIProxy(CoreSysAttributes):
finally: finally:
client.close() client.close()
_LOGGER.info("Home-Assistant EventStream close")
_LOGGER.info("Home-Assistant Event-Stream close") async def api(self, request):
"""Proxy HomeAssistant API Requests."""
path = request.match_info.get('path', '')
# Normal request # Normal request
else: _LOGGER.info("Home-Assistant /api/%s request", path)
_LOGGER.info("Home-Assistant '/api/%s' request", path)
client = await self._api_client(request, path) client = await self._api_client(request, path)
data = await client.read() data = await client.read()