-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenforcement.go
84 lines (77 loc) · 2.15 KB
/
enforcement.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
package umbrella
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
)
// Enforcement API Client
type Enforcement struct {
CustomerKey string // Enforcement API token
BaseURL *url.URL
Client HTTPClient // Defaults to http.DefaultClient if nil
}
// DefaultEnforcementURL is the Umbrella service's default URL for the Enforcement API
const DefaultEnforcementURL = "https://s-platform.api.opendns.com"
// NewEnforcement returns a new client for the Enforcement API using the
// default URL.
func NewEnforcement(key string) Enforcement {
u, _ := url.Parse(DefaultEnforcementURL)
return Enforcement{
CustomerKey: key,
BaseURL: u,
}
}
// Get is a convenience function that adds authentication to a request and
// parses the returned JSON into out (should be a pointer).
func (c Enforcement) Get(location string, out interface{}) error {
u, err := url.Parse(location)
if err != nil {
return err
}
q := u.Query()
q.Set("customerKey", c.CustomerKey)
u.RawQuery = q.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
return do(c.Client, req, out)
}
// Post is a convenience function that adds authentication to a request, POSTs
// the content of 'in' as JSON and parses the returned JSON into out. Both, in
// and out should be pointers.
func (c Enforcement) Post(location string, in, out interface{}) error {
u, err := url.Parse(location)
if err != nil {
return err
}
q := u.Query()
q.Set("customerKey", c.CustomerKey)
b := new(bytes.Buffer)
if err = json.NewEncoder(b).Encode(in); err != nil {
return err
}
req, err := http.NewRequest("POST", u.String(), b)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
return do(c.Client, req, out)
}
// Delete is a convenience function that adds authentication to a request and
// makes a DELETE call to the API.
func (c Enforcement) Delete(location string) error {
u, err := url.Parse(location)
if err != nil {
return err
}
q := u.Query()
q.Set("customerKey", c.CustomerKey)
u.RawQuery = q.Encode()
req, err := http.NewRequest("DELETE", u.String(), nil)
if err != nil {
return err
}
return do(c.Client, req, nil)
}