-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
463 lines (381 loc) · 12.8 KB
/
user.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
package sendios
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"net/http"
)
const (
PlatformUnknown = iota
PlatformDesktop
PlatformMobile
PlatformAndroid
PlatformIos
)
const (
VipNo = iota
VipYes
)
const (
GenderMale = "m"
GenderFemale = "f"
)
type UserResponse struct {
Meta `json:"_meta"`
Data struct {
User struct {
ID int64 `json:"id"`
ProjectID int `json:"project_id"`
ProjectTitle string `json:"project_title"`
Email string `json:"email"`
Name string `json:"name"`
Gender string `json:"gender"`
Country interface{} `json:"country"`
Language interface{} `json:"language"`
ErrResponse int `json:"err_response"`
Confirm int `json:"confirm"`
LastOnline interface{} `json:"last_online"`
LastReaction interface{} `json:"last_reaction"`
LastMailed string `json:"last_mailed"`
LastRequest interface{} `json:"last_request"`
Activation interface{} `json:"activation"`
Meta interface{} `json:"meta"`
Clicks int `json:"clicks"`
Sends int `json:"sends"`
CreatedAt string `json:"created_at"`
SentMails []struct {
ID int64 `json:"id"`
CategoryID int `json:"category_id"`
TypeID int `json:"type_id"`
SubjectID int `json:"subject_id"`
TemplateID int `json:"template_id"`
SplitGroup int `json:"split_group"`
SourceID interface{} `json:"source_id"`
ServerID int `json:"server_id"`
CreatedAt string `json:"created_at"`
MailGroupID int `json:"mail_group_id"`
PreHeaderID interface{} `json:"pre_header_id"`
CategorySig string `json:"category_sig"`
ServerName string `json:"server_name"`
ServerIp string `json:"server_ip"`
SourceName string `json:"source_name"`
TypeSig string `json:"type_sig"`
} `json:"sent_mails"`
Unsubscribe interface{} `json:"unsubscribe"`
UnsubscribeTypes []interface{} `json:"unsubscribe_types"`
UnsubPromo interface{} `json:"unsub_promo"`
Webpush interface{} `json:"webpush"`
LastPayment interface{} `json:"last_payment"`
ChannelID interface{} `json:"channel_id"`
SubchannelID interface{} `json:"subchannel_id"`
CustomFields interface{} `json:"custom_fields"`
Vip int `json:"vip"`
} `json:"user"`
} `json:"data"`
}
type UserResponseByID struct {
Meta `json:"_meta"`
Data struct {
Result struct {
User struct {
// ProjectID int `json:"project_id"`
// ListID interface{} `json:"list_id"`
// Email string `json:"email"`
// Name interface{} `json:"name"` // name can be int
// LanguageID interface{} `json:"language_id"`
// CityID int `json:"city_id"`
// VendorID int `json:"vendor_id"`
// ValidID interface{} `json:"valid_id"`
// CountryID interface{} `json:"country_id"`
// PlatformID int `json:"platform_id"`
// Gender string `json:"gender"`
Confirm int `json:"confirm"`
// Vip int `json:"vip"`
// ErrResponse int `json:"err_response"`
// LastOnline int `json:"last_online"`
// LastReaction int `json:"last_reaction"`
// LastMailed int `json:"last_mailed"`
// CreatedAt string `json:"created_at"`
// ID int `json:"id"`
// RegisteredAt string `json:"registered_at"`
// UpdatedAt string `json:"updated_at"`
} `json:"user"`
CustomFields interface{} `json:"custom_fields"`
} `json:"result"`
} `json:"data"`
}
type (
UpdateUserFieldsRequest map[string]interface{}
UpdateUserFieldsResponse struct {
Meta `json:"_meta"`
Data struct {
Result bool `json:"result"`
} `json:"data"`
}
)
func (c *Client) GetUserInfo(email string) (*UserResponse, error) {
url := fmt.Sprintf("https://api.sendios.io/v1/user/project/%d/email/%s", c.Config.Project, email)
statusCode, body, err := c.makeRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrap(err, "get user info")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map get user error")
}
return nil, fmt.Errorf("get user error: %s", resp.Data.Error)
}
var resp UserResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map get user response")
}
return &resp, nil
}
func (c *Client) GetUserInfoByID(id int64) (*UserResponseByID, error) {
// https://sendios.readme.io/reference/get-user-custom-fields-by-user
url := fmt.Sprintf("https://api.sendios.io/v1/userfields/user/%d", id)
statusCode, body, err := c.makeRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrap(err, "get user info")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map get user error")
}
return nil, fmt.Errorf("get user error: %s", resp.Data.Error)
}
var resp UserResponseByID
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map get user response")
}
return &resp, nil
}
func (c *Client) GetUserFields(email string) (*UserResponseByID, error) {
// https://sendios.readme.io/reference/get-user-custom-fields
url := fmt.Sprintf("https://api.sendios.io/v1/userfields/project/%d/email/%s", c.Config.Project, email)
statusCode, body, err := c.makeRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrap(err, "get user info")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map get user error")
}
return nil, fmt.Errorf("get user error: %s", resp.Data.Error)
}
var resp UserResponseByID
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map get user response")
}
return &resp, nil
}
func (c *Client) SetUserData(email string, req UpdateUserFieldsRequest) (*UpdateUserFieldsResponse, error) {
emailHash := base64.StdEncoding.EncodeToString([]byte(email))
data, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrap(err, "marshal payload")
}
url := fmt.Sprintf("https://api.sendios.io/v1/userfields/project/%d/emailhash/%s", c.Config.Project, emailHash)
statusCode, body, err := c.makeRequest(http.MethodPut, url, bytes.NewReader(data))
if err != nil {
return nil, errors.Wrap(err, "save user")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map save user error")
}
return nil, fmt.Errorf("save user error: %s", resp.Data.Error)
}
var resp UpdateUserFieldsResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map save user response")
}
return &resp, nil
}
type UnsubscribeUserResponse struct {
Meta struct {
Status string `json:"status"`
Time int64 `json:"time"`
Count int `json:"count"`
} `json:"_meta"`
Data struct {
Unsub bool `json:"unsub"`
} `json:"data"`
}
func (c *Client) UnsubscribeUser(email string) (*UnsubscribeUserResponse, error) {
emailHash := base64.StdEncoding.EncodeToString([]byte(email))
url := fmt.Sprintf("https://api.sendios.io/v1/unsub/admin/%d/email/%s", c.Config.Project, emailHash)
statusCode, body, err := c.makeRequest(http.MethodPost, url, nil)
if err != nil {
return nil, errors.Wrap(err, "unsubscribe user")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err = json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map unsubscribe user error")
}
return nil, fmt.Errorf("unsubscribe user error: %s", resp.Data.Error)
}
var resp UnsubscribeUserResponse
if err = json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map unsubscribe user response")
}
return &resp, nil
}
type CreateClientUserResponse struct {
Meta struct {
Status string `json:"status"`
Time int64 `json:"time"`
Count int `json:"count"`
} `json:"_meta"`
Data struct {
Message string `json:"message"`
Date string `json:"date"`
Status bool `json:"status"`
} `json:"data"`
}
func (c *Client) CreateClientUser(email string) (*CreateClientUserResponse, error) {
type CreateClientUserRequest struct {
Email string `json:"email"`
ProjectID int `json:"project_id"`
ClientUserID int `json:"client_user_id"`
}
data, err := json.Marshal(CreateClientUserRequest{
Email: email,
ProjectID: c.Config.Project,
ClientUserID: c.Config.ClientID,
})
statusCode, body, err := c.makeRequest(http.MethodPost, "https://api.sendios.io/v1/clientuser/create", bytes.NewReader(data))
if err != nil {
return nil, errors.Wrap(err, "create user")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map create user error")
}
return nil, fmt.Errorf("create user error: %s", resp.Data.Error)
}
var resp CreateClientUserResponse
if err = json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map create user response")
}
return &resp, nil
}
type UnsubscribeStatusResponse struct {
Meta struct {
Status string `json:"status"`
Time int `json:"time"`
Count int `json:"count"`
} `json:"_meta"`
Data struct {
Result struct {
ProjectID int `json:"project_id"`
UserID int `json:"user_id"`
SourceID interface{} `json:"source_id"`
Meta struct {
Message string `json:"message"`
} `json:"meta"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"result"`
} `json:"data"`
}
type SubscribedStatusResponse struct {
Meta struct {
Status string `json:"status"`
Time int `json:"time"`
Count int `json:"count"`
} `json:"_meta"`
Data struct {
Result bool `json:"result"`
} `json:"data"`
}
func (c *Client) CheckIsUnsubscribedUser(userID int64) (bool, error) {
statusCode, body, err := c.makeRequest(http.MethodGet, fmt.Sprintf("https://api.sendios.io/v1/unsub/isunsub/%d", userID), nil)
if err != nil {
return false, errors.Wrap(err, "check unsubscribed")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err = json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return false, errors.Wrap(err, "map check unsubscribed error")
}
return false, fmt.Errorf("check unsubscribed error: %s", resp.Data.Error)
}
var resp UnsubscribeStatusResponse
if err = json.Unmarshal(body, &resp); err != nil {
var r SubscribedStatusResponse
if err = json.Unmarshal(body, &r); err != nil {
return false, errors.Wrap(err, "map check unsubscribed response")
}
return false, nil
}
return true, nil
}
func (c *Client) CheckIsUnsubscribed(email string) (bool, error) {
userInfo, err := c.GetUserInfo(email)
if err != nil {
return false, errors.Wrap(err, "get user info")
}
return c.CheckIsUnsubscribedUser(userInfo.Data.User.ID)
}
type (
ProvideClientRequest struct {
Email string `json:"email"`
ProjectID int `json:"project_id"`
ClientUserID string `json:"client_user_id"`
}
ProvideClientResponse struct {
Meta `json:"_meta"`
Data struct {
Message string `json:"message"`
Date string `json:"date"`
Status bool `json:"status"`
} `json:"data"`
}
)
func (c *Client) ProvideClientID(req ProvideClientRequest) (*ProvideClientResponse, error) {
// https://sendios.readme.io/reference/providing-user-id-on-product
data, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrap(err, "marshal request")
}
statusCode, body, err := c.makeRequest(http.MethodPost, "https://api.sendios.io/v1/clientuser/create", bytes.NewReader(data))
if err != nil {
return nil, errors.Wrap(err, "unsubscribe user")
}
if statusCode != http.StatusOK {
var resp ErrorResponse
if err = json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map unsubscribe user error")
}
return nil, fmt.Errorf("unsubscribe user error: %s", resp.Data.Error)
}
var resp ProvideClientResponse
if err = json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map unsubscribe user response")
}
return &resp, nil
}