Skip to content

Commit 86b805a

Browse files
authored
Adds new version method and update checker (#168)
1 parent 6afecfa commit 86b805a

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/aiontfy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Response,
2424
Sound,
2525
Stats,
26+
Version,
2627
ViewAction,
2728
)
2829

@@ -48,6 +49,7 @@
4849
"Response",
4950
"Sound",
5051
"Stats",
52+
"Version",
5153
"ViewAction",
5254
"__version__",
5355
]

src/aiontfy/ntfy.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Notification,
1919
Response,
2020
Stats,
21+
Version,
2122
)
2223

2324

@@ -397,6 +398,19 @@ async def delete_reservation(
397398
)
398399
).success
399400

401+
async def version(self) -> Stats:
402+
"""Get server version (admin-only).
403+
404+
Returns
405+
-------
406+
Stats
407+
An instance of the `Version` class containing ntfy server version.
408+
409+
410+
"""
411+
412+
return Version.from_json(await self._request("GET", self.url / "v1/version"))
413+
400414
async def close(self) -> None:
401415
"""Close session.
402416

src/aiontfy/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ class Stats(DataClassORJSONMixin):
355355
messages_rate: float
356356

357357

358+
@dataclass(kw_only=True, frozen=True)
359+
class Version(DataClassORJSONMixin):
360+
"""Version response."""
361+
362+
version: str
363+
commit: str
364+
date: datetime
365+
366+
358367
@dataclass(kw_only=True, frozen=True)
359368
class Subscription(DataClassORJSONMixin):
360369
"""Subscription information."""

src/aiontfy/update.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
BASE_URL = URL("https://api.github.com/repos/binwiederhier/ntfy")
11+
12+
13+
@dataclass(kw_only=True)
14+
class LatestRelease:
15+
"""Latest release data."""
16+
17+
tag_name: str
18+
name: str
19+
html_url: str
20+
body: str
21+
22+
23+
class UpdateChecker:
24+
"""Check for updates."""
25+
26+
def __init__(
27+
self,
28+
session: ClientSession,
29+
) -> None:
30+
"""Initialize release checker."""
31+
self._session = session
32+
33+
async def latest_release(self) -> LatestRelease:
34+
"""Fetch latest release."""
35+
url = BASE_URL / "releases/latest"
36+
try:
37+
async with self._session.get(url) as response:
38+
response.raise_for_status()
39+
data = await response.json()
40+
return LatestRelease(
41+
tag_name=data["tag_name"],
42+
name=data["name"],
43+
html_url=data["html_url"],
44+
body=data["body"],
45+
)
46+
except ClientError as e:
47+
msg = "Failed to fetch latest release from Github"
48+
raise UpdateCheckerError(msg) from e
49+
except KeyError as e:
50+
msg = "Failed to parse latest release from Github response"
51+
raise UpdateCheckerError(msg) from e
52+
53+
54+
class UpdateCheckerError(Exception):
55+
"""Exception raised for errors fetching latest release from github."""

0 commit comments

Comments
 (0)