-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_api.go
102 lines (86 loc) · 2.19 KB
/
client_api.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
package cli
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
func (c *clientAPI) getWhitelist() ([]string, error) {
req, err := createGetRequest(c.makeURL("/api/whitelist"))
if err != nil {
return nil, err
}
return c.getBWlist(req)
}
func (c *clientAPI) getBlacklist() ([]string, error) {
req, err := createGetRequest(c.makeURL("/api/blacklist"))
if err != nil {
return nil, err
}
return c.getBWlist(req)
}
func (c *clientAPI) getBWlist(req *http.Request) ([]string, error) {
client := c.getHTTPClient()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ips := struct {
Result []string `json:"result"`
}{}
err = json.Unmarshal(body, &ips)
if err != nil {
return nil, fmt.Errorf("%w: unable parse response", err)
}
return ips.Result, nil
}
func (c *clientAPI) appendBlacklist(clientIP string) error {
return c.sendUpdate("/api/blacklist/add", "ip", clientIP)
}
func (c *clientAPI) appendWhitelist(clientIP string) error {
return c.sendUpdate("/api/whitelist/add", "ip", clientIP)
}
func (c *clientAPI) removeBlacklist(clientIP string) error {
return c.sendUpdate("/api/blacklist/remove", "ip", clientIP)
}
func (c *clientAPI) removeWhitelist(clientIP string) error {
return c.sendUpdate("/api/whitelist/remove", "ip", clientIP)
}
func (c *clientAPI) dropBucketByLogin(key string) error {
return c.sendUpdate("/api/bucket/drop/login", "key", key)
}
func (c *clientAPI) dropBucketByPasswd(key string) error {
return c.sendUpdate("/api/bucket/drop/pwd", "key", key)
}
func (c *clientAPI) dropBucketByIP(key string) error {
return c.sendUpdate("/api/bucket/drop/ip", "key", key)
}
func (c *clientAPI) sendUpdate(path, paramName, value string) error {
client := c.getHTTPClient()
req, err := c.createPostRequest(
c.makeURL(path),
map[string]string{paramName: value},
)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
errResp, err := c.extractError(resp)
if err != nil {
return err
}
return errors.New(errResp.ErrMessage)
}