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

edit_monitor() doesn't run code from _build_monitor_data() #3

Closed spkenney closed 2 years ago

spkenney commented 2 years ago

First, let me say thank you so much for putting this api and accompanying ansible collection together - this is exactly what I was looking for to manage my Uptime Kuma monitors!

The specific issue I am having is that adding a notification to existing monitors wasn't working. Specifically, I was getting this error when running the edit_monitor() function.

...
api.edit_monitor(id_=31, notificationIDList=[1])
...
uptime_kuma_api.exceptions.UptimeKumaException: insert into `monitor_notification` (`monitor_id`, `notification_id`) values (31, '0') - SQLITE_CONSTRAINT: FOREIGN KEY constraint failed

After digging through api.py and a little debugging, I am pretty sure that the issue is that there is some important code included in _build_monitor_data() that doesn't get called when edit_monitor() is called. For my specific issue, I think this code block is the critical piece. As this looks to be responsible for converting the list/array of integer notification ids to the dictionary of {id: True} that the Uptime Kuma socket API is looking for.

def _build_monitor_data(
...
):
...
dict_notification_ids = {}
if notificationIDList:
    for notification_id in notificationIDList:
        dict_notification_ids[notification_id] = True
notificationIDList = dict_notification_ids
...

However, I am not sure if there might be other code in this section that would be important for other monitor attributes that might be edited via edit_monitor() and is currently being missed when that function is called?

For completeness, I'll just document that currently edit_monitor() simply passes its args to data.update(), without running the args through the code above to translate the format, which I believe is the root cause of my issue.

Using the raw api library, I am able to work around this by calling edit_monitor() using notificationIDList={1: True} instead of notificationIDList=[1], but I don't think that is the intent, is it? This work around doesn't seem to work in ansible since notificationIDList is looking for a list, not a dict. Please let me know if I am just missing something here.

Thanks again for putting these packages together, they are really going to help me out.

lucasheld commented 2 years ago

Thank you, that was exactly the problem. The conversion was missing in the edit_monitor method.