-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
45 lines (38 loc) · 1.22 KB
/
client.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
package cmkapi
import (
"io"
"io/ioutil"
"log"
"net/http"
)
// Client holds the struct for connecting to a Check_MK instance
type Client struct {
User string
Password string
Host string
httpClient *http.Client
}
// NewClient is the skeleton for instantiating a new Check_MK connection
func NewClient(user, password, host string) (*Client, error) {
return &Client{user, password, host, nil}, nil
}
// NewAPIRequest sends and receives the Check_MK webAPI
func (c *Client) NewAPIRequest(method, APICall string, body io.Reader) (resp_body []byte, resp_error error) {
baseurl := c.Host
action := "?action=" + APICall
//credentails := "&_username=" + c.User + "&_secret=" + c.Password
credentials := "&_username=" + c.User + "&_secret=" + c.Password + "&effective_attributes=1"
apiurl := baseurl + action + credentials
request, requestErr := http.NewRequest("POST", apiurl, body)
if requestErr != nil {
log.Fatalf("ERROR: %s", requestErr)
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, doErr := (&http.Client{}).Do(request)
if doErr != nil {
log.Fatalf("ERROR: %s", doErr)
}
defer response.Body.Close()
respBody, err := ioutil.ReadAll(response.Body)
return respBody, err
}