-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.go
203 lines (188 loc) · 5.42 KB
/
methods.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
package spamwatch
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
// This returns the major, minor and patch version of the API. This endpoint doesn't need a Authorization Token.
// https://docs.spamwat.ch/?go#getting-the-api-version
func (s *SpamWatch) Version() (*Version, error) {
b, err := s.MakeRequest(http.MethodGet, "version", nil)
if err != nil {
return nil, err
}
var a = &Version{}
err = json.Unmarshal(b, a)
if err != nil {
return nil, err
}
return a, nil
}
// This returns general stats about the API. Right now this only returns the total ban count.
// https://docs.spamwat.ch/?go#getting-some-stats
func (s *SpamWatch) Stats() (*Stats, error) {
b, err := s.MakeRequest(http.MethodGet, "stats", nil)
if err != nil {
return nil, err
}
var a = &Stats{}
err = json.Unmarshal(b, a)
if err != nil {
return nil, err
}
return a, nil
}
// Check the ban status of a specific User.
// https://docs.spamwat.ch/?go#getting-a-specific-ban
func (s *SpamWatch) GetBan(id int64) (*BanList, error) {
b, err := s.MakeRequest(http.MethodGet, fmt.Sprintf("banlist/%d", id), nil)
if err != nil {
return nil, err
}
var a = &BanList{}
err = json.Unmarshal(b, a)
if err != nil {
return nil, err
}
return a, nil
}
// This returns a list of all Bans.
// https://docs.spamwat.ch/?go#getting-all-bans
func (s *SpamWatch) GetBans() (*[]BanList, error) {
b, err := s.MakeRequest(http.MethodGet, "banlist", nil)
if err != nil {
return nil, err
}
var a = []BanList{}
err = json.Unmarshal(b, &a)
if err != nil {
return nil, err
}
return &a, nil
}
// This returns a newline seperated list of all Bans. This method currently ignores the Accept header and will always return a newline seperated list. In the future it might return a JSON with the corresponding content type.
// https://docs.spamwat.ch/?go#getting-a-list-of-banned-ids
func (s *SpamWatch) GetBansMin() ([]int64, error) {
b, err := s.MakeRequest(http.MethodGet, "banlist/all", nil)
if err != nil {
return nil, err
}
idstrs := strings.Fields(string(b))
a := make([]int64, 0, len(idstrs))
for _, x := range idstrs {
n, err := strconv.ParseInt(x, 10, 0)
if err != nil {
return nil, err
}
a = append(a, n)
}
return a, nil
}
// This method can be used for adding a ban.
// https://docs.spamwat.ch/?go#adding-a-ban
func (s *SpamWatch) AddBan(id int64, reason string, message string) (bool, error) {
_, err := s.MakeRequest(http.MethodPost, "banlist", bytes.NewBuffer([]byte(fmt.Sprintf(`[{"id":%d,"reason":"%s","message":"%s"}]`, id, reason, message))))
if err != nil {
return false, err
}
return true, nil
}
// This method can be used for adding multiple bans at a time.
// https://docs.spamwat.ch/?go#adding-a-ban
func (s *SpamWatch) AddBans(toBan []AddBans) (bool, error) {
jsonBytes, err := json.Marshal(toBan)
if err != nil {
return false, err
}
_, err = s.MakeRequest(http.MethodPost, "banlist", bytes.NewBuffer(jsonBytes))
if err != nil {
return false, err
}
return true, nil
}
// Deleting a ban
// https://docs.spamwat.ch/?go#deleting-a-ban
func (s *SpamWatch) DeleteBan(id int64) (bool, error) {
_, err := s.MakeRequest(http.MethodDelete, fmt.Sprintf("banlist/%d", id), nil)
if err != nil {
return false, err
}
return true, nil
}
// This returns the Token the request was made with. Useful for checking the permission Level of the token.
// https://docs.spamwat.ch/?go#getting-your-own-token
func (s *SpamWatch) GetSelf() (*Tokens, error) {
b, err := s.MakeRequest(http.MethodGet, "tokens/self", nil)
if err != nil {
return nil, err
}
var a = &Tokens{}
err = json.Unmarshal(b, a)
if err != nil {
return nil, err
}
return a, nil
}
// This returns a list of all Tokens.
// https://docs.spamwat.ch/?go#getting-all-tokens
func (s *SpamWatch) GetTokens() (*[]Tokens, error) {
b, err := s.MakeRequest(http.MethodGet, "tokens", nil)
if err != nil {
return nil, err
}
var a = []Tokens{}
err = json.Unmarshal(b, &a)
if err != nil {
return nil, err
}
return &a, nil
}
// This returns a specific Tokens.
// https://docs.spamwat.ch/?go#getting-a-specific-token
func (s *SpamWatch) GetToken(id int) (*Tokens, error) {
b, err := s.MakeRequest(http.MethodGet, fmt.Sprintf("tokens/%d", id), nil)
if err != nil {
return nil, err
}
var a = &Tokens{}
err = json.Unmarshal(b, a)
if err != nil {
return nil, err
}
return a, nil
}
// This returns a list of all tokens associated with the specified user id.
// https://docs.spamwat.ch/?go#getting-a-users-tokens
func (s *SpamWatch) GetUserTokens(id int64) (*[]Tokens, error) {
b, err := s.MakeRequest(http.MethodGet, fmt.Sprintf("tokens/userid/%d", id), nil)
if err != nil {
return nil, err
}
var a = []Tokens{}
err = json.Unmarshal(b, &a)
if err != nil {
return nil, err
}
return &a, nil
}
// Creating a Token
// https://docs.spamwat.ch/?go#creating-a-token
func (s *SpamWatch) CreateToken(userId int64, permission string) (bool, error) {
_, err := s.MakeRequest(http.MethodPost, "tokens", bytes.NewBuffer([]byte(fmt.Sprintf(`{"id":%d,"permission":"%s"}`, userId, permission))))
if err != nil {
return false, err
}
return true, nil
}
// This retires a specific Token. The Token won't be able to make any requests anymore.
// https://docs.spamwat.ch/?go#retiring-a-specific-token
func (s *SpamWatch) DeleteToken(tokenId int) (bool, error) {
_, err := s.MakeRequest(http.MethodDelete, fmt.Sprintf("tokens/%d", tokenId), nil)
if err != nil {
return false, err
}
return true, nil
}