Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base client to get data from R2 #2990

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions custom_components/hacs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions custom_components/hacs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions custom_components/hacs/data_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""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)
20 changes: 4 additions & 16 deletions scripts/data/generate_category_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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
from custom_components.hacs.exceptions import HacsExecutionStillInProgress
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down