Skip to content

Commit c9149e6

Browse files
authored
Add update checker (#60)
1 parent 4e238db commit c9149e6

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

pythonkuma/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
"""Python API wrapper for Uptime Kuma."""
22

33
from .exceptions import (
4+
UpdateException,
45
UptimeKumaAuthenticationException,
56
UptimeKumaConnectionException,
67
UptimeKumaException,
78
)
89
from .models import MonitorStatus, MonitorType, UptimeKumaMonitor, UptimeKumaVersion
10+
from .update import UpdateChecker
911
from .uptimekuma import UptimeKuma
1012

1113
__version__ = "0.2.0"
1214

1315
__all__ = [
1416
"MonitorStatus",
1517
"MonitorType",
18+
"UpdateChecker",
19+
"UpdateException",
1620
"UptimeKuma",
1721
"UptimeKumaAuthenticationException",
1822
"UptimeKumaConnectionException",

pythonkuma/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ class UptimeKumaConnectionException(UptimeKumaException):
1111

1212
class UptimeKumaAuthenticationException(UptimeKumaException):
1313
"""Uptime Kuma authentication exception."""
14+
15+
16+
class UpdateException(Exception):
17+
"""Exception raised for errors fetching latest release from github."""

pythonkuma/update.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Check for latest Uptime Kuma release."""
2+
3+
from __future__ import annotations
4+
5+
from dataclasses import dataclass
6+
7+
from aiohttp import ClientError, ClientSession
8+
from yarl import URL
9+
10+
from .exceptions import UpdateException
11+
12+
BASE_URL = URL("https://api.github.com/repos/louislam/uptime-kuma")
13+
14+
15+
@dataclass(kw_only=True)
16+
class LatestRelease:
17+
"""Latest release data."""
18+
19+
tag_name: str
20+
name: str
21+
html_url: str
22+
body: str
23+
24+
25+
class UpdateChecker:
26+
"""Check for Uptime Kuma updates."""
27+
28+
def __init__(
29+
self,
30+
session: ClientSession,
31+
) -> None:
32+
"""Initialize Uptime Kuma release checker."""
33+
self._session = session
34+
35+
async def latest_release(self) -> LatestRelease:
36+
"""Fetch latest IronOS release."""
37+
url = BASE_URL / "releases/latest"
38+
try:
39+
async with self._session.get(url) as response:
40+
data = await response.json()
41+
return LatestRelease(
42+
tag_name=data["tag_name"],
43+
name=data["name"],
44+
html_url=data["html_url"],
45+
body=data["body"],
46+
)
47+
except ClientError as e:
48+
msg = "Failed to fetch latest Uptime Kuma release from Github"
49+
raise UpdateException(msg) from e
50+
except KeyError as e:
51+
msg = "Failed to parse latest Uptime Kuma release from Github response"
52+
raise UpdateException(msg) from e

0 commit comments

Comments
 (0)