-
Notifications
You must be signed in to change notification settings - Fork 4
/
sensu.go
155 lines (124 loc) · 3.17 KB
/
sensu.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
/*
Author: Jeff LaPlante - jeff@jefflaplante.com
License: GPLv3
Sensu API Library for Golang
Supports Sensu 0.20.0. Other versions may have unknown breaking changes.
*/
package sensu
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
)
// Config is used to configure the creation of a client
type Config struct {
Address string
Scheme string
Username string
Password string
HTTPClient *http.Client
}
// API Client is used as a handle for all client methods
type API struct {
config Config
}
// DefaultConfig sets up a default configuration struct
func DefaultConfig() *Config {
config := &Config{
Scheme: "http",
Address: "127.0.0.1:4567",
HTTPClient: http.DefaultClient,
}
return config
}
// NewAPIClient gets a new Sensu API client
func NewAPIClient(config *Config) (*API, error) {
defConfig := DefaultConfig()
if len(config.Scheme) == 0 {
config.Scheme = defConfig.Scheme
}
if len(config.Address) == 0 {
config.Address = defConfig.Address
}
if config.HTTPClient == nil {
config.HTTPClient = defConfig.HTTPClient
}
apiClient := &API{
config: *config,
}
return apiClient, nil
}
// Build a http request
func (c *API) buildRequest(method, path string) (*http.Request, error) {
url := &url.URL{
Scheme: c.config.Scheme,
Host: c.config.Address,
Path: path,
}
req, err := http.NewRequest(method, url.String(), nil)
if c.config.Username != "" && c.config.Password != "" {
req.SetBasicAuth(c.config.Username, c.config.Password)
}
return req, err
}
// Build a POST http request
func (c *API) buildRequestPOST(path string, payload interface{}) (*http.Request, error) {
method := "POST"
url := &url.URL{
Scheme: c.config.Scheme,
Host: c.config.Address,
Path: path,
}
// Encode payload struct into JSON and create a reader for it
encodedPayload, err := json.Marshal(payload)
payloadReader := bytes.NewReader(encodedPayload)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, url.String(), payloadReader)
req.Header.Set("Content-Type", "application/json")
return req, err
}
// Send the request to the server
func (c *API) doRequest(req *http.Request) (*http.Response, error) {
resp, err := c.config.HTTPClient.Do(req)
return resp, err
}
// Decode JSON payload
func jsonDecode(out interface{}, data io.ReadCloser) error {
d := json.NewDecoder(data)
error := d.Decode(out)
return error
}
// Generic GET request. Decoded JSON is set in the out interface{} passed in.
func (c *API) get(uri string, out interface{}) (*http.Response, error) {
request, _ := c.buildRequest("GET", uri)
resp, err := c.doRequest(request)
if err != nil {
return nil, err
}
if out != nil {
err = jsonDecode(out, resp.Body)
}
return resp, err
}
// Generic POST request.
func (c *API) post(uri string, payload interface{}) (*http.Response, error) {
request, _ := c.buildRequestPOST(uri, payload)
resp, err := c.doRequest(request)
if err != nil {
return nil, err
}
return resp, err
}
// Generic DELETE request.
func (c *API) delete(uri string) (*http.Response, error) {
request, _ := c.buildRequest("DELETE", uri)
resp, err := c.doRequest(request)
if err != nil {
return nil, err
}
return resp, err
}