Documentation ¶
Overview ¶
Package notify provides an abstraction for sending notifications through multiple services. It allows easy addition of new services and uniform handling of notification sending.
Index ¶
- Variables
- func Send(ctx context.Context, subject, message string, opts ...SendOption) error
- func UseServices(services ...Service)
- type Attachment
- type BadRequestError
- type Dispatcher
- type Option
- type RateLimitError
- type SendConfig
- type SendError
- type SendOption
- type Service
- type ServiceFailureError
- type UnauthorizedError
Constants ¶
This section is empty.
Variables ¶
var DefaultDispatcher = New()
DefaultDispatcher is the default Dispatcher instance.
var ErrNoRecipients = errors.New("no recipients specified")
ErrNoRecipients indicates that there are no recipients specified for a service.
Functions ¶
func Send ¶
func Send(ctx context.Context, subject, message string, opts ...SendOption) error
Send sends a notification with the given subject and message through all the services of the defaultNotify instance. It performs these operations concurrently and returns the first encountered error, if any.
func UseServices ¶
func UseServices(services ...Service)
UseServices appends the given service(s) to the defaultNotify instance's services list. Nil services are ignored.
Types ¶
type Attachment ¶
type Attachment interface { // Reader is used to read the contents of the attachment. io.Reader // Name is used as the filename when sending the attachment. Name() string }
Attachment represents a file that can be attached to a notification message.
type BadRequestError ¶
type BadRequestError struct { // Cause is the underlying error that caused the bad request error. Cause error }
BadRequestError indicates that the request to the remote service was incorrect.
func (*BadRequestError) Error ¶
func (e *BadRequestError) Error() string
Error provides the string representation of the BadRequestError error.
func (*BadRequestError) Unwrap ¶
func (e *BadRequestError) Unwrap() error
Unwrap retrieves the underlying error for the BadRequestError error.
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher handles notifications sending through multiple services.
func New ¶
func New(opts ...Option) *Dispatcher
New creates a Dispatcher instance with the specified options. If no options are provided, a Dispatcher instance is created with no services and default options.
func (*Dispatcher) Send ¶
func (d *Dispatcher) Send(ctx context.Context, subject, message string, opts ...SendOption) error
Send sends a notification with the given subject and message through all the services of n. It performs these operations concurrently and returns the first encountered error, if any.
func (*Dispatcher) UseServices ¶
func (d *Dispatcher) UseServices(services ...Service)
UseServices appends the given service(s) to the Dispatcher instance's services list. Nil services are ignored.
type Option ¶
type Option = func(*Dispatcher)
Option is a function that configures a Dispatcher instance.
func WithServices ¶
WithServices adds the given services to a Dispatcher instance's services list. The services will be used in the order they are provided. Nil services are ignored.
type RateLimitError ¶
type RateLimitError struct { // Cause is the underlying error that caused the rate limit exceeded error. Cause error }
RateLimitError indicates that the rate limit for the service has been exceeded.
func (*RateLimitError) Error ¶
func (e *RateLimitError) Error() string
Error provides the string representation of the RateLimitError error.
func (*RateLimitError) Unwrap ¶
func (e *RateLimitError) Unwrap() error
Unwrap retrieves the underlying error for the RateLimitError error.
type SendConfig ¶
type SendConfig interface { // SetAttachments sets attachments that can be sent alongside the message. SetAttachments(attachments ...Attachment) // SetMetadata sets additional metadata that can be sent with the message. SetMetadata(metadata map[string]any) // SetDryRun sets the dry run flag. SetDryRun(dryRun bool) // SetContinueOnErr sets the continue on error flag. SetContinueOnErr(continueOnError bool) }
SendConfig is used to configure the Send call.
type SendOption ¶
type SendOption = func(SendConfig)
SendOption is a function that modifies the configuration of a Send call.
func SendWithAttachments ¶
func SendWithAttachments(attachments ...Attachment) SendOption
SendWithAttachments attaches the provided files to the message being sent.
func SendWithContinueOnErr ¶
func SendWithContinueOnErr(continueOnErr bool) SendOption
SendWithContinueOnErr sets the continue on error flag. If set to true, the service will continue sending the message to the next recipient even if an error occurred.
func SendWithDryRun ¶
func SendWithDryRun(dryRun bool) SendOption
SendWithDryRun sets the dry run flag. If set to true, the service will not try to authenticate or send the message.
func SendWithMetadata ¶
func SendWithMetadata(metadata map[string]any) SendOption
SendWithMetadata attaches the provided metadata to the message being sent.
type Service ¶
type Service interface { // Name should return a unique identifier for the service. Name() string // Send sends a message with a subject through this service. // Additional options can be provided to customize the sending process. // Returns an error if the sending process failed. Send(ctx context.Context, subject string, message string, opts ...SendOption) error }
Service describes a notification service that can send messages. Each service implementation should provide its own way of sending messages.
type ServiceFailureError ¶
type ServiceFailureError struct { // Service is the name of the service that failed. Service string // Cause is the underlying error that caused the service to fail. Cause error }
ServiceFailureError represents an error that occurs when a service fails.
func (*ServiceFailureError) Error ¶
func (e *ServiceFailureError) Error() string
Error provides the string representation of the ServiceFailureError error.
func (*ServiceFailureError) Unwrap ¶
func (e *ServiceFailureError) Unwrap() error
Unwrap retrieves the underlying error for the ServiceFailureError error.
type UnauthorizedError ¶
type UnauthorizedError struct { error }Cause
UnauthorizedError indicates that the user is not authorized to perform the requested action.
func (*UnauthorizedError) Error ¶
func (e *UnauthorizedError) Error() string
Error provides the string representation of the UnauthorizedError error.
func (*UnauthorizedError) Unwrap ¶
func (e *UnauthorizedError) Unwrap() error
Unwrap retrieves the underlying error for the UnauthorizedError error.