From 2aa6ac25a71176a351053df7688b1ec5daa4cfee Mon Sep 17 00:00:00 2001 From: lucasheld Date: Wed, 6 Jul 2022 22:35:05 +0200 Subject: [PATCH] move notification provider map to api and improve notification edit --- .../build_params_map_notification_provider.py | 28 ++++ uptimekumaapi/__init__.py | 2 +- uptimekumaapi/api.py | 22 ++- uptimekumaapi/converter.py | 139 +++++++++++++++++- 4 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 datacollection/build_params_map_notification_provider.py diff --git a/datacollection/build_params_map_notification_provider.py b/datacollection/build_params_map_notification_provider.py new file mode 100644 index 0000000..6ed3b23 --- /dev/null +++ b/datacollection/build_params_map_notification_provider.py @@ -0,0 +1,28 @@ +import re +import os + +from uptimekumaapi import notification_provider_options + + +def build_notification_provider_map(): + params_map_notification_providers = {} + + for notification_provider in notification_provider_options: + options = notification_provider_options[notification_provider] + provider_name = notification_provider.__dict__["_value_"].lower().replace(".", "") + for option in options: + option_orig = option + + prefix = os.path.commonprefix([o.lower() for o in options] + [provider_name]) + option = option[len(prefix):] + + option = re.sub('([A-Z]+)', r'_\1', option).lower() + option = provider_name + "_" + option + option = option.replace("__", "_") + + params_map_notification_providers[option_orig] = option + return params_map_notification_providers + + +notification_provider_map = build_notification_provider_map() +print("params_map_notification_provider =", notification_provider_map) diff --git a/uptimekumaapi/__init__.py b/uptimekumaapi/__init__.py index d7c630f..7f501f1 100644 --- a/uptimekumaapi/__init__.py +++ b/uptimekumaapi/__init__.py @@ -4,5 +4,5 @@ from .notification_providers import NotificationType, notification_provider_opti from .proxy_protocol import ProxyProtocol from .incident_style import IncidentStyle from .converter import convert_from_socket, convert_to_socket, params_map_monitor, params_map_notification, \ - params_map_proxy, params_map_status_page + params_map_notification_provider, params_map_proxy, params_map_status_page from .api import UptimeKumaApi diff --git a/uptimekumaapi/api.py b/uptimekumaapi/api.py index 05440d6..250bb18 100644 --- a/uptimekumaapi/api.py +++ b/uptimekumaapi/api.py @@ -8,7 +8,7 @@ from . import MonitorType from . import NotificationType, notification_provider_options from . import ProxyProtocol from . import IncidentStyle -from . import convert_from_socket, convert_to_socket, params_map_monitor, params_map_notification, params_map_proxy, params_map_status_page +from . import convert_from_socket, convert_to_socket, params_map_monitor, params_map_notification, params_map_notification_provider, params_map_proxy, params_map_status_page def int_to_bool(data: list[dict] | dict, keys): @@ -159,12 +159,7 @@ def _build_monitor_data( def _build_notification_data(name: str, type_: NotificationType, default: bool, **kwargs): - allowed_options = notification_provider_options[type_] - s1 = set(allowed_options) - s2 = set(kwargs.keys()) - if len(s1 - s2) > 0 or len(s2 - s1) > 0: - raise ValueError(f"Allowed options: {allowed_options}") - + kwargs = convert_to_socket(params_map_notification_provider, kwargs) return { "name": name, "type": type_, @@ -424,6 +419,19 @@ class UptimeKumaApi(object): def edit_notification(self, id_: int, **kwargs): notification = self.get_notification(id_) kwargs_sock = convert_to_socket(params_map_notification, kwargs) + kwargs_sock = convert_to_socket(params_map_notification_provider, kwargs_sock) + + # remove old notification provider options from notification object + old_type = notification["type"] + new_type = kwargs_sock["type"] + if "type" in kwargs_sock and new_type != old_type: + for provider in notification_provider_options: + provider_options = notification_provider_options[provider] + if provider != new_type: + for option in provider_options: + if option in notification: + del notification[option] + notification.update(kwargs_sock) return self.sio.call('addNotification', (notification, id_)) diff --git a/uptimekumaapi/converter.py b/uptimekumaapi/converter.py index 82d7ff5..8aee2dc 100644 --- a/uptimekumaapi/converter.py +++ b/uptimekumaapi/converter.py @@ -32,6 +32,132 @@ params_map_notification = { "isDefault": "default" } +params_map_notification_provider = { + 'alertaApiEndpoint': 'alerta_api_endpoint', + 'alertaApiKey': 'alerta_api_key', + 'alertaEnvironment': 'alerta_environment', + 'alertaAlertState': 'alerta_alert_state', + 'alertaRecoverState': 'alerta_recover_state', + 'phonenumber': 'aliyunsms_phonenumber', + 'templateCode': 'aliyunsms_template_code', + 'signName': 'aliyunsms_sign_name', + 'accessKeyId': 'aliyunsms_access_key_id', + 'secretAccessKey': 'aliyunsms_secret_access_key', + 'appriseURL': 'apprise_apprise_url', + 'title': 'apprise_title', + 'barkEndpoint': 'bark_endpoint', + 'clicksendsmsLogin': 'clicksendsms_login', + 'clicksendsmsPassword': 'clicksendsms_password', + 'clicksendsmsToNumber': 'clicksendsms_to_number', + 'clicksendsmsSenderName': 'clicksendsms_sender_name', + 'webHookUrl': 'dingding_web_hook_url', + 'secretKey': 'dingding_secret_key', + 'discordUsername': 'discord_username', + 'discordWebhookUrl': 'discord_webhook_url', + 'discordPrefixMessage': 'discord_prefix_message', + 'feishuWebHookUrl': 'feishu_web_hook_url', + 'googleChatWebhookURL': 'googlechat_webhook_url', + 'gorushDeviceToken': 'gorush_device_token', + 'gorushPlatform': 'gorush_platform', + 'gorushTitle': 'gorush_title', + 'gorushPriority': 'gorush_priority', + 'gorushRetry': 'gorush_retry', + 'gorushTopic': 'gorush_topic', + 'gorushServerURL': 'gorush_server_url', + 'gotifyserverurl': 'gotify_serverurl', + 'gotifyapplicationToken': 'gotify_application_token', + 'gotifyPriority': 'gotify_priority', + 'lineChannelAccessToken': 'line_channel_access_token', + 'lineUserID': 'line_user_id', + 'lunaseaDevice': 'lunasea_device', + 'internalRoomId': 'matrix_internal_room_id', + 'accessToken': 'onebot_access_token', + 'homeserverUrl': 'matrix_homeserver_url', + 'mattermostusername': 'mattermost_username', + 'mattermostWebhookUrl': 'mattermost_webhook_url', + 'mattermostchannel': 'mattermost_channel', + 'mattermosticonemo': 'mattermost_iconemo', + 'mattermosticonurl': 'mattermost_iconurl', + 'ntfyserverurl': 'ntfy_serverurl', + 'ntfytopic': 'ntfy_topic', + 'ntfyPriority': 'ntfy_priority', + 'octopushVersion': 'octopush_version', + 'octopushAPIKey': 'octopush_apikey', + 'octopushLogin': 'octopush_login', + 'octopushPhoneNumber': 'octopush_phone_number', + 'octopushSMSType': 'octopush_smstype', + 'octopushSenderName': 'octopush_sender_name', + 'octopushDMLogin': 'octopush_dmlogin', + 'octopushDMAPIKey': 'octopush_dmapikey', + 'octopushDMPhoneNumber': 'octopush_dmphone_number', + 'octopushDMSenderName': 'octopush_dmsender_name', + 'octopushDMSMSType': 'octopush_dmsmstype', + 'httpAddr': 'onebot_http_addr', + 'msgType': 'onebot_msg_type', + 'recieverId': 'onebot_reciever_id', + 'pagerdutyAutoResolve': 'pagerduty_auto_resolve', + 'pagerdutyIntegrationUrl': 'pagerduty_integration_url', + 'pagerdutyPriority': 'pagerduty_priority', + 'pagerdutyIntegrationKey': 'pagerduty_integration_key', + 'promosmsLogin': 'promosms_login', + 'promosmsPassword': 'promosms_password', + 'promosmsPhoneNumber': 'promosms_phone_number', + 'promosmsSMSType': 'promosms_smstype', + 'promosmsSenderName': 'promosms_sender_name', + 'pushbulletAccessToken': 'pushbullet_access_token', + 'pushdeerKey': 'pushdeer_key', + 'pushoveruserkey': 'pushover_userkey', + 'pushoverapptoken': 'pushover_apptoken', + 'pushoversounds': 'pushover_sounds', + 'pushoverpriority': 'pushover_priority', + 'pushovertitle': 'pushover_title', + 'pushoverdevice': 'pushover_device', + 'pushyAPIKey': 'pushy_apikey', + 'pushyToken': 'pushy_token', + 'rocketchannel': 'rocketchat_channel', + 'rocketusername': 'rocketchat_username', + 'rocketiconemo': 'rocketchat_iconemo', + 'rocketwebhookURL': 'rocketchat_webhook_url', + 'rocketbutton': 'rocketchat_button', + 'serwersmsUsername': 'serwersms_username', + 'serwersmsPassword': 'serwersms_password', + 'serwersmsPhoneNumber': 'serwersms_phone_number', + 'serwersmsSenderName': 'serwersms_sender_name', + 'signalNumber': 'signal_number', + 'signalRecipients': 'signal_recipients', + 'signalURL': 'signal_url', + 'slackbutton': 'slack_button', + 'slackchannel': 'slack_channel', + 'slackusername': 'slack_username', + 'slackiconemo': 'slack_iconemo', + 'slackwebhookURL': 'slack_webhook_url', + 'smtpHost': 'smtp_smtp_host', + 'smtpPort': 'smtp_smtp_port', + 'smtpSecure': 'smtp_smtp_secure', + 'smtpIgnoreTLSError': 'smtp_smtp_ignore_tlserror', + 'smtpDkimDomain': 'smtp_smtp_dkim_domain', + 'smtpDkimKeySelector': 'smtp_smtp_dkim_key_selector', + 'smtpDkimPrivateKey': 'smtp_smtp_dkim_private_key', + 'smtpDkimHashAlgo': 'smtp_smtp_dkim_hash_algo', + 'smtpDkimheaderFieldNames': 'smtp_smtp_dkimheader_field_names', + 'smtpDkimskipFields': 'smtp_smtp_dkimskip_fields', + 'smtpUsername': 'smtp_smtp_username', + 'smtpPassword': 'smtp_smtp_password', + 'customSubject': 'smtp_custom_subject', + 'smtpFrom': 'smtp_smtp_from', + 'smtpCC': 'smtp_smtp_cc', + 'smtpBCC': 'smtp_smtp_bcc', + 'smtpTo': 'smtp_smtp_to', + 'stackfieldwebhookURL': 'stackfield_webhook_url', + 'webhookUrl': 'teams_webhook_url', + 'pushAPIKey': 'pushbytechulus_apikey', + 'telegramBotToken': 'telegram_bot_token', + 'telegramChatID': 'telegram_chat_id', + 'webhookContentType': 'webhook_content_type', + 'webhookURL': 'webhook_url', + 'weComBotKey': 'wecom_bot_key' +} + params_map_proxy = { "applyExisting": "apply_existing" } @@ -52,7 +178,7 @@ params_map_status_page = { } -def _convert_to_from_socket(params_map: dict[str, str], params: list[dict] | dict, to_socket=False): +def _convert_to_from_socket(params_map: dict[str, str], params: list[dict] | dict | str, to_socket=False): if type(params) == list: out = [] params_list = params @@ -62,10 +188,13 @@ def _convert_to_from_socket(params_map: dict[str, str], params: list[dict] | dic else: if to_socket: params_map = {v: k for k, v in params_map.items()} - out = {} - for key, value in params.items(): - key = params_map.get(key, key) - out[key] = value + if type(params) == dict: + out = {} + for key, value in params.items(): + key = params_map.get(key, key) + out[key] = value + else: + return params_map.get(params, params) return out