-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.go
175 lines (146 loc) · 5.77 KB
/
List.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
package mailchimp
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
errortools "github.com/leapforce-libraries/go_errortools"
go_http "github.com/leapforce-libraries/go_http"
"github.com/leapforce-libraries/go_mailchimp/types"
)
type List struct {
Id string `json:"id"`
WebId int `json:"web_id"`
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"`
DateCreated types.DateTimeString `json:"date_created"`
ListRating int `json:"list_rating"`
EmailTypeOption bool `json:"email_type_option"`
SubscribeUrlShort string `json:"subscribe_url_short"`
SubscribeUrlLong string `json:"subscribe_url_long"`
BeamerAddress string `json:"beamer_address"`
Visibility string `json:"visibility"`
DoubleOptin bool `json:"double_optin"`
HasWelcome bool `json:"has_welcome"`
MarketingPermissions bool `json:"marketing_permissions"`
Modules []string `json:"modules"`
Stats ListStats `json:"stats"`
Links []Link `json:"_links"`
}
type CampaignDefaults struct {
FromName string `json:"from_name"`
FromEmail string `json:"from_email"`
Subject string `json:"subject"`
Language string `json:"language"`
}
type ListStats 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 *types.DateTimeString `json:"campaign_last_sent"`
MergeFieldCount int `json:"merge_field_count"`
AvgSubRate int `json:"avg_sub_rate"`
AvgUnsubRate int `json:"avg_unsub_rate"`
TargetSubRate int `json:"target_sub_rate"`
OpenRate float64 `json:"open_rate"`
ClickRate float64 `json:"click_rate"`
LastSubDate *types.DateTimeString `json:"last_sub_date"`
LastUnsubDate *types.DateTimeString `json:"last_unsub_date"`
}
type ListListsConfig struct {
Fields *[]string
ExcludeFields *[]string
Count *int64
BeforeDateCreated *time.Time
SinceDateCreated *time.Time
BeforeCampaignLastSent *time.Time
SinceCampaignLastSent *time.Time
Email *string
SortField *string
SortDir *string
HasEcommerceStore *bool
IncludeTotalContacts *bool
}
type ListListsResponse struct {
Lists []List `json:"lists"`
TotalItems int `json:"total_items"`
Constraints struct {
MayCreate bool `json:"may_create"`
MaxInstances int `json:"max_instances"`
CurrentTotalInstances int `json:"current_total_instances"`
} `json:"constraints"`
Links []Link `json:"_links"`
}
func (service *Service) ListLists(cfg *ListListsConfig) (*[]List, *errortools.Error) {
if cfg == nil {
return nil, errortools.ErrorMessage("ListListsConfig must not be nil")
}
var lists []List
var values = url.Values{}
if cfg.Fields != nil {
values.Set("fields", strings.Join(*cfg.Fields, ","))
}
if cfg.ExcludeFields != nil {
values.Set("exclude_fields", strings.Join(*cfg.ExcludeFields, ","))
}
var count = countDefault
if cfg.Count != nil {
count = *cfg.Count
}
values.Set("count", fmt.Sprintf("%v", count))
if cfg.BeforeDateCreated != nil {
values.Set("before_date_created", (*cfg.BeforeDateCreated).Format(types.DateTimeFormat))
}
if cfg.SinceDateCreated != nil {
values.Set("since_date_created", (*cfg.SinceDateCreated).Format(types.DateTimeFormat))
}
if cfg.BeforeCampaignLastSent != nil {
values.Set("before_campaign_last_sent", (*cfg.BeforeCampaignLastSent).Format(types.DateTimeFormat))
}
if cfg.SinceCampaignLastSent != nil {
values.Set("since_campaign_last_sent", (*cfg.SinceCampaignLastSent).Format(types.DateTimeFormat))
}
if cfg.Email != nil {
values.Set("email", *cfg.Email)
}
if cfg.SortField != nil {
values.Set("sort_field", *cfg.SortField)
}
if cfg.SortDir != nil {
values.Set("sort_dir", *cfg.SortDir)
}
if cfg.HasEcommerceStore != nil {
values.Set("has_ecommerce_store", fmt.Sprintf("%v", *cfg.HasEcommerceStore))
}
if cfg.IncludeTotalContacts != nil {
values.Set("include_total_contacts", fmt.Sprintf("%v", *cfg.IncludeTotalContacts))
}
for {
var response ListListsResponse
requestConfig := go_http.RequestConfig{
Method: http.MethodGet,
Url: service.url(fmt.Sprintf("lists?%s", values.Encode())),
ResponseModel: &response,
}
_, _, e := service.httpRequest(&requestConfig)
if e != nil {
return nil, e
}
lists = append(lists, response.Lists...)
if len(lists) >= response.TotalItems {
break
}
values.Set("offset", fmt.Sprintf("%v", len(lists)))
}
return &lists, nil
}