Skip to content

Commit

Permalink
Migrate rest notify to httpx (home-assistant#90769)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored May 11, 2023
1 parent 26f7843 commit 949e8f7
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions homeassistant/components/rest/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import logging
from typing import Any

import requests
from requests.auth import AuthBase, HTTPBasicAuth, HTTPDigestAuth
import httpx
import voluptuous as vol

from homeassistant.components.notify import (
Expand All @@ -32,6 +31,7 @@
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

Expand Down Expand Up @@ -72,7 +72,7 @@
_LOGGER = logging.getLogger(__name__)


def get_service(
async def async_get_service(
hass: HomeAssistant,
config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
Expand All @@ -91,12 +91,12 @@ def get_service(
password: str | None = config.get(CONF_PASSWORD)
verify_ssl: bool = config[CONF_VERIFY_SSL]

auth: AuthBase | None = None
auth: httpx.Auth | None = None
if username and password:
if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
auth = HTTPDigestAuth(username, password)
auth = httpx.DigestAuth(username, password)
else:
auth = HTTPBasicAuth(username, password)
auth = httpx.BasicAuth(username, password)

return RestNotificationService(
hass,
Expand Down Expand Up @@ -129,7 +129,7 @@ def __init__(
target_param_name: str | None,
data: dict[str, Any] | None,
data_template: dict[str, Any] | None,
auth: AuthBase | None,
auth: httpx.Auth | None,
verify_ssl: bool,
) -> None:
"""Initialize the service."""
Expand All @@ -146,7 +146,7 @@ def __init__(
self._auth = auth
self._verify_ssl = verify_ssl

def send_message(self, message: str = "", **kwargs: Any) -> None:
async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
"""Send a message to a user."""
data = {self._message_param_name: message}

Expand Down Expand Up @@ -179,56 +179,62 @@ def _data_template_creator(value: Any) -> Any:
if self._data_template:
data.update(_data_template_creator(self._data_template))

websession = get_async_client(self._hass, self._verify_ssl)
if self._method == "POST":
response = requests.post(
response = await websession.post(
self._resource,
headers=self._headers,
params=self._params,
data=data,
timeout=10,
auth=self._auth,
verify=self._verify_ssl,
auth=self._auth or httpx.USE_CLIENT_DEFAULT,
)
elif self._method == "POST_JSON":
response = requests.post(
response = await websession.post(
self._resource,
headers=self._headers,
params=self._params,
json=data,
timeout=10,
auth=self._auth,
verify=self._verify_ssl,
auth=self._auth or httpx.USE_CLIENT_DEFAULT,
)
else: # default GET
response = requests.get(
response = await websession.get(
self._resource,
headers=self._headers,
params={**self._params, **data} if self._params else data,
timeout=10,
auth=self._auth,
verify=self._verify_ssl,
)

if (
response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
and response.status_code < 600
):
_LOGGER.exception(
"Server error. Response %d: %s:", response.status_code, response.reason
"Server error. Response %d: %s:",
response.status_code,
response.reason_phrase,
)
elif (
response.status_code >= HTTPStatus.BAD_REQUEST
and response.status_code < HTTPStatus.INTERNAL_SERVER_ERROR
):
_LOGGER.exception(
"Client error. Response %d: %s:", response.status_code, response.reason
"Client error. Response %d: %s:",
response.status_code,
response.reason_phrase,
)
elif (
response.status_code >= HTTPStatus.OK
and response.status_code < HTTPStatus.MULTIPLE_CHOICES
):
_LOGGER.debug(
"Success. Response %d: %s:", response.status_code, response.reason
"Success. Response %d: %s:",
response.status_code,
response.reason_phrase,
)
else:
_LOGGER.debug("Response %d: %s:", response.status_code, response.reason)
_LOGGER.debug(
"Response %d: %s:", response.status_code, response.reason_phrase
)

0 comments on commit 949e8f7

Please sign in to comment.