1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-23 12:29:55 +00:00

Fix aiohttp response serialize (#23858)

* Fix aiohttp response serialize

* Suport bytes

* Handle None
This commit is contained in:
Paulus Schoutsen
2019-05-14 11:59:27 +02:00
committed by GitHub
parent 6fa8556033
commit 94a2fd542e
2 changed files with 48 additions and 2 deletions

View File

@@ -1,13 +1,25 @@
"""Helper functions for cloud components."""
from typing import Any, Dict
from aiohttp import web
from aiohttp import web, payload
def aiohttp_serialize_response(response: web.Response) -> Dict[str, Any]:
"""Serialize an aiohttp response to a dictionary."""
body = response.body
if body is None:
pass
elif isinstance(body, payload.StringPayload):
# pylint: disable=protected-access
body = body._value.decode(body.encoding)
elif isinstance(body, bytes):
body = body.decode(response.charset or 'utf-8')
else:
raise ValueError("Unknown payload encoding")
return {
'status': response.status,
'body': response.text,
'body': body,
'headers': dict(response.headers),
}