From c5c35c15dc7758f76ae41974288a602da02e2968 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Thu, 12 Jan 2023 20:10:25 +0000 Subject: [PATCH 1/2] Add base client to get data from R2 --- custom_components/hacs/__init__.py | 2 ++ custom_components/hacs/base.py | 2 ++ custom_components/hacs/data_client.py | 41 ++++++++++++++++++++++++++ scripts/data/generate_category_data.py | 20 +++---------- 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 custom_components/hacs/data_client.py diff --git a/custom_components/hacs/__init__.py b/custom_components/hacs/__init__.py index ea831bb1988..e80a5d28cdf 100644 --- a/custom_components/hacs/__init__.py +++ b/custom_components/hacs/__init__.py @@ -25,6 +25,7 @@ from .base import HacsBase from .const import DOMAIN, MINIMUM_HA_VERSION, STARTUP +from .data_client import HacsDataClient from .enums import ConfigurationType, HacsDisabledReason, HacsStage, LovelaceMode from .frontend import async_register_frontend from .utils.configuration_schema import hacs_config_combined @@ -87,6 +88,7 @@ async def async_initialize_integration( hacs.hass = hass hacs.queue = QueueManager(hass=hass) hacs.data = HacsData(hacs=hacs) + hacs.data_client = HacsDataClient(session=clientsession) hacs.system.running = True hacs.session = clientsession diff --git a/custom_components/hacs/base.py b/custom_components/hacs/base.py index 302802d67ec..af1518245c9 100644 --- a/custom_components/hacs/base.py +++ b/custom_components/hacs/base.py @@ -33,6 +33,7 @@ from homeassistant.util import dt from .const import DOMAIN, TV, URL_BASE +from .data_client import HacsDataClient from .enums import ( ConfigurationType, HacsCategory, @@ -350,6 +351,7 @@ class HacsBase: configuration = HacsConfiguration() core = HacsCore() data: HacsData | None = None + data_client: HacsDataClient | None = None frontend_version: str | None = None github: GitHub | None = None githubapi: GitHubAPI | None = None diff --git a/custom_components/hacs/data_client.py b/custom_components/hacs/data_client.py new file mode 100644 index 00000000000..2898c04315c --- /dev/null +++ b/custom_components/hacs/data_client.py @@ -0,0 +1,41 @@ +"""HACS Data client.""" +from __future__ import annotations + +from typing import Any +from aiohttp import ClientSession, ClientTimeout + +from .exceptions import HacsException + + +class HacsDataClient: + """HACS Data client.""" + + def __init__(self, session: ClientSession) -> None: + """Initialize.""" + self.session = session + + async def _do_request( + self, + filename: str, + section: str | None = None, + ) -> dict[str, dict[str, Any]] | list[str]: + """Do request.""" + endpoint = "/".join([v for v in [section, filename] if v is not None]) + try: + response = await self.session.get( + f"https://data-v2.hacs.xyz/{endpoint}", + timeout=ClientTimeout(total=60), + ) + response.raise_for_status() + except Exception as exception: + raise HacsException(f"Error fetching data from HACS: {exception}") from exception + + return await response.json() + + async def get_data(self, section: str | None) -> dict[str, dict[str, Any]]: + """Get data.""" + return await self._do_request(filename="data.json", section=section) + + async def get_repositories(self, section: str) -> list[str]: + """Get repositories.""" + return await self._do_request(filename="repositories.json", section=section) diff --git a/scripts/data/generate_category_data.py b/scripts/data/generate_category_data.py index 3802272cbf0..8af67fc174d 100644 --- a/scripts/data/generate_category_data.py +++ b/scripts/data/generate_category_data.py @@ -11,6 +11,7 @@ from aiohttp import ClientSession from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.json import JSONEncoder +from config.custom_components.hacs.data_client import HacsDataClient from custom_components.hacs.base import HacsBase from custom_components.hacs.const import HACS_ACTION_GITHUB_API_HEADERS @@ -108,6 +109,7 @@ def __init__(self, session: ClientSession, *, token: str | None = None): self.configuration.token = token self.configuration.experimental = True self.data = AdjustedHacsData(hacs=self) + self.data_client = HacsDataClient(session=session) self.github = GitHub( token, @@ -125,30 +127,16 @@ async def update_repository(self, repository: HacsRepository, force: bool) -> No """Update a repository.""" await repository.common_update(force=force) - async def get_removed_list(self) -> set[str]: - """Get removed list.""" - response = await self.session.get("https://data-v2.hacs.xyz/removed/repositories.json") - response.raise_for_status() - - return set(await response.json()) - - async def get_base_category_data(self, category: str) -> dict[str, dict[str, Any]]: - """Get base data.""" - response = await self.session.get(f"https://data-v2.hacs.xyz/{category}/data.json") - response.raise_for_status() - - return await response.json() - async def generate_data_for_category( self, category: str, force: bool, ) -> dict[str, dict[str, Any]]: """Generate data for category.""" - removed = await self.get_removed_list() + removed = await self.data_client.get_repositories("removed") await self.data.register_base_data( category, - await self.get_base_category_data(category), + await self.data_client.get_data(category), removed, ) self.queue.clear() From bd9c410f53694b26f2022c23c756adc97f12fc5b Mon Sep 17 00:00:00 2001 From: ludeeus Date: Thu, 12 Jan 2023 20:11:53 +0000 Subject: [PATCH 2/2] lint --- custom_components/hacs/data_client.py | 1 + scripts/data/generate_category_data.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/hacs/data_client.py b/custom_components/hacs/data_client.py index 2898c04315c..e40a36f221a 100644 --- a/custom_components/hacs/data_client.py +++ b/custom_components/hacs/data_client.py @@ -2,6 +2,7 @@ from __future__ import annotations from typing import Any + from aiohttp import ClientSession, ClientTimeout from .exceptions import HacsException diff --git a/scripts/data/generate_category_data.py b/scripts/data/generate_category_data.py index 8af67fc174d..aca28bc8262 100644 --- a/scripts/data/generate_category_data.py +++ b/scripts/data/generate_category_data.py @@ -11,8 +11,8 @@ from aiohttp import ClientSession from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.json import JSONEncoder -from config.custom_components.hacs.data_client import HacsDataClient +from config.custom_components.hacs.data_client import HacsDataClient from custom_components.hacs.base import HacsBase from custom_components.hacs.const import HACS_ACTION_GITHUB_API_HEADERS from custom_components.hacs.exceptions import HacsExecutionStillInProgress