Support upload streaming (#1362)

* Support upload streaming

* Fix header

* Fix typing
This commit is contained in:
Pascal Vizeli 2019-11-06 23:57:51 +01:00 committed by GitHub
parent ee76317392
commit a92cab48e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 14 deletions

View File

@ -8,7 +8,6 @@ from aiohttp import web
from aiohttp.web_exceptions import HTTPBadGateway, HTTPUnauthorized
from aiohttp.client_exceptions import ClientConnectorError
from aiohttp.hdrs import CONTENT_TYPE, AUTHORIZATION
import async_timeout
from ..const import HEADER_HA_ACCESS
from ..coresys import CoreSysAttributes
@ -17,6 +16,9 @@ from ..exceptions import HomeAssistantAuthError, HomeAssistantAPIError, APIError
_LOGGER: logging.Logger = logging.getLogger(__name__)
LEGACY_HEADER = ("X-Speech-Content",)
class APIProxy(CoreSysAttributes):
"""API Proxy for Home Assistant."""
@ -43,20 +45,16 @@ class APIProxy(CoreSysAttributes):
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
with async_timeout.timeout(30):
data = await request.read()
if data:
content_type = request.content_type
else:
content_type = None
async with self.sys_homeassistant.make_request(
request.method.lower(),
f"api/{path}",
content_type=content_type,
data=data,
headers={
name: value
for name, value in request.headers.items()
if name in LEGACY_HEADER
},
content_type=request.content_type,
data=request.content,
timeout=timeout,
params=request.query,
) as resp:

View File

@ -487,13 +487,14 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
path: str,
json: Optional[Dict[str, Any]] = None,
content_type: Optional[str] = None,
data: Optional[bytes] = None,
data: Any = None,
timeout: int = 30,
params: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None,
) -> AsyncContextManager[aiohttp.ClientResponse]:
"""Async context manager to make a request with right auth."""
url = f"{self.api_url}/{path}"
headers = {}
headers = headers or {}
# Passthrough content type
if content_type is not None: