Skip to content

Base Notifier

Base class for notification delivery implementations.


Base notifier class for notification delivery.

Provides the abstract base class for implementing notification delivery to different channels.

Notifier dataclass

Notifier()

Bases: Generic[NOTIFIER_TARGET]

Abstract base class for notification delivery implementations.

Defines the interface for sending notifications to specific targets. Subclasses implement the actual delivery logic for different channels (e.g., SES, SNS).

Class Type Parameters:

Name Bound or Constraints Description Default
NOTIFIER_TARGET

The target model type for this notifier.

required
Example
@dataclass
class MyNotifier(Notifier[MyTarget]):
    def notify(self, content: NotificationContent, target: MyTarget) -> NotifierResult:
        # Implement delivery logic
        return NotifierResult(success=True, ...)

notifier_target_class classmethod

notifier_target_class()

Get the target class type for this notifier.

Returns:

Type Description
Type[NOTIFIER_TARGET]

The target model class.

Source code in src/aibs_informatics_aws_lambda/handlers/notifications/notifiers/base.py
43
44
45
46
47
48
49
50
@classmethod
def notifier_target_class(cls) -> Type[NOTIFIER_TARGET]:
    """Get the target class type for this notifier.

    Returns:
        The target model class.
    """
    return cls.__orig_bases__[0].__args__[0]  # type: ignore

notify abstractmethod

notify(content, target)

Deliver a notification to the target.

Parameters:

Name Type Description Default
content NotificationContent

The notification content to deliver.

required
target NOTIFIER_TARGET

The delivery target specification.

required

Returns:

Type Description
NotifierResult

Result indicating success or failure.

Source code in src/aibs_informatics_aws_lambda/handlers/notifications/notifiers/base.py
52
53
54
55
56
57
58
59
60
61
62
63
@abstractmethod
def notify(self, content: NotificationContent, target: NOTIFIER_TARGET) -> NotifierResult:
    """Deliver a notification to the target.

    Args:
        content (NotificationContent): The notification content to deliver.
        target (NOTIFIER_TARGET): The delivery target specification.

    Returns:
        Result indicating success or failure.
    """
    raise NotImplementedError("Please implement `notify` method")  # pragma: no cover

parse_target classmethod

parse_target(target)

Parse a target from a dictionary or validate an existing target.

Parameters:

Name Type Description Default
target Union[Dict[str, Any], NOTIFIER_TARGET]

Either a dictionary to parse or an existing target object.

required

Returns:

Type Description
NOTIFIER_TARGET

The parsed or validated target object.

Raises:

Type Description
ValueError

If the target cannot be parsed.

Source code in src/aibs_informatics_aws_lambda/handlers/notifications/notifiers/base.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@classmethod
def parse_target(cls, target: Union[Dict[str, Any], NOTIFIER_TARGET]) -> NOTIFIER_TARGET:
    """Parse a target from a dictionary or validate an existing target.

    Args:
        target (Union[Dict[str, Any], NOTIFIER_TARGET]): Either a dictionary to parse or
            an existing target object.

    Returns:
        The parsed or validated target object.

    Raises:
        ValueError: If the target cannot be parsed.
    """
    if isinstance(target, cls.notifier_target_class()):
        return target
    elif isinstance(target, dict):
        return cls.notifier_target_class().from_dict(target)
    else:
        raise ValueError(f"Could not parse target {target} as {cls.notifier_target_class()}")