-
Notifications
You must be signed in to change notification settings - Fork 80
/
lists.go
646 lines (498 loc) Β· 19.1 KB
/
lists.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package gochimp3
import (
"errors"
"fmt"
)
const (
lists_path = "/lists"
single_list_path = lists_path + "/%s"
abuse_reports_path = "/lists/%s/abuse_reports"
single_abuse_report_path = abuse_reports_path + "/%s"
activity_path = "/lists/%s/activity"
clients_path = "/lists/%s/clients"
history_path = "/lists/%s/growth-history"
single_history_path = history_path + "/%s"
interest_categories_path = "/lists/%s/interest-categories"
single_interest_category_path = interest_categories_path + "/%s"
interests_path = "/lists/%s/interest-categories/%s/interests"
single_interest_path = interests_path + "/%s"
lists_batch_subscribe_members = "/lists/%s"
merge_fields_path = "/lists/%s/merge-fields"
merge_field_path = merge_fields_path + "/%s"
)
type ListQueryParams struct {
ExtendedQueryParams
BeforeDateCreated string
SinceDateCreated string
BeforeCampaignLastSent string
SinceCampaignLastSent string
Email string
}
func (q ListQueryParams) Params() map[string]string {
m := q.ExtendedQueryParams.Params()
m["before_date_created"] = q.BeforeDateCreated
m["since_date_created"] = q.SinceDateCreated
m["before_campaign_last_sent"] = q.BeforeCampaignLastSent
m["since_campaign_last_sent"] = q.SinceCampaignLastSent
m["email"] = q.Email
return m
}
type ListOfLists struct {
baseList
Lists []ListResponse `json:"lists"`
}
type ListCreationRequest struct {
Name string `json:"name"`
Contact Contact `json:"contact"`
PermissionReminder string `json:"permission_reminder"`
UseArchiveBar bool `json:"use_archive_bar"`
CampaignDefaults CampaignDefaults `json:"campaign_defaults"`
NotifyOnSubscribe string `json:"notify_on_subscribe"`
NotifyOnUnsubscribe string `json:"notify_on_unsubscribe"`
EmailTypeOption bool `json:"email_type_option"`
Visibility string `json:"visibility"`
}
type ListResponse struct {
ListCreationRequest
withLinks
ID string `json:"id"`
DateCreated string `json:"date_created"`
ListRating int `json:"list_rating"`
SubscribeURLShort string `json:"subscribe_url_short"`
SubscribeURLLong string `json:"subscribe_url_long"`
BeamerAddress string `json:"beamer_address"`
Modules []string `json:"modules"`
Stats Stats `json:"stats"`
api *API
}
func (list *ListResponse) CanMakeRequest() error {
if list.ID == "" {
return errors.New("No ID provided on list")
}
return nil
}
type Stats struct {
MemberCount int `json:"member_count"`
UnsubscribeCount int `json:"unsubscribe_count"`
CleanedCount int `json:"cleaned_count"`
MemberCountSinceSend int `json:"member_count_since_send"`
UnsubscribeCountSinceSend int `json:"unsubscribe_count_since_send"`
CleanedCountSinceSend int `json:"cleaned_count_since_send"`
CampaignCount int `json:"campaign_count"`
CampaignLastSent string `json:"campaign_last_sent"`
MergeFieldCount int `json:"merge_field_count"`
AvgSubRate float64 `json:"avg_sub_rate"`
AvgUnsubRate float64 `json:"avg_unsub_rate"`
TargetSubRate float64 `json:"target_sub_rate"`
OpenRate float64 `json:"open_rate"`
ClickRate float64 `json:"click_rate"`
LastSubDate string `json:"last_sub_date"`
LastUnsubDate string `json:"last_unsub_date"`
}
type CampaignDefaults struct {
FromName string `json:"from_name"`
FromEmail string `json:"from_email"`
Subject string `json:"subject"`
Language string `json:"language"`
}
func (api *API) GetLists(params *ListQueryParams) (*ListOfLists, error) {
response := new(ListOfLists)
err := api.Request("GET", lists_path, params, nil, response)
if err != nil {
return nil, err
}
for i, _ := range response.Lists {
response.Lists[i].api = api
}
return response, nil
}
// NewListResponse returns a *ListResponse that is minimally viable for making
// API requests. This is useful for such API requests that depend on a
// ListResponse for its ID (e.g. CreateMember) without having to make a second
// network request to get the list itself.
func (api *API) NewListResponse(id string) *ListResponse {
return &ListResponse{
ID: id,
api: api,
}
}
func (api *API) GetList(id string, params *BasicQueryParams) (*ListResponse, error) {
endpoint := fmt.Sprintf(single_list_path, id)
response := new(ListResponse)
response.api = api
return response, api.Request("GET", endpoint, params, nil, response)
}
func (api *API) CreateList(body *ListCreationRequest) (*ListResponse, error) {
response := new(ListResponse)
response.api = api
return response, api.Request("POST", lists_path, nil, body, response)
}
func (api *API) UpdateList(id string, body *ListCreationRequest) (*ListResponse, error) {
endpoint := fmt.Sprintf(single_list_path, id)
response := new(ListResponse)
response.api = api
return response, api.Request("PATCH", endpoint, nil, body, response)
}
func (api *API) DeleteList(id string) (bool, error) {
endpoint := fmt.Sprintf(single_list_path, id)
return api.RequestOk("DELETE", endpoint)
}
// ------------------------------------------------------------------------------------------------
// Abuse Reports
// ------------------------------------------------------------------------------------------------
type ListOfAbuseReports struct {
baseList
ListID string `json:"list_id"`
Reports []AbuseReport `json:"abuse_reports"`
}
type AbuseReport struct {
ID string `json:"id"`
CampaignID string `json:"campaign_id"`
ListID string `json:"list_id"`
EmailID string `json:"email_id"`
EmailAddress string `json:"email_address"`
Date string `json:"date"`
withLinks
}
func (list *ListResponse) GetAbuseReports(params *ExtendedQueryParams) (*ListOfAbuseReports, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(abuse_reports_path, list.ID)
response := new(ListOfAbuseReports)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list *ListResponse) GetAbuseReport(id string, params *ExtendedQueryParams) (*AbuseReport, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_abuse_report_path, list.ID, id)
response := new(AbuseReport)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
// ------------------------------------------------------------------------------------------------
// Activity
// ------------------------------------------------------------------------------------------------
type ListOfActivity struct {
baseList
ListID string `json:"list_id"`
Activities []Activity `json:"activity"`
}
type Activity struct {
Day string `json:"day"`
EmailsSent int `json:"emails_sent"`
UniqueOpens int `json:"unique_opens"`
RecipientClicks int `json:"recipient_clicks"`
HardBounce int `json:"hard_bounce"`
SoftBounce int `json:"soft_bounce"`
Subs int `json:"subs"`
Unsubs int `json:"unsubs"`
OtherAdds int `json:"other_adds"`
OtherRemoves int `json:"other_removes"`
withLinks
}
func (list *ListResponse) GetActivity(params *BasicQueryParams) (*ListOfActivity, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(activity_path, list.ID)
response := new(ListOfActivity)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
// ------------------------------------------------------------------------------------------------
// Clients
// ------------------------------------------------------------------------------------------------
type ListOfClients struct {
baseList
ListID string `json:"list_id"`
Clients []Client `json:"clients"`
}
type Client struct {
Client string `json:"client"`
Members int `json:"members"`
ListID string `json:"list_id"`
withLinks
}
func (list *ListResponse) GetClients(params *BasicQueryParams) (*ListOfClients, error) {
if list.ID == "" {
return nil, errors.New("No ID provided on list")
}
endpoint := fmt.Sprintf(clients_path, list.ID)
response := new(ListOfClients)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
// ------------------------------------------------------------------------------------------------
// Growth History
// ------------------------------------------------------------------------------------------------
type ListOfGrownHistory struct {
baseList
ListID string `json:"list_id"`
History []GrowthHistory `json:"history"`
}
type GrowthHistory struct {
ListID string `json:"list_id"`
Month string `json:"month"`
Existing int `json:"existing"`
Imports int `json:"imports"`
OptIns int `json:"optins"`
withLinks
}
func (list *ListResponse) GetGrowthHistory(params *ExtendedQueryParams) (*ListOfGrownHistory, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(history_path, list.ID)
response := new(ListOfGrownHistory)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list *ListResponse) GetGrowthHistoryForMonth(month string, params *BasicQueryParams) (*GrowthHistory, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_history_path, list.ID, month)
response := new(GrowthHistory)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
// ------------------------------------------------------------------------------------------------
// Interest Categories
// ------------------------------------------------------------------------------------------------
type ListOfInterestCategories struct {
baseList
ListID string `json:"list_id"`
Categories []InterestCategory `json:"categories"`
}
type InterestCategoryRequest struct {
Title string `json:"title"`
DisplayOrder int `json:"display_order"`
Type string `json:"type"`
}
type InterestCategory struct {
InterestCategoryRequest
ListID string `json:"list_id"`
ID string `json:"id"`
withLinks
api *API
}
func (interestCatgory *InterestCategory) CanMakeRequest() error {
if interestCatgory.ID == "" {
return errors.New("No ID provided on interest category")
}
return nil
}
type InterestCategoriesQueryParams struct {
ExtendedQueryParams
Type string `json:"type"`
}
func (q *InterestCategoriesQueryParams) Params() map[string]string {
m := q.ExtendedQueryParams.Params()
m["type"] = q.Type
return m
}
func (list *ListResponse) GetInterestCategories(params *InterestCategoriesQueryParams) (*ListOfInterestCategories, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(interest_categories_path, list.ID)
response := new(ListOfInterestCategories)
err := list.api.Request("GET", endpoint, params, nil, response)
if err != nil {
return nil, err
}
for i, _ := range response.Categories {
response.Categories[i].api = list.api
}
return response, nil
}
func (list *ListResponse) GetInterestCategory(id string, params *BasicQueryParams) (*InterestCategory, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_interest_category_path, list.ID, id)
response := new(InterestCategory)
response.api = list.api
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list *ListResponse) CreateInterestCategory(body *InterestCategoryRequest) (*InterestCategory, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(interest_categories_path, list.ID)
response := new(InterestCategory)
response.api = list.api
return response, list.api.Request("POST", endpoint, nil, body, response)
}
func (list *ListResponse) UpdateInterestCategory(id string, body *InterestCategoryRequest) (*InterestCategory, error) {
if list.ID == "" {
return nil, errors.New("No ID provided on list")
}
endpoint := fmt.Sprintf(single_interest_category_path, list.ID, id)
response := new(InterestCategory)
response.api = list.api
return response, list.api.Request("PATCH", endpoint, nil, body, response)
}
func (list *ListResponse) DeleteInterestCategory(id string) (bool, error) {
if list.ID == "" {
return false, errors.New("No ID provided on list")
}
endpoint := fmt.Sprintf(single_interest_category_path, list.ID, id)
return list.api.RequestOk("DELETE", endpoint)
}
// ------------------------------------------------------------------------------------------------
// Interest
// ------------------------------------------------------------------------------------------------
type ListOfInterests struct {
Interests []Interest `json:"interests"`
CategoryID string `json:"category_id"`
ListID string `json:"list_id"`
TotalItems int `json:"total_items"`
withLinks
}
type Interest struct {
CategoryID string `json:"category_id"`
ListID string `json:"list_id"`
ID string `json:"id"`
Name string `json:"name"`
DisplayOrder int `json:"display_order"`
withLinks
}
type InterestRequest struct {
Name string `json:"name"`
DisplayOrder int `json:"display_order"`
}
func (list *ListResponse) GetInterests(interestCategoryID string, params *ExtendedQueryParams) (*ListOfInterests, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(interests_path, list.ID, interestCategoryID)
response := new(ListOfInterests)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list *ListResponse) GetInterest(interestCategoryID, interestID string, params *BasicQueryParams) (*Interest, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_interest_path, list.ID, interestCategoryID, interestID)
response := new(Interest)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (interestCategory *InterestCategory) CreateInterest(body *InterestRequest) (*Interest, error) {
if err := interestCategory.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(interests_path, interestCategory.ListID, interestCategory.ID)
response := new(Interest)
return response, interestCategory.api.Request("POST", endpoint, nil, body, response)
}
// ------------------------------------------------------------------------------------------------
// Batch subscribe list members
// ------------------------------------------------------------------------------------------------
type BatchSubscribeMembersError struct {
EmailAddress string `json:"email_address"`
ErrorMessage string `json:"error"`
ErrorCode string `json:"error_code"`
}
type BatchSubscribeMembersResponse struct {
withLinks
NewMembers []Member `json:"new_members"`
UpdatedMembers []Member `json:"updated_members"`
ErrorMessages []BatchSubscribeMembersError `json:"errors"`
TotalCreated int `json:"total_created"`
TotalUpdated int `json:"total_updated"`
ErrorCount int `json:"error_count"`
api *API
}
type BatchSubscribeMembersRequest struct {
Members []MemberRequest `json:"members"`
UpdateExisting bool `json:"update_existing"`
}
func (list *ListResponse) BatchSubscribeMembers(body *BatchSubscribeMembersRequest) (*BatchSubscribeMembersResponse, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(lists_batch_subscribe_members, list.ID)
response := new(BatchSubscribeMembersResponse)
return response, list.api.Request("POST", endpoint, nil, body, response)
}
// ------------------------------------------------------------------------------------------------
// Merge Fields
// ------------------------------------------------------------------------------------------------
type MergeFieldsParams struct {
ExtendedQueryParams
Type string `json:"type"`
Required bool `json:"required"`
}
type MergeFieldParams struct {
BasicQueryParams
MergeID string `json:"_"`
}
type ListOfMergeFields struct {
baseList
ListID string `json:"list_id"`
MergeFields []MergeField `json:"merge_fields"`
}
type MergeField struct {
MergeID int `json:"merge_id"`
Tag string `json:"tag"`
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required"`
DefaultValue string `json:"default_value"`
Public bool `json:"public"`
DisplayOrder int `json:"display_order"`
Options MergeFieldOptions `json:"options"`
HelpText string `json:"help_text"`
ListID string `json:"list_id"`
withLinks
}
type MergeFieldOptions struct {
DefaultCountry int `json:"default_Country"`
PhoneFormat string `json:"phone_format"`
DateFormat string `json:"date_format"`
Choices []string `json:"choices,omitempty"`
Size int `json:"size"`
}
type MergeFieldRequest struct {
// The tag used in MailChimp campaigns and for the /members endpoint.
Tag string `json:"tag"`
// The name of the merge field.
Name string `json:"name"`
// The type for the merge field.
// Possible Values: text, number, address, phone, date, url, image, url, radio, dropdown, birthday, zip
Type string `json:"type"`
// The boolean value if the merge field is required.
Required bool `json:"required"`
// The default value for the merge field if null.
DefaultValue string `json:"default_value"`
// Whether the merge field is displayed on the signup form.
Public bool `json:"public"`
// The order that the merge field displays on the list signup form.
DisplayOrder int `json:"display_order"`
// The order that the merge field displays on the list signup form.
Options MergeFieldOptions `json:"options"`
// Extra text to help the subscriber fill out the form.
HelpText string `json:"help_text"`
}
func (list *ListResponse) GetMergeFields(params *MergeFieldsParams) (*ListOfMergeFields, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(merge_fields_path, list.ID)
response := new(ListOfMergeFields)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list *ListResponse) GetMergeField(params *MergeFieldParams) (*MergeField, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(merge_field_path, list.ID, params.MergeID)
response := new(MergeField)
return response, list.api.Request("GET", endpoint, params, nil, response)
}
func (list *ListResponse) CreateMergeField(body *MergeFieldRequest) (*MergeField, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(merge_fields_path, list.ID)
response := new(MergeField)
return response, list.api.Request("POST", endpoint, nil, body, response)
}