10 2c4d16ae 2021-12-23 o const versionPrefix = "/v1"
12 514f2cd1 2022-01-07 o // NewRequest returns an authenticated HTTP request with appropriate header
13 514f2cd1 2022-01-07 o // for sending to an Icinga2 server.
14 514f2cd1 2022-01-07 o func NewRequest(method, url, username, password string, body io.Reader) (*http.Request, error) {
15 2c4d16ae 2021-12-23 o req, err := http.NewRequest(method, url, body)
16 2c4d16ae 2021-12-23 o if err != nil {
17 2c4d16ae 2021-12-23 o return nil, err
19 2c4d16ae 2021-12-23 o switch req.Method {
20 76669f9b 2022-01-11 o case http.MethodGet, http.MethodDelete:
21 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
22 2c4d16ae 2021-12-23 o case http.MethodPost, http.MethodPut:
23 dad28b1a 2021-12-30 o req.Header.Set("Accept", "application/json")
24 2c4d16ae 2021-12-23 o req.Header.Set("Content-Type", "application/json")
26 dad28b1a 2021-12-30 o return nil, fmt.Errorf("new request: unsupported method %s", req.Method)
28 514f2cd1 2022-01-07 o req.SetBasicAuth(username, password)
29 2c4d16ae 2021-12-23 o return req, nil
32 6d1ce85e 2022-01-12 o func (c *Client) get(path, filter string) (*http.Response, error) {
33 76669f9b 2022-01-11 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
34 76669f9b 2022-01-11 o if err != nil {
35 76669f9b 2022-01-11 o return nil, err
37 6d1ce85e 2022-01-12 o if filter != "" {
38 6d1ce85e 2022-01-12 o v := url.Values{}
39 6d1ce85e 2022-01-12 o v.Set("filter", filter)
40 6c703620 2022-01-12 o u.RawQuery = v.Encode()
42 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodGet, u.String(), c.username, c.password, nil)
43 76669f9b 2022-01-11 o if err != nil {
44 76669f9b 2022-01-11 o return nil, err
46 76669f9b 2022-01-11 o return c.Do(req)
49 2c4d16ae 2021-12-23 o func (c *Client) post(path string, body io.Reader) (*http.Response, error) {
50 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
51 514f2cd1 2022-01-07 o req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
52 2c4d16ae 2021-12-23 o if err != nil {
53 2c4d16ae 2021-12-23 o return nil, err
55 514f2cd1 2022-01-07 o return c.Do(req)
58 76669f9b 2022-01-11 o func (c *Client) put(path string, body io.Reader) (*http.Response, error) {
59 514f2cd1 2022-01-07 o url := "https://" + c.addr + versionPrefix + path
60 76669f9b 2022-01-11 o req, err := NewRequest(http.MethodPut, url, c.username, c.password, body)
61 2c4d16ae 2021-12-23 o if err != nil {
62 76669f9b 2022-01-11 o return nil, err
64 76669f9b 2022-01-11 o return c.Do(req)
67 02a1a100 2022-01-18 o func (c *Client) delete(path string, cascade bool) (*http.Response, error) {
68 02a1a100 2022-01-18 o u, err := url.Parse("https://" + c.addr + versionPrefix + path)
69 2c4d16ae 2021-12-23 o if err != nil {
70 76669f9b 2022-01-11 o return nil, err
73 02a1a100 2022-01-18 o v := url.Values{}
74 02a1a100 2022-01-18 o v.Set("cascade", "1")
75 02a1a100 2022-01-18 o u.RawQuery = v.Encode()
77 02a1a100 2022-01-18 o req, err := NewRequest(http.MethodDelete, u.String(), c.username, c.password, nil)
78 02a1a100 2022-01-18 o if err != nil {
79 02a1a100 2022-01-18 o return nil, err
81 76669f9b 2022-01-11 o return c.Do(req)