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)
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."""

View File

@ -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."""