From 976fc4f8ea3ebe226613045658ff8a4f0d634dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Thu, 11 Nov 2021 19:13:36 +0100 Subject: [PATCH] Remove backoff dependency (#2274) --- .../hacs/helpers/functions/download.py | 37 ++++++++++++------- custom_components/hacs/manifest.json | 3 +- requirements.txt | 1 - 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/custom_components/hacs/helpers/functions/download.py b/custom_components/hacs/helpers/functions/download.py index 07aa123e780..942146cc6cd 100644 --- a/custom_components/hacs/helpers/functions/download.py +++ b/custom_components/hacs/helpers/functions/download.py @@ -1,11 +1,12 @@ """Helpers to download repository content.""" +from __future__ import annotations +import asyncio import os import pathlib import tempfile import zipfile import async_timeout -import backoff from custom_components.hacs.exceptions import HacsException from custom_components.hacs.helpers.functions.filters import ( @@ -26,30 +27,38 @@ def __init__(self, url, path, name): self.name = name -@backoff.on_exception(backoff.expo, Exception, max_tries=5) -async def async_download_file(url): +async def async_download_file(url: str) -> bytes | None: """Download files, and return the content.""" - hacs = get_hacs() if url is None: - return + return None + + hacs = get_hacs() + tries_left = 5 if "tags/" in url: url = url.replace("tags/", "") _LOGGER.debug("Downloading %s", url) - result = None + while tries_left > 0: + try: + with async_timeout.timeout(60): + request = await hacs.session.get(url) - with async_timeout.timeout(60): - request = await hacs.session.get(url) + # Make sure that we got a valid result + if request.status == 200: + return await request.read() - # Make sure that we got a valid result - if request.status == 200: - result = await request.read() - else: - raise HacsException(f"Got status code {request.status} when trying to download {url}") + raise HacsException( + f"Got status code {request.status} when trying to download {url}" + ) + except Exception as exception: + _LOGGER.debug("Download failed - %s", exception) + tries_left -= 1 + await asyncio.sleep(1) + continue - return result + return None def should_try_releases(repository): diff --git a/custom_components/hacs/manifest.json b/custom_components/hacs/manifest.json index 4dfec6e6a70..bef99ddbf8e 100644 --- a/custom_components/hacs/manifest.json +++ b/custom_components/hacs/manifest.json @@ -17,8 +17,7 @@ "name": "HACS", "requirements": [ "aiogithubapi>=21.8.1", - "awesomeversion>=21.2.2", - "backoff>=1.10.0" + "awesomeversion>=21.2.2" ], "version": "0.0.0" } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c69aa9495f6..b2f13268be1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,6 @@ aresponses==2.1.4 asynctest==0.13.0 attrs==21.2.0 awesomeversion>=21.2.2 -backoff>=1.10.0 bellybutton==0.3.1 colorlog==6.6.0 pre-commit==2.15.0