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

Implement generic async_github_api_method #2329

Merged
merged 2 commits into from
Dec 5, 2021
Merged
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
38 changes: 27 additions & 11 deletions custom_components/hacs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import pathlib
import shutil
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Awaitable

from aiogithubapi import (
GitHub,
Expand All @@ -19,7 +19,7 @@
)
from aiogithubapi.objects.repository import AIOGitHubAPIRepository
from aiohttp.client import ClientSession
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, T
from homeassistant.loader import Integration

from .const import REPOSITORY_HACS_DEFAULT
Expand Down Expand Up @@ -250,27 +250,43 @@ def _write_file():
async def async_can_update(self) -> int:
"""Helper to calculate the number of repositories we can fetch data for."""
try:
response = await self.githubapi.rate_limit()
response = await self.async_github_api_method(self.githubapi.rate_limit)
if ((limit := response.data.resources.core.remaining or 0) - 1000) >= 15:
return math.floor((limit - 1000) / 15)
self.log.error(
"GitHub API ratelimited - %s remaining", response.data.resources.core.remaining
)
self.disable_hacs(HacsDisabledReason.RATE_LIMIT)
except GitHubAuthenticationException as exception:
self.log.error("GitHub authentication failed - %s", exception)
self.disable_hacs(HacsDisabledReason.INVALID_TOKEN)
except GitHubRatelimitException as exception:
self.log.error("GitHub API ratelimited - %s", exception)
self.disable_hacs(HacsDisabledReason.RATE_LIMIT)
except BaseException as exception: # pylint: disable=broad-except
self.log.exception(exception)

return 0

async def async_github_get_hacs_default_file(self, filename: str) -> dict[str, Any]:
"""Get the content of a default file."""
response = await self.githubapi.repos.contents.get(
repository=REPOSITORY_HACS_DEFAULT, path=filename
response = await self.async_github_api_method(
method=self.githubapi.repos.contents.get,
repository=REPOSITORY_HACS_DEFAULT,
path=filename,
)
return json.loads(decode_content(response.data.content))

async def async_github_api_method(
self,
method: Awaitable[T],
*args,
**kwargs,
) -> T | None:
"""Call a GitHub API method"""
try:
return await method(*args, **kwargs)
except GitHubAuthenticationException as exception:
self.log.error("GitHub authentication failed - %s", exception)
self.disable_hacs(HacsDisabledReason.INVALID_TOKEN)
except GitHubRatelimitException as exception:
self.log.error("GitHub API ratelimited - %s", exception)
self.disable_hacs(HacsDisabledReason.RATE_LIMIT)
except BaseException as exception: # pylint: disable=broad-except
self.log.exception(exception)
raise exception
return None