Passed
Push — master ( 9acbae...dd6fe7 )
by eval
04:36 queued 02:53
created

ses.NewXBulkEmailDestinationList   A

Complexity

Conditions 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nop 3
dl 0
loc 19
rs 9.75
c 0
b 0
f 0
1
package ses
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"fmt"
7
)
8
9
// XSendBulkTemplatedEmail sends bulk template emails.
10
func (svc *SES) XSendBulkTemplatedEmail(ctx context.Context, r XSendBulkTemplatedEmailRequest) (*SendBulkTemplatedEmailResult, error) {
11
	req, err := r.ToRequest()
12
	if err != nil {
13
		return nil, err
14
	}
15
	if req.ConfigurationSetName != "" {
16
		req.ConfigurationSetName = svc.defaultConfigurationSet
17
	}
18
19
	return svc.SendBulkTemplatedEmail(ctx, req)
20
}
21
22
// XSendBulkTemplatedEmailEachList sends bulk template emails.
23
func (svc *SES) XSendBulkTemplatedEmailEachList(ctx context.Context, template, from string, to []string, templateData []map[string]interface{}, tags []map[string]string) (*SendBulkTemplatedEmailResult, error) {
24
	if len(templateData) != 0 && len(to) != len(templateData) {
25
		return nil, fmt.Errorf("list size does not mutch: to:[%d], status:[%d]", len(to), len(templateData))
26
	}
27
28
	req := XSendBulkTemplatedEmailRequest{
29
		From:     from,
30
		Template: template,
31
	}
32
33
	req.Destinations = NewXBulkEmailDestinationList(to, templateData, tags)
34
	return svc.XSendBulkTemplatedEmail(ctx, req)
35
}
36
37
// XSendBulkTemplatedEmailRequest has parameters for `XSendBulkTemplatedEmail` function.
38
type XSendBulkTemplatedEmailRequest struct {
39
	Destinations         []XBulkEmailDestination
40
	From                 string
41
	Template             string
42
	ConfigurationSetName string
43
44
	// optional
45
	DefaultTemplateData map[string]interface{}
46
	DefaultTags         map[string]string
47
}
48
49
// NewXBulkEmailDestinationList creates []XBulkEmailDestination by the email destination and other data.
50
func NewXBulkEmailDestinationList(to []string, templateData []map[string]interface{}, tags []map[string]string) []XBulkEmailDestination {
51
	list := make([]XBulkEmailDestination, len(to))
52
	useTemplateData := len(templateData) != 0
53
	useTags := len(tags) != 0
54
55
	for i := range to {
56
		v := XBulkEmailDestination{
57
			To: []string{to[i]},
58
		}
59
		if useTemplateData {
60
			v.TemplateData = templateData[i]
61
		}
62
		if useTags {
63
			v.Tags = tags[i]
64
		}
65
66
		list[i] = v
67
	}
68
	return list
69
}
70
71
// ToRequest converts to SendBulkTemplatedEmailRequest.
72
func (r XSendBulkTemplatedEmailRequest) ToRequest() (SendBulkTemplatedEmailRequest, error) {
73
	req := SendBulkTemplatedEmailRequest{
74
		Template:             r.Template,
75
		Source:               r.From,
76
		ConfigurationSetName: r.ConfigurationSetName,
77
	}
78
79
	if len(r.Destinations) != 0 {
80
		list := make([]BulkEmailDestination, len(r.Destinations))
81
		for i, v := range r.Destinations {
82
			vv, err := v.ToRequest()
83
			if err != nil {
84
				return req, err
85
			}
86
			list[i] = vv
87
		}
88
		req.Destinations = list
89
	}
90
91
	req.DefaultTemplateData = "{}"
92
	if len(r.DefaultTemplateData) != 0 {
93
		b, err := json.Marshal(r.DefaultTemplateData)
94
		if err != nil {
95
			return req, err
96
		}
97
		req.DefaultTemplateData = string(b)
98
	}
99
100
	if len(r.DefaultTags) != 0 {
101
		tags := make([]MessageTag, 0, len(r.DefaultTags))
102
		for k, v := range r.DefaultTags {
103
			tags = append(tags, MessageTag{
104
				Name:  k,
105
				Value: v,
106
			})
107
		}
108
		req.DefaultTags = tags
109
	}
110
	return req, nil
111
}
112
113
type XBulkEmailDestination struct {
0 ignored issues
show
introduced by
exported type XBulkEmailDestination should have comment or be unexported
Loading history...
114
	To []string
115
116
	// optional
117
	Cc           []string
118
	Bcc          []string
119
	TemplateData map[string]interface{}
120
	Tags         map[string]string
121
}
122
123
func (r XBulkEmailDestination) ToRequest() (BulkEmailDestination, error) {
0 ignored issues
show
introduced by
exported method XBulkEmailDestination.ToRequest should have comment or be unexported
Loading history...
124
	req := BulkEmailDestination{
125
		Destination: Destination{
126
			ToAddresses:  r.To,
127
			CcAddresses:  r.Cc,
128
			BccAddresses: r.Bcc,
129
		},
130
	}
131
132
	req.ReplacementTemplateData = "{}"
133
	if len(r.TemplateData) != 0 {
134
		b, err := json.Marshal(r.TemplateData)
135
		if err != nil {
136
			return req, err
137
		}
138
		req.ReplacementTemplateData = string(b)
139
	}
140
141
	if len(r.Tags) != 0 {
142
		tags := make([]MessageTag, 0, len(r.Tags))
143
		for k, v := range r.Tags {
144
			tags = append(tags, MessageTag{
145
				Name:  k,
146
				Value: v,
147
			})
148
		}
149
		req.ReplacementTags = tags
150
	}
151
	return req, nil
152
}
153