0857314906
- Updated the NotificationRequest struct to include new fields: Title, Type, Priority, and ScheduledAt, improving the flexibility and usability of notification requests. - Implemented validation for the new ScheduledAt field to ensure it is a future timestamp or zero for immediate delivery. - Refactored the NotificationService interface and SDK methods to accept a new Model struct, streamlining the parameters for sending notifications. - Enhanced the builder pattern for constructing notification requests, adding methods for setting Title, Type, Priority, and ScheduledAt, promoting a fluent API design. - Updated service methods for sending notifications (Email, SMS, Push, OTP) to utilize the new Model struct, ensuring consistency across notification types.
179 lines
4.6 KiB
Go
179 lines
4.6 KiB
Go
package notification
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
// SDK provides a high-level interface for the notification service
|
|
type SDK struct {
|
|
service NotificationService
|
|
config *Config
|
|
}
|
|
|
|
// NewSDK creates a new notification SDK with the provided configuration and logger
|
|
func New(config *Config, logger Logger) (*SDK, error) {
|
|
if config == nil {
|
|
config = DefaultConfig()
|
|
}
|
|
|
|
client, err := NewClient(config, logger)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create notification client: %w", err)
|
|
}
|
|
|
|
service := NewService(client)
|
|
|
|
return &SDK{
|
|
service: service,
|
|
config: config,
|
|
}, nil
|
|
}
|
|
|
|
// NewSDKWithClient creates a new notification SDK with a custom HTTP client
|
|
func NewSDKWithClient(config *Config, httpClient HTTPClient, logger Logger) (*SDK, error) {
|
|
if config == nil {
|
|
config = DefaultConfig()
|
|
}
|
|
|
|
client, err := NewClientWithHTTPClient(config, httpClient, logger)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create notification client: %w", err)
|
|
}
|
|
|
|
service := NewService(client)
|
|
|
|
return &SDK{
|
|
service: service,
|
|
config: config,
|
|
}, nil
|
|
}
|
|
|
|
// SendNotification sends a custom notification request
|
|
func (s *SDK) SendNotification(ctx context.Context, req *NotificationRequest) (*NotificationResponse, error) {
|
|
return s.service.SendNotification(ctx, req)
|
|
}
|
|
|
|
// SendEmail sends an email notification
|
|
func (s *SDK) SendEmail(ctx context.Context, model Model) (*NotificationResponse, error) {
|
|
return s.service.SendEmail(ctx, model)
|
|
}
|
|
|
|
// SendSMS sends an SMS notification
|
|
func (s *SDK) SendSMS(ctx context.Context, model Model) (*NotificationResponse, error) {
|
|
return s.service.SendSMS(ctx, model)
|
|
}
|
|
|
|
// SendPush sends a push notification
|
|
func (s *SDK) SendPush(ctx context.Context, model Model) (*NotificationResponse, error) {
|
|
return s.service.SendPush(ctx, model)
|
|
}
|
|
|
|
// SendOTP sends an OTP notification
|
|
func (s *SDK) SendOTP(ctx context.Context, model Model) (*NotificationResponse, error) {
|
|
return s.service.SendOTP(ctx, model)
|
|
}
|
|
|
|
// NewBuilder creates a new notification builder for fluent API usage
|
|
func (s *SDK) NewBuilder() Builder {
|
|
return s.service.(*Service).NewBuilder()
|
|
}
|
|
|
|
// Health checks if the notification service is available
|
|
func (s *SDK) Health(ctx context.Context) error {
|
|
return s.service.Health(ctx)
|
|
}
|
|
|
|
// GetConfig returns the current configuration
|
|
func (s *SDK) GetConfig() *Config {
|
|
return s.config
|
|
}
|
|
|
|
// Convenience functions for quick notifications
|
|
|
|
// QuickEmail sends a simple email notification
|
|
func (s *SDK) QuickEmail(ctx context.Context, model Model) error {
|
|
_, err := s.SendEmail(ctx, model)
|
|
return err
|
|
}
|
|
|
|
// QuickSMS sends a simple SMS notification
|
|
func (s *SDK) QuickSMS(ctx context.Context, model Model) error {
|
|
_, err := s.SendSMS(ctx, model)
|
|
return err
|
|
}
|
|
|
|
// QuickPush sends a simple push notification
|
|
func (s *SDK) QuickPush(ctx context.Context, model Model) error {
|
|
_, err := s.SendPush(ctx, model)
|
|
return err
|
|
}
|
|
|
|
// QuickOTP sends a simple OTP notification
|
|
func (s *SDK) QuickOTP(ctx context.Context, model Model) error {
|
|
_, err := s.SendOTP(ctx, model)
|
|
return err
|
|
}
|
|
|
|
// Batch operations for sending multiple notifications
|
|
|
|
// BatchRequest represents a batch notification request
|
|
type BatchRequest struct {
|
|
Requests []*NotificationRequest `json:"requests"`
|
|
}
|
|
|
|
// BatchResponse represents the response from batch operations
|
|
type BatchResponse struct {
|
|
Responses []BatchItemResponse `json:"responses"`
|
|
Success int `json:"success"`
|
|
Failed int `json:"failed"`
|
|
}
|
|
|
|
// BatchItemResponse represents individual response in batch
|
|
type BatchItemResponse struct {
|
|
Index int `json:"index"`
|
|
Success bool `json:"success"`
|
|
Response *NotificationResponse `json:"response,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// SendBatch sends multiple notifications in batch (sequential for now)
|
|
func (s *SDK) SendBatch(ctx context.Context, requests []*NotificationRequest) (*BatchResponse, error) {
|
|
if len(requests) == 0 {
|
|
return &BatchResponse{
|
|
Responses: []BatchItemResponse{},
|
|
Success: 0,
|
|
Failed: 0,
|
|
}, nil
|
|
}
|
|
|
|
responses := make([]BatchItemResponse, len(requests))
|
|
successCount := 0
|
|
failedCount := 0
|
|
|
|
for i, req := range requests {
|
|
resp, err := s.service.SendNotification(ctx, req)
|
|
if err != nil {
|
|
responses[i] = BatchItemResponse{
|
|
Index: i,
|
|
Success: false,
|
|
Error: err.Error(),
|
|
}
|
|
failedCount++
|
|
} else {
|
|
responses[i] = BatchItemResponse{
|
|
Index: i,
|
|
Success: true,
|
|
Response: resp,
|
|
}
|
|
successCount++
|
|
}
|
|
}
|
|
|
|
return &BatchResponse{
|
|
Responses: responses,
|
|
Success: successCount,
|
|
Failed: failedCount,
|
|
}, nil
|
|
}
|