Add timeouts to requests calls (#76851)

This commit is contained in:
Marc Mueller 2022-08-19 08:58:18 +02:00 committed by GitHub
parent 4eb4146e29
commit 1faabb8f40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 13 additions and 7 deletions

View file

@ -74,7 +74,9 @@ class AbodeCamera(AbodeDevice, Camera):
"""Attempt to download the most recent capture."""
if self._device.image_url:
try:
self._response = requests.get(self._device.image_url, stream=True)
self._response = requests.get(
self._device.image_url, stream=True, timeout=10
)
self._response.raise_for_status()
except requests.HTTPError as err:

View file

@ -68,7 +68,7 @@ def check_box_health(url, username, password):
if username:
kwargs["auth"] = requests.auth.HTTPBasicAuth(username, password)
try:
response = requests.get(url, **kwargs)
response = requests.get(url, **kwargs, timeout=10)
if response.status_code == HTTPStatus.UNAUTHORIZED:
_LOGGER.error("AuthenticationError on %s", CLASSIFIER)
return None
@ -116,7 +116,9 @@ def post_image(url, image, username, password):
if username:
kwargs["auth"] = requests.auth.HTTPBasicAuth(username, password)
try:
response = requests.post(url, json={"base64": encode_image(image)}, **kwargs)
response = requests.post(
url, json={"base64": encode_image(image)}, timeout=10, **kwargs
)
if response.status_code == HTTPStatus.UNAUTHORIZED:
_LOGGER.error("AuthenticationError on %s", CLASSIFIER)
return None
@ -137,6 +139,7 @@ def teach_file(url, name, file_path, username, password):
url,
data={FACEBOX_NAME: name, ATTR_ID: file_path},
files={"file": open_file},
timeout=10,
**kwargs,
)
if response.status_code == HTTPStatus.UNAUTHORIZED:

View file

@ -66,6 +66,6 @@ class AutomateNotificationService(BaseNotificationService):
"payload": message,
}
response = requests.post(_RESOURCE, json=data)
response = requests.post(_RESOURCE, json=data, timeout=10)
if response.status_code != HTTPStatus.OK:
_LOGGER.error("Error sending message: %s", response)

View file

@ -142,7 +142,7 @@ class NestCamera(Camera):
url = self.device.snapshot_url
try:
response = requests.get(url)
response = requests.get(url, timeout=10)
except requests.exceptions.RequestException as error:
_LOGGER.error("Error getting camera image: %s", error)
return None

View file

@ -87,7 +87,7 @@ def _create_processor_from_config(hass, camera_entity, config):
def _get_default_classifier(dest_path):
"""Download the default OpenCV classifier."""
_LOGGER.info("Downloading default classifier")
req = requests.get(CASCADE_URL, stream=True)
req = requests.get(CASCADE_URL, stream=True, timeout=10)
with open(dest_path, "wb") as fil:
for chunk in req.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks

View file

@ -134,7 +134,7 @@ class UkTransportSensor(SensorEntity):
{"app_id": self._api_app_id, "app_key": self._api_app_key}, **params
)
response = requests.get(self._url, params=request_params)
response = requests.get(self._url, params=request_params, timeout=10)
if response.status_code != HTTPStatus.OK:
_LOGGER.warning("Invalid response from API")
elif "error" in response.json():

View file

@ -503,6 +503,7 @@ class ConfigEntryWithingsApi(AbstractWithingsApi):
f"{self.URL}/{path}",
params=params,
headers={"Authorization": f"Bearer {access_token}"},
timeout=10,
)
return response.json()