-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocked_services.go
85 lines (70 loc) · 2.04 KB
/
blocked_services.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
package adguard
import (
// custom json module to allow for omitting zero value structs
"fmt"
"net/http"
"strings"
"github.com/clarketm/json"
)
// GetBlockedServicesList - Returns the list of all available services to be blocked
func (c *ADG) GetBlockedServicesList() (*BlockedServicesAll, error) {
// initialize request
req, err := http.NewRequest("GET", fmt.Sprintf("%s/blocked_services/all", 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 BlockedServicesAll object
var blockedServicesAll BlockedServicesAll
err = json.Unmarshal(body, &blockedServicesAll)
if err != nil {
return nil, err
}
return &blockedServicesAll, nil
}
// GetBlockedServices - Returns the services that are blocked globally
func (c *ADG) GetBlockedServices() (*BlockedServicesSchedule, error) {
// initialize request
req, err := http.NewRequest("GET", fmt.Sprintf("%s/blocked_services/get", 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 BlockedServicesSchedule object
var blockedServicesSchedule BlockedServicesSchedule
err = json.Unmarshal(body, &blockedServicesSchedule)
if err != nil {
return nil, err
}
return &blockedServicesSchedule, nil
}
// UpdateBlockedServices - Update blocked services
func (c *ADG) SetBlockedServices(blockedServicesSchedule BlockedServicesSchedule) (*BlockedServicesSchedule, error) {
// convert provided blocked services to JSON
rb, err := json.Marshal(blockedServicesSchedule)
if err != nil {
return nil, err
}
// initialize request
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/blocked_services/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 same blocked services that were passed
return &blockedServicesSchedule, nil
}