forked from Virtomize/mailtrain-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblacklist.go
103 lines (84 loc) · 2.22 KB
/
blacklist.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
package gomailtrain
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
)
// BlacklistResponse type
type BlacklistResponse struct {
Data BlacklistData `json:"data"`
}
// BlacklistData type
type BlacklistData struct {
Start int `json:"start"`
Limit int `json:"limit"`
Emails []string `json:"emails"`
}
// GetBlacklistMails returns blacklisted mail addresses
func (a *API) GetBlacklistMails(start int, limit int, search string) (*BlacklistResponse, error) {
ep, err := url.ParseRequestURI(a.endPoint.String() + "/api/blacklist/get")
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, ep.String(), nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("start", strconv.Itoa(start))
q.Add("limit", strconv.Itoa(limit))
q.Add("search", search)
req.URL.RawQuery = q.Encode()
res, err := a.Request(req)
if err != nil {
return nil, err
}
var mails BlacklistResponse
err = json.Unmarshal(res, &mails)
if err != nil {
return nil, err
}
return &mails, nil
}
// AddMailToBlacklist adds a mail to the blacklist
func (a *API) AddMailToBlacklist(mail string) error {
ep, err := url.ParseRequestURI(a.endPoint.String() + "/api/blacklist/add")
if err != nil {
return err
}
data := url.Values{}
data.Set("email", mail)
req, err := http.NewRequest(http.MethodPost, ep.String(), strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
_, err = a.Request(req)
if err != nil {
return err
}
return nil
}
// DeleteMailFromBlacklist adds a mail to the blacklist
func (a *API) DeleteMailFromBlacklist(mail string) error {
ep, err := url.ParseRequestURI(a.endPoint.String() + "/api/blacklist/delete")
if err != nil {
return err
}
data := url.Values{}
data.Set("email", mail)
req, err := http.NewRequest(http.MethodPost, ep.String(), strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
_, err = a.Request(req)
if err != nil {
return err
}
return nil
}