mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 15:27:08 +00:00
Use json_loads by default for the aiohttp helper (#75214)
This commit is contained in:
parent
61cc9f5288
commit
03e3ebb238
@ -7,7 +7,7 @@ from contextlib import suppress
|
|||||||
from ssl import SSLContext
|
from ssl import SSLContext
|
||||||
import sys
|
import sys
|
||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
from typing import Any, cast
|
from typing import TYPE_CHECKING, Any, cast
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
@ -22,7 +22,11 @@ from homeassistant.loader import bind_hass
|
|||||||
from homeassistant.util import ssl as ssl_util
|
from homeassistant.util import ssl as ssl_util
|
||||||
|
|
||||||
from .frame import warn_use
|
from .frame import warn_use
|
||||||
from .json import json_dumps
|
from .json import json_dumps, json_loads
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from aiohttp.typedefs import JSONDecoder
|
||||||
|
|
||||||
|
|
||||||
DATA_CONNECTOR = "aiohttp_connector"
|
DATA_CONNECTOR = "aiohttp_connector"
|
||||||
DATA_CONNECTOR_NOTVERIFY = "aiohttp_connector_notverify"
|
DATA_CONNECTOR_NOTVERIFY = "aiohttp_connector_notverify"
|
||||||
@ -35,6 +39,19 @@ SERVER_SOFTWARE = "HomeAssistant/{0} aiohttp/{1} Python/{2[0]}.{2[1]}".format(
|
|||||||
WARN_CLOSE_MSG = "closes the Home Assistant aiohttp session"
|
WARN_CLOSE_MSG = "closes the Home Assistant aiohttp session"
|
||||||
|
|
||||||
|
|
||||||
|
class HassClientResponse(aiohttp.ClientResponse):
|
||||||
|
"""aiohttp.ClientResponse with a json method that uses json_loads by default."""
|
||||||
|
|
||||||
|
async def json(
|
||||||
|
self,
|
||||||
|
*args: Any,
|
||||||
|
loads: JSONDecoder = json_loads,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> Any:
|
||||||
|
"""Send a json request and parse the json response."""
|
||||||
|
return await super().json(*args, loads=loads, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def async_get_clientsession(
|
def async_get_clientsession(
|
||||||
@ -99,6 +116,7 @@ def _async_create_clientsession(
|
|||||||
clientsession = aiohttp.ClientSession(
|
clientsession = aiohttp.ClientSession(
|
||||||
connector=_async_get_connector(hass, verify_ssl),
|
connector=_async_get_connector(hass, verify_ssl),
|
||||||
json_serialize=json_dumps,
|
json_serialize=json_dumps,
|
||||||
|
response_class=HassClientResponse,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
# Prevent packages accidentally overriding our default headers
|
# Prevent packages accidentally overriding our default headers
|
||||||
|
Loading…
x
Reference in New Issue
Block a user