def _get_valid_config(cls, config, *fields) -> ConfigType: config = to_list(config) web_hooks = [] for web_hook in config: if not web_hook.get("url"): logger.warning( "Settings contains a non compatible web hook: `%s`", web_hook ) continue url = web_hook["url"] if not validate_url(url): raise PolyaxonNotificationException( "{} received invalid URL `{}`.".format(cls.name, url) ) method = web_hook.get("method", "POST") if not isinstance(method, str): raise PolyaxonNotificationException( "{} received invalid method `{}`.".format(cls.name, method) ) _method = method.upper() if _method not in ["GET", "POST"]: raise PolyaxonNotificationException( "{} received non compatible method `{}`.".format(cls.name, method) ) result_web_hook = {"url": url, "method": _method} for field in fields: if field in web_hook: result_web_hook[field] = web_hook[field] web_hooks.append(result_web_hook) return web_hooks
def execute( cls, notification: NotificationSpec, config: Optional[ConfigType] = None, ) -> Any: if not notification: raise PolyaxonNotificationException( "Received an invalid run `{}`.".format(notification) ) context = cls.serialize_notification_to_context(notification=notification) config = cls.get_config(config=config) if cls.check_config and not config: return False data = cls._prepare(context) try: result = cls._execute(data=data, config=config) except Exception as e: logger.error( "Exception during the execution of the notifier %s. Error: %s", cls.name, e, exc_info=True, ) return return result
def _prepare(cls, context: Dict) -> Dict: """This is where you should alter the context to fit the action. Default behaviour will leave the context as it is. """ if not context and cls.raise_empty_context: raise PolyaxonNotificationException( "{} received invalid payload context.".format(cls.name)) return context
def _get_config(cls) -> ConfigType: """Getting config to execute an action. Currently we get config from env vars. should be a list of urls and potentially a method. If no method is given, then by default we use POST. """ value = os.environ.get(cls.config_key) if not value: raise PolyaxonNotificationException( "Could not validate config for notifier {}".format(cls.name)) return parser.get_dict(key=cls.config_key, value=value, is_list=True)
def _prepare(cls, context: Dict) -> Dict: context = super()._prepare(context) payload = { "username": "******", "avatar_url": context.get("avatar_url"), "tts": context.get("tts", False), } content = context.get("content") if content and len(content) <= 2000: payload["content"] = content else: raise PolyaxonNotificationException( "Discord content must non null and 2000 or fewer characters.") proxy = context.get("proxy") if proxy: payload["https"] = proxy return payload