-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathemails_blacklist_service.go
74 lines (58 loc) · 1.77 KB
/
emails_blacklist_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
package sendpulse_sdk_go
import (
"context"
b64 "encoding/base64"
"net/http"
"strings"
)
// BlacklistService is a service to interact with blacklist
type BlacklistService struct {
client *Client
}
// newBlacklistService creates BlacklistService
func newBlacklistService(cl *Client) *BlacklistService {
return &BlacklistService{client: cl}
}
// AddToBlacklist appends an email addresses to a blacklist
func (service *BlacklistService) AddToBlacklist(ctx context.Context, emails []string, comment string) error {
path := "/blacklist"
type paramsFormat struct {
Emails string `json:"emails"`
Comment string `json:"comment,omitempty"`
}
params := paramsFormat{
Emails: b64.StdEncoding.EncodeToString([]byte(strings.Join(emails, ","))),
}
if comment != "" {
params.Comment = comment
}
type response struct {
Result bool
}
var respData response
_, err := service.client.newRequest(ctx, http.MethodPost, path, params, &respData, true)
return err
}
// RemoveFromBlacklist removes an email addresses from a blacklist
func (service *BlacklistService) RemoveFromBlacklist(ctx context.Context, emails []string) error {
path := "/blacklist"
type paramsFormat struct {
Emails string `json:"emails"`
}
params := paramsFormat{
Emails: b64.StdEncoding.EncodeToString([]byte(strings.Join(emails, ","))),
}
type response struct {
Result bool
}
var respData response
_, err := service.client.newRequest(ctx, http.MethodDelete, path, params, &respData, true)
return err
}
// GetEmails returns a list of emails added to blacklist
func (service *BlacklistService) GetEmails(ctx context.Context) ([]string, error) {
path := "/blacklist"
var respData []string
_, err := service.client.newRequest(ctx, http.MethodGet, path, nil, &respData, true)
return respData, err
}