Add GET param support (#314)
This commit is contained in:
parent
f443d3052b
commit
26d390b66e
|
@ -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."""
|
||||||
|
|
|
@ -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,48 +57,46 @@ class APIProxy(CoreSysAttributes):
|
||||||
|
|
||||||
raise HTTPBadGateway()
|
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):
|
async def api(self, request):
|
||||||
"""Proxy HomeAssistant API Requests."""
|
"""Proxy HomeAssistant API Requests."""
|
||||||
path = request.match_info.get('path', '')
|
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
|
# 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()
|
||||||
return web.Response(
|
return web.Response(
|
||||||
body=data,
|
body=data,
|
||||||
status=client.status,
|
status=client.status,
|
||||||
content_type=client.content_type
|
content_type=client.content_type
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _websocket_client(self):
|
async def _websocket_client(self):
|
||||||
"""Initialize a websocket api connection."""
|
"""Initialize a websocket api connection."""
|
||||||
|
|
Loading…
Reference in New Issue