Forward Params (#1150)

This commit is contained in:
Pascal Vizeli 2019-07-06 18:06:39 +02:00 committed by GitHub
parent 0a3a752b4c
commit a37c90af96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -20,7 +20,7 @@ _LOGGER = logging.getLogger(__name__)
class APIProxy(CoreSysAttributes):
"""API Proxy for Home Assistant."""
def _check_access(self, request):
def _check_access(self, request: web.Request):
"""Check the Hass.io token."""
if AUTHORIZATION in request.headers:
bearer = request.headers[AUTHORIZATION]
@ -40,7 +40,7 @@ class APIProxy(CoreSysAttributes):
raise HTTPUnauthorized()
@asynccontextmanager
async def _api_client(self, request, path, timeout=300):
async def _api_client(self, request: web.Request, path: str, timeout: int = 300):
"""Return a client request with proxy origin for Home Assistant."""
try:
# read data
@ -58,6 +58,7 @@ class APIProxy(CoreSysAttributes):
content_type=content_type,
data=data,
timeout=timeout,
params=request.query,
) as resp:
yield resp
return
@ -73,7 +74,7 @@ class APIProxy(CoreSysAttributes):
raise HTTPBadGateway()
async def stream(self, request):
async def stream(self, request: web.Request):
"""Proxy HomeAssistant EventStream Requests."""
self._check_access(request)
@ -92,7 +93,7 @@ class APIProxy(CoreSysAttributes):
_LOGGER.info("Home Assistant EventStream close")
return response
async def api(self, request):
async def api(self, request: web.Request):
"""Proxy Home Assistant API Requests."""
self._check_access(request)
@ -162,7 +163,7 @@ class APIProxy(CoreSysAttributes):
raise APIError()
async def websocket(self, request):
async def websocket(self, request: web.Request):
"""Initialize a WebSocket API connection."""
_LOGGER.info("Home Assistant WebSocket API request initialize")

View File

@ -460,7 +460,8 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
json: Optional[Dict[str, Any]] = None,
content_type: Optional[str] = None,
data: Optional[bytes] = None,
timeout=30,
timeout: int = 30,
params: Optional[Dict[str, str]] = None,
) -> AsyncContextManager[aiohttp.ClientResponse]:
"""Async context manager to make a request with right auth."""
url = f"{self.api_url}/{path}"
@ -482,7 +483,12 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
try:
async with getattr(self.sys_websession_ssl, method)(
url, data=data, timeout=timeout, json=json, headers=headers
url,
data=data,
timeout=timeout,
json=json,
headers=headers,
params=params,
) as resp:
# Access token expired
if resp.status == 401 and self.refresh_token: