11 2c4d16ae 2021-12-23 o const versionPrefix = "/v1"
13 514f2cd1 2022-01-07 o // NewRequest returns an authenticated HTTP request with appropriate header
14 514f2cd1 2022-01-07 o // for sending to an Icinga2 server.
15 514f2cd1 2022-01-07 o func NewRequest(method, url, username, password string, body io.Reader) (*http.Request, error) {
16 2c4d16ae 2021-12-23 o req, err := http.NewRequest(method, url, body)
17 2c4d16ae 2021-12-23 o if err != nil {
18 2c4d16ae 2021-12-23 o return nil, err
20 2c4d16ae 2021-12-23 o switch req.Method {
21 76669f9b 2022-01-11 o case http.MethodGet, http.MethodDelete:
22 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
23 2c4d16ae 2021-12-23 o case http.MethodPost, http.MethodPut:
24 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
25 2c4d16ae 2021-12-23 o req.Header.Set("Content-Type", "application/json")
27 dad28b1a 2021-12-30 o return nil, fmt.Errorf("new request: unsupported method %s", req.Method)
29 514f2cd1 2022-01-07 o req.SetBasicAuth(username, password)
30 2c4d16ae 2021-12-23 o return req, nil
33 9ffce56b 2022-01-31 o // filterEncode url-encodes the filter expression expr in the required
34 9ffce56b 2022-01-31 o // format to be included in a request. Notably, spaces need to be encoded
35 9ffce56b 2022-01-31 o // as "%20", not "+" as returned by the url package.
36 9ffce56b 2022-01-31 o func filterEncode(expr string) string {
37 9ffce56b 2022-01-31 o v := url.Values{}
38 9ffce56b 2022-01-31 o v.Set("filter", expr)
39 9ffce56b 2022-01-31 o return strings.ReplaceAll(v.Encode(), "+", "%20")
42 6d1ce85e 2022-01-12 o func (c *Client) get(path, filter string) (*http.Response, error) {
43 76669f9b 2022-01-11 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
44 76669f9b 2022-01-11 o if err != nil {
45 76669f9b 2022-01-11 o return nil, err
47 6d1ce85e 2022-01-12 o if filter != "" {
48 9ffce56b 2022-01-31 o u.RawQuery = filterEncode(filter)
50 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodGet, u.String(), c.username, c.password, nil)
51 76669f9b 2022-01-11 o if err != nil {
52 76669f9b 2022-01-11 o return nil, err
54 76669f9b 2022-01-11 o return c.Do(req)
57 2c4d16ae 2021-12-23 o func (c *Client) post(path string, body io.Reader) (*http.Response, error) {
58 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
59 514f2cd1 2022-01-07 o req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
60 2c4d16ae 2021-12-23 o if err != nil {
61 2c4d16ae 2021-12-23 o return nil, err
63 514f2cd1 2022-01-07 o return c.Do(req)
66 76669f9b 2022-01-11 o func (c *Client) put(path string, body io.Reader) (*http.Response, error) {
67 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
68 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodPut, url, c.username, c.password, body)
69 2c4d16ae 2021-12-23 o if err != nil {
70 76669f9b 2022-01-11 o return nil, err
72 76669f9b 2022-01-11 o return c.Do(req)
75 02a1a100 2022-01-18 o func (c *Client) delete(path string, cascade bool) (*http.Response, error) {
76 02a1a100 2022-01-18 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
77 2c4d16ae 2021-12-23 o if err != nil {
78 76669f9b 2022-01-11 o return nil, err
81 02a1a100 2022-01-18 o v := url.Values{}
82 02a1a100 2022-01-18 o v.Set("cascade", "1")
83 02a1a100 2022-01-18 o u.RawQuery = v.Encode()
85 02a1a100 2022-01-18 o req, err := NewRequest(http.MethodDelete, u.String(), c.username, c.password, nil)
86 02a1a100 2022-01-18 o if err != nil {
87 02a1a100 2022-01-18 o return nil, err
89 76669f9b 2022-01-11 o return c.Do(req)