diff --git a/tests/test_monitor.py b/tests/test_monitor.py index ef053a3..e84b4c2 100644 --- a/tests/test_monitor.py +++ b/tests/test_monitor.py @@ -147,6 +147,21 @@ class TestMonitor(UptimeKumaTestCase): } self.do_test_monitor_type(expected_monitor) + def test_edit_notification_id_list(self): + # https://github.com/lucasheld/uptime-kuma-api/issues/3 + + monitor_id = self.add_monitor() + notification_id = self.add_notification() + + expected_monitor = self.api.get_monitor(monitor_id) + expected_monitor["notificationIDList"] = {str(notification_id): True} + + r = self.api.edit_monitor(id_=monitor_id, notificationIDList=[notification_id]) + self.assertEqual(r["msg"], "Saved.") + + monitor = self.api.get_monitor(monitor_id) + self.compare(monitor, expected_monitor) + if __name__ == '__main__': unittest.main() diff --git a/tests/uptime_kuma_test_case.py b/tests/uptime_kuma_test_case.py index 967933b..8cbbc8e 100644 --- a/tests/uptime_kuma_test_case.py +++ b/tests/uptime_kuma_test_case.py @@ -11,8 +11,14 @@ def compare(subset, superset): value2 = superset.get(key) if type(value) == list: for i in range(len(value)): - if not value2 or not compare(value[i], value2[i]): + if not value2: return False + elif type(value[i]) == list or type(value[i]) == dict: + if not compare(value[i], value2[i]): + return False + else: + if value[i] != value2[i]: + return False elif type(value) == dict: if not compare(value, value2): return False @@ -72,3 +78,8 @@ class UptimeKumaTestCase(unittest.TestCase): r = self.api.add_tag(name="tag 1", color="#ffffff") tag_id = r["id"] return tag_id + + def add_notification(self): + r = self.api.add_notification(name="notification 1", type="PushByTechulus", pushAPIKey="123456789") + notification_id = r["id"] + return notification_id diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index e4481e4..23e71e9 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -23,6 +23,18 @@ def int_to_bool(data, keys): data[key] = True if data[key] == 1 else False +def _convert_monitor_data(**kwargs): + if not kwargs["accepted_statuscodes"]: + kwargs["accepted_statuscodes"] = ["200-299"] + + dict_notification_ids = {} + if kwargs["notificationIDList"]: + for notification_id in kwargs["notificationIDList"]: + dict_notification_ids[notification_id] = True + kwargs["notificationIDList"] = dict_notification_ids + return kwargs + + def _build_monitor_data( type: MonitorType, name: str, @@ -78,15 +90,6 @@ def _build_monitor_data( "Connection Timeout=", databaseQuery: str = None ): - if not accepted_statuscodes: - accepted_statuscodes = ["200-299"] - - dict_notification_ids = {} - if notificationIDList: - for notification_id in notificationIDList: - dict_notification_ids[notification_id] = True - notificationIDList = dict_notification_ids - data = { "type": type, "name": name, @@ -525,6 +528,7 @@ class UptimeKumaApi(object): def add_monitor(self, **kwargs): data = _build_monitor_data(**kwargs) + data = _convert_monitor_data(**data) _check_arguments_monitor(data) r = self._call('add', data) return r @@ -532,6 +536,7 @@ class UptimeKumaApi(object): def edit_monitor(self, id_: int, **kwargs): data = self.get_monitor(id_) data.update(kwargs) + data = _convert_monitor_data(**data) _check_arguments_monitor(data) r = self._call('editMonitor', data) return r