mirror of
https://github.com/certimate-go/certimate.git
synced 2026-06-22 21:05:48 +08:00
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package notify
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/certimate-go/certimate/internal/domain"
|
|
"github.com/certimate-go/certimate/internal/notify/notifiers"
|
|
)
|
|
|
|
type SendNotificationRequest struct {
|
|
// 提供商相关
|
|
Provider string
|
|
ProviderAccessConfig map[string]any
|
|
ProviderExtendedConfig map[string]any
|
|
|
|
// 通知相关
|
|
Subject string
|
|
Message string
|
|
}
|
|
|
|
type SendNotificationResponse struct{}
|
|
|
|
func (c *Client) SendNotification(ctx context.Context, request *SendNotificationRequest) (*SendNotificationResponse, error) {
|
|
if request == nil {
|
|
return nil, errors.New("the request is nil")
|
|
}
|
|
|
|
providerFactory, err := notifiers.Registries.Get(domain.NotificationProviderType(request.Provider))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
provider, err := providerFactory(¬ifiers.ProviderFactoryOptions{
|
|
ProviderAccessConfig: request.ProviderAccessConfig,
|
|
ProviderExtendedConfig: request.ProviderExtendedConfig,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to initialize notification provider '%s': %w", request.Provider, err)
|
|
}
|
|
|
|
provider.SetLogger(c.logger)
|
|
if _, err := provider.Notify(ctx, request.Subject, request.Message); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &SendNotificationResponse{}, nil
|
|
}
|