lucasheld / uptime-kuma-api

A Python wrapper for the Uptime Kuma Socket.IO API
https://uptime-kuma-api.readthedocs.io
MIT License
273 stars 23 forks source link

Feature Request: Add functionality to delete all monitors simultaneously #70

Open vsamarcus opened 7 months ago

vsamarcus commented 7 months ago

Feature Description:

I'm proposing to add functionality that allows users of the uptime-kuma-api API to delete all monitors at once. Currently, to perform this task, users need to delete each monitor individually, which can be quite time-consuming and error-prone in scenarios where there are a large number of monitors configured.

Motivation:

In test or development environments, it is common to need to restart the monitor configuration, removing all existing ones to start a new set of tests. The absence of a function that allows the mass deletion of monitors makes this process inefficient.

Proposed solution:

I added a function called delete_all_monitors in the api.py module, which iterates over all monitors returned by get_monitors() and deletes them individually. Additionally, I implemented a test for this new functionality in the tests/test_monitor.py module, ensuring that all monitors are deleted correctly.

Implementation Details:

Modified File: uptime_kuma_api/api.py New Function: delete_all_monitors()

uptime_kuma_api/api.py

    def delete_all_monitors(self, **kwargs) -> dict:
        """
        Deletes all monitors.

        :return: The server response.
        :rtype: dict
        :raises UptimeKumaException: If the server returns an error.

        Example::

            >>> api.delete_all_monitors()
            {
                'msg': 'Deleted Successfully.'
            }
        """
        data = self._build_monitor_data(**kwargs)
        _convert_monitor_input(data)
        _check_arguments_monitor(data)
        for monitor in self.get_monitors():
            self.delete_monitor(monitor["id"])
        return self._call('deleteAllMonitors')

tests/test_monitor.py

    def test_delete_all_monitors(self):
        expected_monitor_1 = {
            "type": MonitorType.HTTP,
            "name": "monitor 1",
            "interval": 60,
            "retryInterval": 60,
            "maxretries": 0,
            "upsideDown": False,
            "url": "http://127.0.0.1",
            "resendInterval": 0
        }

        expected_monitor_2 = {
            "type": MonitorType.HTTP,
            "name": "monitor 2",
            "interval": 60,
            "retryInterval": 60,
            "maxretries": 0,
            "upsideDown": False,
            "url": "https://example.com",
            "resendInterval": 0
        }

        # add monitor 1
        r1 = self.api.add_monitor(**expected_monitor_1)
        self.assertEqual(r1["msg"], "Added Successfully.")

        # add monitor 2
        r2 = self.api.add_monitor(**expected_monitor_2)
        self.assertEqual(r2["msg"], "Added Successfully.")

        self.api.delete_all_monitors()
        self.assertEqual(0, len(self.api.get_monitors()))

Final considerations:

I believe this functionality will bring a huge improvement in terms of usability and efficiency to uptime-kuma-api. I am open to discussing this implementation and making adjustments as recommended by the community and project maintainers.