1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00
ha-core/homeassistant/components/cloud/utils.py
2021-10-17 19:56:00 +02:00

22 lines
708 B
Python

"""Helper functions for cloud components."""
from __future__ import annotations
from typing import Any
from aiohttp import payload, web
def aiohttp_serialize_response(response: web.Response) -> dict[str, Any]:
"""Serialize an aiohttp response to a dictionary."""
if (body := response.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": body, "headers": dict(response.headers)}