-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipients.go
262 lines (199 loc) · 6.91 KB
/
recipients.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package contacts
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// ToRecipientID converts an email address to a SendGrid recipient ID
//
//
func ToRecipientID(email string) string {
return base64.StdEncoding.EncodeToString([]byte(email))
}
// Recipient is a Contact added to the SendGrid API.
type Recipient struct {
ID string `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
CreatedAt int `json:"created_at"`
// @TODO there are missing fields here
CustomFields []CustomField `json:"-"` // @TODO custom unmarshal json for this
}
func (r *Recipient) MarshalJSON() ([]byte, error) {
b, err := json.Marshal(*r)
if err != nil {
return nil, err
}
var fields map[string]interface{}
err = json.Unmarshal(b, &fields)
if err != nil {
return nil, err
}
for _, f := range r.CustomFields {
fields[f.Name] = f.Value
}
return json.Marshal(fields)
}
// RecipientClient defines methods for interacting with Recipients
type RecipientClient struct {
client *Client
}
// RecipientResponse is a response from operations dealing with Recipients
type RecipientResponse struct {
ErrorCount int `json:"error_count"`
ErrorIndices []int `json:"error_indices"`
UnmodifiedIndices []int `json:"unmodified_indices"`
NewCount int `json:"new_count"`
PersistedRecipients []string `json:"persisted_recipients"`
UpdatedCount int `json:"updated_count"`
Errors []struct {
Message string `json:"message"`
ErrorIndices []int `json:"error_indices"`
}
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (c *RecipientClient) attachIDs(resp *RecipientResponse, recipients []*Recipient) {
if resp == nil {
return
}
count := 0
for index, recipient := range recipients {
if contains(resp.ErrorIndices, index) || contains(resp.UnmodifiedIndices, index) {
continue
}
recipient.ID = resp.PersistedRecipients[count]
count++
}
}
// Add multiple Recipients. Recipient IDs are attached to recipients upon success
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Add-Multiple-Recipients-POST
func (c *RecipientClient) Add(recipients ...*Recipient) (resp *RecipientResponse, err error) {
err = c.client.makeRequest(http.MethodPost, "/contactdb/recipients", recipients, &resp)
c.attachIDs(resp, recipients)
return resp, err
}
// Update a Recipient.
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Update-Recipient-PATCH
func (c *RecipientClient) Update(recipients ...*Recipient) (resp *RecipientResponse, err error) {
err = c.client.makeRequest(http.MethodPatch, "/contactdb/recipients", recipients, &resp)
c.attachIDs(resp, recipients)
return resp, err
}
// Delete one or more Recipients
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Delete-Recipient-DELETE
func (c *RecipientClient) Delete(recipientIDs []string) error {
return c.client.makeRequest(http.MethodDelete, "/contactdb/recipients", recipientIDs, nil)
}
type listRecipientsResponse struct {
Recipients []*Recipient `json:"recipients"`
}
// List Recipients
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#List-Recipients-GET
func (c *RecipientClient) List(page int, pageSize int) ([]*Recipient, error) {
var recipients listRecipientsResponse
err := c.client.makeRequest(http.MethodGet, fmt.Sprintf("/contactdb/recipients?page=%d&page_size=%d", page, pageSize), nil, &recipients)
if err != nil {
return nil, err
}
return recipients.Recipients, nil
}
// Get (Retrieve) a Recipient
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Retrieve-a-Recipient-GET
func (c *RecipientClient) Get(recipientID string) (*Recipient, error) {
var recipient *Recipient
err := c.client.makeRequest(http.MethodGet, "/contactdb/recipients/"+recipientID, nil, &recipient)
if err != nil {
return nil, err
}
return recipient, nil
}
// ListsForRecipient gets the Lists that a Recipient is on.
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Get-the-Lists-the-Recipient-Is-On-GET
func (c *RecipientClient) ListsForRecipient(recipientID string) ([]List, error) {
var lists []List
err := c.client.makeRequest(http.MethodGet, "/contactdb/recipients/"+recipientID+"/lists", nil, &lists)
if err != nil {
return nil, err
}
return lists, err
}
type recipientCountResponse struct {
RecipientCount int `json:"recipient_count"`
}
// BillableCount gets the count of billable Recipients
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Get-a-Count-of-Billable-Recipients-GET
func (c *RecipientClient) BillableCount() (int, error) {
var recipientCount recipientCountResponse
err := c.client.makeRequest(http.MethodGet, "/contactdb/recipients/billable_count", nil, &recipientCount)
if err != nil {
return -1, err
}
return recipientCount.RecipientCount, nil
}
// Count gets the count of recipients
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Get-a-Count-of-Recipients-GET
func (c *RecipientClient) Count() (int, error) {
var recipientCount recipientCountResponse
err := c.client.makeRequest(http.MethodGet, "/contactdb/recipients/count", nil, &recipientCount)
if err != nil {
return -1, err
}
return recipientCount.RecipientCount, nil
}
type recipientSearch struct {
ListID int `json:"list_id"`
Conditions []Condition `json:"conditions"`
}
// SearchWithConditions searches with Conditions
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Search-with-conditions-POST
func (c *RecipientClient) SearchListWithConditions(listID int, conditions ...Condition) ([]*Recipient, error) {
var recipients listRecipientsResponse
err := c.client.makeRequest(http.MethodGet, "/contactdb/recipients/search", recipientSearch{ListID: listID, Conditions: conditions}, &recipients)
if err != nil {
return nil, err
}
return recipients.Recipients, nil
}
type SearchTerm struct {
FieldName string
FieldValue string
}
// Search for recipients matching Search criteria
//
// https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Get-Recipients-Matching-Search-Criteria-GET
func (c *RecipientClient) Search(criteria ...SearchTerm) ([]*Recipient, error) {
u, err := url.Parse("/contactdb/recipients/search")
if err != nil {
return nil, err
}
q := u.Query()
for _, term := range criteria {
q.Add(term.FieldName, url.QueryEscape(term.FieldValue))
}
u.RawQuery = q.Encode()
var recipients listRecipientsResponse
err = c.client.makeRequest(http.MethodGet, u.String(), nil, &recipients)
if err != nil {
return nil, err
}
return recipients.Recipients, err
}