-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelpers.go
207 lines (176 loc) · 5.15 KB
/
helpers.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
package mailerlite
import (
"net/url"
"strconv"
)
var (
SortByID = "id"
SortByIDDescending = "-id"
SortByName = "name"
SortByNameDescending = "-name"
SortByType = "type"
SortByTypeDescending = "-type"
SortByTotal = "total"
SortByTotalDescending = "-total"
SortByOpenRate = "open_rate"
SortByOpenRateDescending = "-open_rate"
SortByClickRate = "click_rate"
SortByClickRateDescending = "-click_rate"
SortByConversionsCount = "conversions_count"
SortByConversionsCountDescending = "-conversions_count"
SortByConversionRate = "conversion_rate"
SortByConversionRateDescending = "-conversion_rate"
SortByClicksCount = "clicks_count"
SortByClicksCountDescending = "-clicks_count"
SortByOpensCount = "opens_count"
SortByOpensCountDescending = "-opens_count"
SortByVisitors = "visitors"
SortByVisitorsDescending = "-visitors"
SortByLastRegistrationAt = "last_registration_at"
SortByLastRegistrationAtDescending = "-created_at"
SortByCreatedAt = "created_at"
SortByCreatedAtDescending = "-created_at"
SortByUpdatedAt = "updated_at"
SortByUpdatedAtDescending = "-updated_at"
FormTypePopup = "popup"
FormTypeEmbedded = "embedded"
FormTypePromotion = "promotion"
CampaignTypeRegular = "regular"
CampaignTypeAB = "ab"
CampaignTypeResend = "resend"
CampaignScheduleTypeInstant = "instant"
CampaignScheduleTypeScheduled = "scheduled"
CampaignScheduleTypeTimezone = "timezone_based"
)
type Meta struct {
// offset based pagination
CurrentPage int `json:"current_page"`
From int `json:"from"`
LastPage int `json:"last_page"`
Links []MetaLinks `json:"links"`
Path string `json:"path"`
PerPage int `json:"per_page"`
To int `json:"to"`
*Aggregations `json:"aggregations,omitempty"`
*Counts `json:"counts,omitempty"`
// cursor based pagination
Count int `json:"count"`
Last int `json:"last"`
Total int `json:"total"`
TotalUnfiltered int `json:"total_unfiltered,omitempty"`
}
type Aggregations struct {
Total int `json:"total"`
Draft int `json:"draft"`
Ready int `json:"ready"`
Sent int `json:"sent"`
}
type Counts struct {
All int `json:"all"`
Opened int `json:"opened"`
Unopened int `json:"unopened"`
Clicked int `json:"clicked"`
Unsubscribed int `json:"unsubscribed"`
Forwarded int `json:"forwarded"`
Hardbounced int `json:"hardbounced"`
Softbounced int `json:"softbounced"`
Junk int `json:"junk"`
}
// Links manages links that are returned along with a List
type Links struct {
First string `json:"first"`
Last string `json:"last"`
Prev string `json:"prev"`
Next string `json:"next"`
}
type MetaLinks struct {
URL interface{} `json:"url"`
Label string `json:"label"`
Active bool `json:"active"`
}
type OpenRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
type ClickRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
type BounceRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
type UnsubscribeRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
type SpamRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
type HardBounceRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
type SoftBounceRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
type ClickToOpenRate struct {
Float float64 `json:"float"`
String string `json:"string"`
}
// NextPageToken is the page token to request the next page of the list
func (l *Links) NextPageToken() (string, error) {
return l.nextPageToken()
}
// PrevPageToken is the page token to request the previous page of the list
func (l *Links) PrevPageToken() (string, error) {
return l.prevPageToken()
}
func (l *Links) nextPageToken() (string, error) {
if l == nil || l.Next == "" {
return "", nil
}
token, err := pageTokenFromURL(l.Next)
if err != nil {
return "", err
}
return token, nil
}
func (l *Links) prevPageToken() (string, error) {
if l == nil || l.Prev == "" {
return "", nil
}
token, err := pageTokenFromURL(l.Prev)
if err != nil {
return "", err
}
return token, nil
}
// IsLastPage returns true if the current page is the last
func (l *Links) IsLastPage() bool {
return l.isLast()
}
func (l *Links) isLast() bool {
return l.Next == ""
}
func pageForURL(urlText string) (int, error) {
u, err := url.ParseRequestURI(urlText)
if err != nil {
return 0, err
}
pageStr := u.Query().Get("page")
page, err := strconv.Atoi(pageStr)
if err != nil {
return 0, err
}
return page, nil
}
func pageTokenFromURL(urlText string) (string, error) {
u, err := url.ParseRequestURI(urlText)
if err != nil {
return "", err
}
return u.Query().Get("page_token"), nil
}