-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclients.go
132 lines (109 loc) · 2.67 KB
/
clients.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
package adguard
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
// GetAllClients - Returns all clients
func (c *ADG) GetAllClients() (*AllClients, error) {
// initialize request
req, err := http.NewRequest("GET", fmt.Sprintf("%s/clients", c.HostURL), nil)
if err != nil {
return nil, err
}
// perform request
body, err := c.doRequest(req)
if err != nil {
return nil, err
}
// convert response to an AllClients object
var allClients AllClients
err = json.Unmarshal(body, &allClients)
if err != nil {
return nil, err
}
return &allClients, nil
}
// GetClient - Returns a client based on an identifier
func (c *ADG) GetClient(identifier string) (*Client, error) {
// retrieve all clients
allClients, err := c.GetAllClients()
if err != nil {
return nil, err
}
// go through the clients in the response until we find the one we want
for _, clientInfo := range allClients.Clients {
if clientInfo.Name == identifier {
return &clientInfo, nil
}
}
// when no matches are found
return nil, nil
}
// CreateClient - Create a client
func (c *ADG) CreateClient(client Client) (*Client, error) {
// convert provided client to JSON
rb, err := json.Marshal(client)
if err != nil {
return nil, err
}
// initialize request
req, err := http.NewRequest("POST", fmt.Sprintf("%s/clients/add", c.HostURL), strings.NewReader(string(rb)))
if err != nil {
return nil, err
}
// perform request
body, err := c.doRequest(req)
if err != nil {
return nil, err
}
// appease Go
_ = body
// return the same client that was passed
return &client, nil
}
// UpdateClient - Update a client
func (c *ADG) UpdateClient(clientUpdate ClientUpdate) (*Client, error) {
// convert provided update client to JSON
rb, err := json.Marshal(clientUpdate)
if err != nil {
return nil, err
}
// initialize request
req, err := http.NewRequest("POST", fmt.Sprintf("%s/clients/update", c.HostURL), strings.NewReader(string(rb)))
if err != nil {
return nil, err
}
// perform request
body, err := c.doRequest(req)
if err != nil {
return nil, err
}
// appease Go
_ = body
// return the client data that was passed
return &clientUpdate.Data, nil
}
// DeleteClient - Deletes a client
func (c *ADG) DeleteClient(clientDelete ClientDelete) error {
// convert provided delete client to JSON
rb, err := json.Marshal(clientDelete)
if err != nil {
return err
}
// initialize request
req, err := http.NewRequest("POST", fmt.Sprintf("%s/clients/delete", c.HostURL), strings.NewReader(string(rb)))
if err != nil {
return err
}
// perform request
body, err := c.doRequest(req)
if err != nil {
return err
}
// appease Go
_ = body
// no need to return anything
return nil
}