diff --git a/pythonkuma/__init__.py b/pythonkuma/__init__.py index 4506eaa..32543ec 100644 --- a/pythonkuma/__init__.py +++ b/pythonkuma/__init__.py @@ -1,11 +1,13 @@ """Python API wrapper for Uptime Kuma.""" from .exceptions import ( + UpdateException, UptimeKumaAuthenticationException, UptimeKumaConnectionException, UptimeKumaException, ) from .models import MonitorStatus, MonitorType, UptimeKumaMonitor, UptimeKumaVersion +from .update import UpdateChecker from .uptimekuma import UptimeKuma __version__ = "0.2.0" @@ -13,6 +15,8 @@ __all__ = [ "MonitorStatus", "MonitorType", + "UpdateChecker", + "UpdateException", "UptimeKuma", "UptimeKumaAuthenticationException", "UptimeKumaConnectionException", diff --git a/pythonkuma/exceptions.py b/pythonkuma/exceptions.py index 86d2869..47e7d41 100644 --- a/pythonkuma/exceptions.py +++ b/pythonkuma/exceptions.py @@ -11,3 +11,7 @@ class UptimeKumaConnectionException(UptimeKumaException): class UptimeKumaAuthenticationException(UptimeKumaException): """Uptime Kuma authentication exception.""" + + +class UpdateException(Exception): + """Exception raised for errors fetching latest release from github.""" diff --git a/pythonkuma/update.py b/pythonkuma/update.py new file mode 100644 index 0000000..8aa2a35 --- /dev/null +++ b/pythonkuma/update.py @@ -0,0 +1,52 @@ +"""Check for latest Uptime Kuma release.""" + +from __future__ import annotations + +from dataclasses import dataclass + +from aiohttp import ClientError, ClientSession +from yarl import URL + +from .exceptions import UpdateException + +BASE_URL = URL("https://api.github.com/repos/louislam/uptime-kuma") + + +@dataclass(kw_only=True) +class LatestRelease: + """Latest release data.""" + + tag_name: str + name: str + html_url: str + body: str + + +class UpdateChecker: + """Check for Uptime Kuma updates.""" + + def __init__( + self, + session: ClientSession, + ) -> None: + """Initialize Uptime Kuma release checker.""" + self._session = session + + async def latest_release(self) -> LatestRelease: + """Fetch latest IronOS release.""" + url = BASE_URL / "releases/latest" + try: + async with self._session.get(url) as response: + data = await response.json() + return LatestRelease( + tag_name=data["tag_name"], + name=data["name"], + html_url=data["html_url"], + body=data["body"], + ) + except ClientError as e: + msg = "Failed to fetch latest Uptime Kuma release from Github" + raise UpdateException(msg) from e + except KeyError as e: + msg = "Failed to parse latest Uptime Kuma release from Github response" + raise UpdateException(msg) from e