-
Notifications
You must be signed in to change notification settings - Fork 14
/
push_service.go
291 lines (257 loc) · 9.72 KB
/
push_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
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
package sendpulse_sdk_go
import (
"context"
"fmt"
"net/http"
"strings"
"time"
)
// PushService is a service to interact with push notifications
type PushService struct {
client *Client
}
// newPushService creates PushService
func newPushService(cl *Client) *PushService {
return &PushService{client: cl}
}
// PushListParams describes params for GetMessages
type PushListParams struct {
Limit int
Offset int
From time.Time
To time.Time
WebsiteID int
}
// Push represents information of push notification
type Push struct {
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
WebsiteID int `json:"website_id"`
From DateTimeType `json:"from"`
To DateTimeType `json:"to"`
Status int `json:"status"`
}
// GetMessages retrieves a list of sent web push campaigns
func (service *PushService) GetMessages(ctx context.Context, params PushListParams) ([]Push, error) {
path := "/push/tasks/"
var urlParts []string
urlParts = append(urlParts, fmt.Sprintf("offset=%d", params.Offset))
if params.Limit != 0 {
urlParts = append(urlParts, fmt.Sprintf("limit=%d", params.Limit))
}
if !params.From.IsZero() {
urlParts = append(urlParts, fmt.Sprintf("from=%s", params.From.Format("2006-01-02")))
}
if !params.To.IsZero() {
urlParts = append(urlParts, fmt.Sprintf("to=%s", params.From.Format("2006-01-02")))
}
if params.WebsiteID != 0 {
urlParts = append(urlParts, fmt.Sprintf("website_id=%d", params.WebsiteID))
}
if len(urlParts) != 0 {
path += "?" + strings.Join(urlParts, "&")
}
var respData []Push
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
// CountWebsites retrieves the total number of websites
func (service *PushService) CountWebsites(ctx context.Context) (int, error) {
path := "/push/websites/total"
var respData struct {
Total int `json:"total"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Total, err
}
type PushWebsite struct {
ID int `json:"id"`
Url string `json:"url"`
AddDate DateTimeType `json:"add_date"`
Status int `json:"status"`
}
// GetWebsites retrieves a list of websites
func (service *PushService) GetWebsites(ctx context.Context, limit, offset int) ([]*PushWebsite, error) {
path := fmt.Sprintf("/push/websites/?limit=%d&offset=%d", limit, offset)
var respData []*PushWebsite
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
// PushWebsiteVariable describes variable of push notification
type PushWebsiteVariable struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
// GetWebsiteVariables returns a list of variables for specific website
func (service *PushService) GetWebsiteVariables(ctx context.Context, websiteID int) ([]*PushWebsiteVariable, error) {
path := fmt.Sprintf("/push/websites/%d/variables", websiteID)
var respData []*PushWebsiteVariable
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
// WebsiteSubscriptionsParams describes params for GetWebsiteSubscriptions
type WebsiteSubscriptionsParams struct {
Limit int
Offset int
From time.Time
To time.Time
}
// WebsiteSubscription represents subscriber
type WebsiteSubscription struct {
ID int `json:"id"`
Browser string `json:"browser"`
Lang string `json:"lang"`
Os string `json:"os"`
CountryCode string `json:"country_code"`
City string `json:"city"`
Variables []PushWebsiteVariable `json:"variables"`
SubscriptionDate DateTimeType `json:"subscription_date"`
Status int `json:"status"`
}
// GetWebsiteSubscriptions returns a list subscribers for a certain website
func (service *PushService) GetWebsiteSubscriptions(ctx context.Context, websiteID int, params WebsiteSubscriptionsParams) ([]*WebsiteSubscription, error) {
path := fmt.Sprintf("/push/websites/%d/subscriptions", websiteID)
var urlParts []string
urlParts = append(urlParts, fmt.Sprintf("offset=%d", params.Offset))
if params.Limit != 0 {
urlParts = append(urlParts, fmt.Sprintf("limit=%d", params.Limit))
}
if !params.From.IsZero() {
urlParts = append(urlParts, fmt.Sprintf("subscription_date_from=%s", params.From.Format("2006-01-02")))
}
if !params.To.IsZero() {
urlParts = append(urlParts, fmt.Sprintf("subscription_date_to=%s", params.From.Format("2006-01-02")))
}
if len(urlParts) != 0 {
path += "?" + strings.Join(urlParts, "&")
}
var respData []*WebsiteSubscription
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
// CountWebsiteSubscriptions returns the total number of website subscribers
func (service *PushService) CountWebsiteSubscriptions(ctx context.Context, websiteID int) (int, error) {
path := fmt.Sprintf("/push/websites/%d/subscriptions/total", websiteID)
var respData struct {
Total int `json:"total"`
}
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData.Total, err
}
// WebsiteInfo describes information about website
type WebsiteInfo struct {
ID int `json:"id"`
Url string `json:"url"`
Status string `json:"status"`
Icon string `json:"icon"`
AddDate DateTimeType `json:"add_date"`
TotalSubscribers int `json:"total_subscribers"`
Unsubscribed int `json:"unsubscribed"`
SubscribersToday int `json:"subscribers_today"`
ActiveSubscribers int `json:"active_subscribers"`
}
// GetWebsiteInfo returns information about specific website
func (service *PushService) GetWebsiteInfo(ctx context.Context, websiteID int) (*WebsiteInfo, error) {
path := fmt.Sprintf("/push/websites/info/%d", websiteID)
var respData *WebsiteInfo
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}
// ActivateSubscription activates a subscriber
func (service *PushService) ActivateSubscription(ctx context.Context, subscriptionID int) error {
path := "/push/subscriptions/state"
type paramsFormat struct {
ID int `json:"id"`
State int `json:"state"`
}
data := paramsFormat{ID: subscriptionID, State: 1}
var respData struct {
Result bool `json:"true"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &respData, true)
return err
}
// DeactivateSubscription deactivates a subscriber
func (service *PushService) DeactivateSubscription(ctx context.Context, subscriptionID int) error {
path := "/push/subscriptions/state"
type paramsFormat struct {
ID int `json:"id"`
State int `json:"state"`
}
data := paramsFormat{ID: subscriptionID, State: 0}
var respData struct {
Result bool `json:"true"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, data, &respData, true)
return err
}
// PushMessageParams describes parameters to CreatePushCampaign
type PushMessageParams struct {
Title string `json:"title"`
WebsiteID int `json:"website_id"`
Body string `json:"body"`
TtlSec int `json:"ttl"`
Link string `json:"link,omitempty"`
FilterLang string `json:"filter_lang,omitempty"`
FilterBrowser string `json:"filter_browser,omitempty"`
FilterRegion string `json:"filter_region,omitempty"`
FilterUrl string `json:"filter_url,omitempty"`
SubscriptionDateFrom time.Time `json:"filter_subscription_date_from,omitempty"`
SubscriptionDateTo time.Time `json:"filter_subscription_date_to,omitempty"`
Filter *struct {
VariableName string `json:"variable_name"`
Operator string `json:"operator"`
Conditions []struct {
Condition string `json:"condition"`
Value interface{} `json:"value"`
} `json:"conditions"`
} `json:"filter,omitempty"`
StretchTimeSec int `json:"stretch_time"`
SendDate DateTimeType `json:"send_date"`
Buttons *struct {
Text string `json:"text"`
Link string `json:"link"`
} `json:"buttons,omitempty"`
Image *struct {
Name string `json:"name"`
DataBase64 string `json:"data"`
} `json:"image,omitempty"`
Icon *struct {
Name string `json:"name"`
DataBase64 string `json:"data"`
} `json:"icon,omitempty"`
}
// CreatePushCampaign creates new push campaign
func (service *PushService) CreatePushCampaign(ctx context.Context, params PushMessageParams) (int, error) {
path := "/push/tasks"
var respData struct {
ID int `json:"id"`
Result bool `json:"true"`
}
_, err := service.client.newRequest(ctx, http.MethodPost, path, params, &respData, true)
return respData.ID, err
}
// PushMessagesStatistics describes statistics on sent campaign
type PushMessagesStatistics struct {
ID int `json:"id"`
Message struct {
Title string `json:"title"`
Text string `json:"text"`
Link string `json:"link"`
}
Website string `json:"website"`
WebsiteID int `json:"website_id"`
Status int `json:"status"`
Send int `json:"send,string"`
Delivered int `json:"delivered"`
Redirect int `json:"redirect"`
}
// GetPushMessagesStatistics returns statistics on sent campaigns
func (service *PushService) GetPushMessagesStatistics(ctx context.Context, taskID int) (*PushMessagesStatistics, error) {
path := fmt.Sprintf("/push/tasks/%d", taskID)
var respData *PushMessagesStatistics
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}