diff --git a/tests/test_docker_host.py b/tests/test_docker_host.py index b9dc8b7..4b82757 100644 --- a/tests/test_docker_host.py +++ b/tests/test_docker_host.py @@ -30,6 +30,7 @@ class TestDockerHost(UptimeKumaTestCase): # get docker hosts docker_hosts = self.api.get_docker_hosts() + self.assertTrue(type(docker_hosts[0]["dockerType"]) == DockerType) docker_host = self.find_by_id(docker_hosts, docker_host_id) self.assertIsNotNone(docker_host) self.compare(docker_host, expected_docker_host) diff --git a/tests/test_heartbeat.py b/tests/test_heartbeat.py index 483df15..77e28fa 100644 --- a/tests/test_heartbeat.py +++ b/tests/test_heartbeat.py @@ -1,14 +1,19 @@ import unittest +from uptime_kuma_api import MonitorStatus from uptime_kuma_test_case import UptimeKumaTestCase class TestHeartbeat(UptimeKumaTestCase): def test_get_heartbeats(self): - self.api.get_heartbeats() + self.add_monitor() + r = self.api.get_heartbeats() + self.assertTrue(type(list(r.values())[0][0]["status"]) == MonitorStatus) def test_get_important_heartbeats(self): - self.api.get_important_heartbeats() + self.add_monitor() + r = self.api.get_important_heartbeats() + self.assertTrue(type(list(r.values())[0][0]["status"]) == MonitorStatus) if __name__ == '__main__': diff --git a/tests/test_maintenance.py b/tests/test_maintenance.py index a33d67a..223cd60 100644 --- a/tests/test_maintenance.py +++ b/tests/test_maintenance.py @@ -28,10 +28,12 @@ class TestMaintenance(UptimeKumaTestCase): # get maintenance maintenance = self.api.get_maintenance(maintenance_id) + self.assertTrue(type(maintenance["strategy"]) == MaintenanceStrategy) self.compare(maintenance, expected_maintenance) # get maintenances maintenances = self.api.get_maintenances() + self.assertTrue(type(maintenances[0]["strategy"]) == MaintenanceStrategy) maintenance = self.find_by_id(maintenances, maintenance_id) self.assertIsNotNone(maintenance) self.compare(maintenance, expected_maintenance) diff --git a/tests/test_monitor.py b/tests/test_monitor.py index 1b3e9b7..74abda9 100644 --- a/tests/test_monitor.py +++ b/tests/test_monitor.py @@ -1,6 +1,6 @@ import unittest -from uptime_kuma_api import UptimeKumaException, MonitorType, AuthMethod +from uptime_kuma_api import UptimeKumaException, MonitorType, AuthMethod, MonitorStatus from uptime_kuma_test_case import UptimeKumaTestCase @@ -35,7 +35,11 @@ class TestMonitor(UptimeKumaTestCase): # get monitors monitors = self.api.get_monitors() + self.assertTrue(type(monitors[0]["type"]) == MonitorType) + self.assertTrue(type(monitors[0]["authMethod"]) == AuthMethod) monitor = self.find_by_id(monitors, monitor_id) + self.assertTrue(type(monitor["type"]) == MonitorType) + self.assertTrue(type(monitor["authMethod"]) == AuthMethod) self.assertIsNotNone(monitor) self.compare(monitor, expected_monitor) @@ -58,7 +62,8 @@ class TestMonitor(UptimeKumaTestCase): self.assertEqual(r["msg"], "Resumed Successfully.") # get monitor beats - self.api.get_monitor_beats(monitor_id, 6) + r = self.api.get_monitor_beats(monitor_id, 6) + self.assertTrue(type(r[0]["status"]) == MonitorStatus) # delete monitor r = self.api.delete_monitor(monitor_id) diff --git a/tests/test_notification.py b/tests/test_notification.py index 700e08f..dddf448 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -33,7 +33,9 @@ class TestNotification(UptimeKumaTestCase): # get notifications notifications = self.api.get_notifications() + self.assertTrue(type(notifications[0]["type"]) == NotificationType) notification = self.find_by_id(notifications, notification_id) + self.assertTrue(type(notification["type"]) == NotificationType) self.assertIsNotNone(notification) self.compare(notification, expected_notification) diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 5ddeebb..e77fa38 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -31,6 +31,7 @@ class TestProxy(UptimeKumaTestCase): # get proxies proxies = self.api.get_proxies() + self.assertTrue(type(proxies[0]["protocol"]) == ProxyProtocol) proxy = self.find_by_id(proxies, proxy_id) self.assertIsNotNone(proxy) self.compare(proxy, expected_proxy) diff --git a/tests/test_status_page.py b/tests/test_status_page.py index 41e0e00..d7f6e1b 100644 --- a/tests/test_status_page.py +++ b/tests/test_status_page.py @@ -71,9 +71,11 @@ class TestStatusPage(UptimeKumaTestCase): "style": IncidentStyle.DANGER } incident = self.api.post_incident(slug, **incident_expected) + self.assertTrue(type(incident["style"]) == IncidentStyle) self.compare(incident, incident_expected) status_page = self.api.get_status_page(slug) self.compare(status_page["incident"], incident) + self.assertTrue(type(status_page["incident"]["style"]) == IncidentStyle) # unpin incident self.api.unpin_incident(slug) diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index 07a6acd..75f3d2c 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -49,18 +49,53 @@ def int_to_bool(data, keys) -> None: data[key] = True if data[key] == 1 else False -def parse_value(data, func) -> None: +def parse_value(data, key, type_) -> None: + if not data: + return if isinstance(data, list): for d in data: - parse_value(d, func) + parse_value(d, key, type_) else: - func(data) + if key in data: + data[key] = type_(data[key]) +# monitor def parse_monitor_status(data) -> None: - def parse(x): - x["status"] = MonitorStatus(x["status"]) - parse_value(data, parse) + parse_value(data, "status", MonitorStatus) + + +def parse_monitor_type(data) -> None: + parse_value(data, "type", MonitorType) + + +def parse_auth_method(data) -> None: + parse_value(data, "authMethod", AuthMethod) + + +# notification +def parse_notification_type(data) -> None: + parse_value(data, "type", NotificationType) + + +# docker host +def parse_docker_type(data) -> None: + parse_value(data, "dockerType", DockerType) + + +# status page +def parse_incident_style(data) -> None: + parse_value(data, "style", IncidentStyle) + + +# maintenance +def parse_maintenance_strategy(data) -> None: + parse_value(data, "strategy", MaintenanceStrategy) + + +# proxy +def parse_proxy_protocol(data) -> None: + parse_value(data, "protocol", ProxyProtocol) def gen_secret(length: int) -> str: @@ -918,7 +953,7 @@ class UptimeKumaApi(object): 'accepted_statuscodes': ['200-299'], 'active': True, 'authDomain': None, - 'authMethod': '', + 'authMethod': , 'authWorkstation': None, 'basic_auth_pass': None, 'basic_auth_user': None, @@ -968,7 +1003,7 @@ class UptimeKumaApi(object): 'resendInterval': 0, 'retryInterval': 60, 'tags': [], - 'type': 'http', + 'type': 'upsideDown': False, 'url': 'http://127.0.0.1', 'weight': 2000 @@ -982,6 +1017,8 @@ class UptimeKumaApi(object): for monitor in r: _convert_monitor_return(monitor) int_to_bool(r, ["active"]) + parse_monitor_type(r) + parse_auth_method(r) return r def get_monitor(self, id_: int) -> dict: @@ -1000,7 +1037,7 @@ class UptimeKumaApi(object): 'accepted_statuscodes': ['200-299'], 'active': True, 'authDomain': None, - 'authMethod': '', + 'authMethod': , 'authWorkstation': None, 'basic_auth_pass': None, 'basic_auth_user': None, @@ -1050,7 +1087,7 @@ class UptimeKumaApi(object): 'resendInterval': 0, 'retryInterval': 60, 'tags': [], - 'type': 'http', + 'type': 'upsideDown': False, 'url': 'http://127.0.0.1', 'weight': 2000 @@ -1059,6 +1096,8 @@ class UptimeKumaApi(object): r = self._call('getMonitor', id_)["monitor"] _convert_monitor_return(r) int_to_bool(r, ["active"]) + parse_monitor_type(r) + parse_auth_method(r) return r def pause_monitor(self, id_: int) -> dict: @@ -1346,7 +1385,7 @@ class UptimeKumaApi(object): 'isDefault': True, 'name': 'notification 1', 'pushAPIKey': '123456789', - 'type': 'PushByTechulus', + 'type': 'userId': 1 } ] @@ -1359,6 +1398,7 @@ class UptimeKumaApi(object): del notification["config"] notification.update(config) r.append(notification) + parse_notification_type(r) return r def get_notification(self, id_: int) -> dict: @@ -1380,7 +1420,7 @@ class UptimeKumaApi(object): 'isDefault': True, 'name': 'notification 1', 'pushAPIKey': '123456789', - 'type': 'PushByTechulus', + 'type': 'userId': 1 } """ @@ -1545,7 +1585,7 @@ class UptimeKumaApi(object): 'id': 1, 'password': 'password', 'port': 8080, - 'protocol': 'http', + 'protocol': , 'userId': 1, 'username': 'username' } @@ -1553,6 +1593,7 @@ class UptimeKumaApi(object): """ r = self._get_event_data(Event.PROXY_LIST) int_to_bool(r, ["auth", "active", "default", "applyExisting"]) + parse_proxy_protocol(r) return r def get_proxy(self, id_: int) -> dict: @@ -1727,7 +1768,7 @@ class UptimeKumaApi(object): 'id': 1, 'lastUpdatedDate': None, 'pin': 1, - 'style': 'danger', + 'style': , 'title': 'title 1' }, 'maintenanceList': [], @@ -1769,6 +1810,7 @@ class UptimeKumaApi(object): "publicGroupList": r2["publicGroupList"], "maintenanceList": r2["maintenanceList"] } + parse_incident_style(data["incident"]) return data def add_status_page(self, slug: str, title: str) -> dict: @@ -1921,7 +1963,7 @@ class UptimeKumaApi(object): 'createdDate': '2022-12-15 16:51:43', 'id': 1, 'pin': True, - 'style': 'danger', + 'style': , 'title': 'title 1' } """ @@ -1932,6 +1974,7 @@ class UptimeKumaApi(object): } r = self._call('postIncident', (slug, incident))["incident"] self.save_status_page(slug) + parse_incident_style(r) return r def unpin_incident(self, slug: str) -> dict: @@ -1974,7 +2017,7 @@ class UptimeKumaApi(object): 'monitor_id': 1, 'msg': '', 'ping': 10.5, - 'status': True, + 'status': , 'time': '2023-05-01 17:22:20.289' }, { @@ -1985,7 +2028,7 @@ class UptimeKumaApi(object): 'monitor_id': 1, 'msg': '', 'ping': 10.7, - 'status': True, + 'status': , 'time': '2023-05-01 17:23:20.349' } ] @@ -2015,7 +2058,7 @@ class UptimeKumaApi(object): 'monitorID': 1, 'msg': '', 'ping': 10.5, - 'status': True, + 'status': , 'time': '2023-05-01 17:22:20.289' } ] @@ -2869,7 +2912,7 @@ class UptimeKumaApi(object): [ { 'dockerDaemon': '/var/run/docker.sock', - 'dockerType': 'socket', + 'dockerType': , 'id': 1, 'name': 'name 1', 'userID': 1 @@ -2877,6 +2920,7 @@ class UptimeKumaApi(object): ] """ r = self._get_event_data(Event.DOCKER_HOST_LIST) + parse_docker_type(r) return r def get_docker_host(self, id_: int) -> dict: @@ -3019,7 +3063,7 @@ class UptimeKumaApi(object): "id": 1, "title": "title", "description": "description", - "strategy": "single", + "strategy": , "intervalDay": 1, "active": true, "dateRange": [ @@ -3052,7 +3096,9 @@ class UptimeKumaApi(object): } ] """ - return list(self._get_event_data(Event.MAINTENANCE_LIST).values()) + r = list(self._get_event_data(Event.MAINTENANCE_LIST).values()) + parse_maintenance_strategy(r) + return r def get_maintenance(self, id_: int) -> dict: """ @@ -3070,7 +3116,7 @@ class UptimeKumaApi(object): "id": 1, "title": "title", "description": "description", - "strategy": "single", + "strategy": , "intervalDay": 1, "active": true, "dateRange": [ @@ -3103,7 +3149,9 @@ class UptimeKumaApi(object): "status": "ended" } """ - return self._call('getMaintenance', id_)["maintenance"] + r = self._call('getMaintenance', id_)["maintenance"] + parse_maintenance_strategy(r) + return r @append_docstring(maintenance_docstring("add")) def add_maintenance(self, **kwargs) -> dict: