Blame


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