Blob


1 package icinga
3 import (
4 "io"
5 "net/http"
6 )
8 const versionPrefix = "/v1"
10 func newRequest(method, host, path string, body io.Reader) (*http.Request, error) {
11 url := "https://" + host + versionPrefix + path
12 req, err := http.NewRequest(method, url, body)
13 if err != nil {
14 return nil, err
15 }
16 switch req.Method {
17 case http.MethodGet:
18 break
19 case http.MethodDelete:
20 req.Header.Set("Accept", "application/json")
21 case http.MethodPost, http.MethodPut:
22 req.Header.Set("Accept", "application/json")
23 req.Header.Set("Content-Type", "application/json")
24 default:
25 return nil, fmt.Errorf("new request: unsupported method %s", req.Method)
26 }
27 return req, nil
28 }
30 func (c *Client) get(path string) (*http.Response, error) {
31 req, err := newRequest(http.MethodGet, c.host, path, nil)
32 if err != nil {
33 return nil, err
34 }
35 return c.do(req)
36 }
38 func (c *Client) post(path string, body io.Reader) (*http.Response, error) {
39 req, err := newRequest(http.MethodPost, c.host, path, body)
40 if err != nil {
41 return nil, err
42 }
43 return c.do(req)
44 }
46 func (c *Client) put(path string, body io.Reader) (*http.Response, error) {
47 req, err := newRequest(http.MethodPut, c.host, path, body)
48 if err != nil {
49 return nil, err
50 }
51 return c.do(req)
52 }
54 func (c *Client) delete(path string, body io.Reader) (*http.Response, error) {
55 req, err := newRequest(http.MethodDelete, c.host, path, nil)
56 if err != nil {
57 return nil, err
58 }
59 return c.do(req)
60 }
62 func (c *Client) do(req *http.Request) (*http.Response, error) {
63 req.SetBasicAuth(c.username, c.password)
64 return c.Do(req)
65 }