8 2c4d16ae 2021-12-23 o const versionPrefix = "/v1"
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
16 2c4d16ae 2021-12-23 o switch req.Method {
17 dad28b1a 2021-12-30 o case http.MethodGet:
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")
25 dad28b1a 2021-12-30 o return nil, fmt.Errorf("new request: unsupported method %s", req.Method)
27 2c4d16ae 2021-12-23 o return req, nil
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
35 2c4d16ae 2021-12-23 o return c.do(req)
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
43 2c4d16ae 2021-12-23 o return c.do(req)
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
51 2c4d16ae 2021-12-23 o return c.do(req)
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
59 2c4d16ae 2021-12-23 o return c.do(req)
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)