forked from Exayn/go-listmonk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactional_service.go
157 lines (121 loc) · 3.32 KB
/
transactional_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package listmonk
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type PostTransactionalService struct {
c ClientInterface
subscriberEmail string
subscriberId uint
subscriberEmails []string
subscriberIds []uint
templateId uint
fromEmail string
data map[string]string
headers map[string]string
messenger string
content_type string
}
func (s *PostTransactionalService) SubscriberEmail(email string) *PostTransactionalService {
s.subscriberEmail = email
return s
}
func (s *PostTransactionalService) SubscriberId(id uint) *PostTransactionalService {
s.subscriberId = id
return s
}
func (s *PostTransactionalService) SubscriberEmails(emails []string) *PostTransactionalService {
s.subscriberEmails = emails
return s
}
func (s *PostTransactionalService) SubscriberIds(ids []uint) *PostTransactionalService {
s.subscriberIds = ids
return s
}
func (s *PostTransactionalService) TemplateId(id uint) *PostTransactionalService {
s.templateId = id
return s
}
func (s *PostTransactionalService) FromEmail(email string) *PostTransactionalService {
s.fromEmail = email
return s
}
func (s *PostTransactionalService) Data(data map[string]string) *PostTransactionalService {
s.data = data
return s
}
func (s *PostTransactionalService) Headers(headers map[string]string) *PostTransactionalService {
s.headers = headers
return s
}
func (s *PostTransactionalService) Messenger(messenger string) *PostTransactionalService {
s.messenger = messenger
return s
}
func (s *PostTransactionalService) ContentType(content_type string) *PostTransactionalService {
s.content_type = content_type
return s
}
func (s *PostTransactionalService) Do(ctx context.Context, opts ...requestOption) error {
r := &request{
method: http.MethodPost,
endpoint: "/tx",
}
if s.subscriberEmail != "" {
r.setJsonParam("subscriber_email", s.subscriberEmail)
}
if s.subscriberId != 0 {
r.setJsonParam("subscriber_id", s.subscriberId)
}
if len(s.subscriberEmails) > 0 {
quoted := make([]string, len(s.subscriberEmails))
for i, s := range s.subscriberEmails {
quoted[i] = fmt.Sprintf("\"%s\"", s)
}
emails := "[" + strings.Join(quoted, ", ") + "]"
r.setJsonParam("subscriber_emails", emails)
}
if len(s.subscriberIds) > 0 {
strIds := make([]string, len(s.subscriberIds))
for i, id := range s.subscriberIds {
strIds[i] = fmt.Sprintf("%d", id)
}
ids := "[" + strings.Join(strIds, ", ") + "]"
r.setJsonParam("subscriber_ids", ids)
}
if s.templateId != 0 {
r.setJsonParam("template_id", s.templateId)
}
if s.fromEmail != "" {
r.setJsonParam("from_email", s.fromEmail)
}
if len(s.data) > 0 {
dataJson, err := json.Marshal(s.data)
if err != nil {
return err
}
r.setJsonParam("data", string(dataJson))
}
if len(s.headers) > 0 {
elements := make([]string, 0)
for k, v := range s.headers {
elements = append(elements, fmt.Sprintf("{%s: %s}", fmt.Sprintf("\"%s\"", k), fmt.Sprintf("\"%s\"", v)))
}
headers := "[" + strings.Join(elements, ", ") + "]"
r.setJsonParam("headers", headers)
}
if s.messenger != "" {
r.setJsonParam("messenger", s.messenger)
}
if s.content_type != "" {
r.setJsonParam("content_type", s.content_type)
}
_, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
return nil
}