From 804f6eb05b1dcfde26aee274788c0d1febd1b722 Mon Sep 17 00:00:00 2001 From: "dries.k" Date: Sun, 9 Jul 2023 18:34:02 +0200 Subject: [PATCH] make gotify api return values uniform --- gotify_tray/gotify/api.py | 24 ++++++++++++++++-------- gotify_tray/tasks.py | 33 ++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/gotify_tray/gotify/api.py b/gotify_tray/gotify/api.py index e21225a..64371ef 100644 --- a/gotify_tray/gotify/api.py +++ b/gotify_tray/gotify/api.py @@ -110,8 +110,9 @@ class GotifyClient(GotifySession): else GotifyErrorModel(response) ) - def delete_application(self, application_id: int) -> bool: - return self._delete(f"/application/{application_id}").ok + def delete_application(self, application_id: int) -> None | GotifyErrorModel: + response = self._delete(f"/application/{application_id}") + return None if response.ok else GotifyErrorModel(response) def upload_application_image( self, application_id: int, img_path: str @@ -132,6 +133,10 @@ class GotifyClient(GotifySession): ) return None + def delete_application_image(self, application_id: int) -> None | GotifyErrorModel: + response = self._delete(f"/application/{application_id}/image") + return None if response.ok else GotifyErrorModel(response) + """ Message """ @@ -151,8 +156,9 @@ class GotifyClient(GotifySession): paging=GotifyPagingModel(j["paging"]), ) - def delete_application_messages(self, application_id: int) -> bool: - return self._delete(f"/application/{application_id}/message").ok + def delete_application_messages(self, application_id: int) -> None | GotifyErrorModel: + response = self._delete(f"/application/{application_id}/message") + return None if response.ok else GotifyErrorModel(response) def get_messages( self, limit: int = 100, since: int | None = None @@ -166,11 +172,13 @@ class GotifyClient(GotifySession): paging=GotifyPagingModel(j["paging"]), ) - def delete_messages(self) -> bool: - return self._delete("/message").ok + def delete_messages(self) -> None | GotifyErrorModel: + response = self._delete("/message") + return None if response.ok else GotifyErrorModel(response) - def delete_message(self, message_id: int) -> bool: - return self._delete(f"/message/{message_id}").ok + def delete_message(self, message_id: int) -> None | GotifyErrorModel: + response = self._delete(f"/message/{message_id}") + return None if response.ok else GotifyErrorModel(response) def listen( self, diff --git a/gotify_tray/tasks.py b/gotify_tray/tasks.py index 04bb4cd..4ccc4e2 100644 --- a/gotify_tray/tasks.py +++ b/gotify_tray/tasks.py @@ -9,7 +9,6 @@ from PyQt6 import QtCore from PyQt6.QtCore import pyqtSignal from gotify_tray.database import Cache, Settings -from gotify_tray.gotify.api import GotifyClient from gotify_tray.gotify.models import GotifyVersionModel from gotify_tray.utils import process_messages @@ -50,7 +49,8 @@ class BaseTask(QtCore.QThread): class DeleteMessageTask(BaseTask): - deleted = pyqtSignal(bool) + success = pyqtSignal() + error = pyqtSignal(gotify.GotifyErrorModel) def __init__(self, message_id: int, gotify_client: gotify.GotifyClient): super(DeleteMessageTask, self).__init__() @@ -58,12 +58,16 @@ class DeleteMessageTask(BaseTask): self.gotify_client = gotify_client def task(self): - success = self.gotify_client.delete_message(self.message_id) - self.deleted.emit(success) + result = self.gotify_client.delete_message(self.message_id) + if isinstance(result, gotify.GotifyErrorModel): + self.error.emit(result) + else: + self.success.emit() class DeleteApplicationMessagesTask(BaseTask): - deleted = pyqtSignal(bool) + success = pyqtSignal() + error = pyqtSignal(gotify.GotifyErrorModel) def __init__(self, appid: int, gotify_client: gotify.GotifyClient): super(DeleteApplicationMessagesTask, self).__init__() @@ -71,20 +75,27 @@ class DeleteApplicationMessagesTask(BaseTask): self.gotify_client = gotify_client def task(self): - success = self.gotify_client.delete_application_messages(self.appid) - self.deleted.emit(success) + result = self.gotify_client.delete_application_messages(self.appid) + if isinstance(result, gotify.GotifyErrorModel): + self.error.emit(result) + else: + self.success.emit() class DeleteAllMessagesTask(BaseTask): - deleted = pyqtSignal(bool) + success = pyqtSignal() + error = pyqtSignal(gotify.GotifyErrorModel) def __init__(self, gotify_client: gotify.GotifyClient): super(DeleteAllMessagesTask, self).__init__() self.gotify_client = gotify_client def task(self): - success = self.gotify_client.delete_messages() - self.deleted.emit(success) + result = self.gotify_client.delete_messages() + if isinstance(result, gotify.GotifyErrorModel): + self.error.emit(result) + else: + self.success.emit() class GetApplicationsTask(BaseTask): @@ -197,7 +208,7 @@ class VerifyServerInfoTask(BaseTask): class ServerConnectionWatchdogTask(BaseTask): closed = pyqtSignal() - def __init__(self, gotify_client: GotifyClient): + def __init__(self, gotify_client: gotify.GotifyClient): super(ServerConnectionWatchdogTask, self).__init__() self.gotify_client = gotify_client