forked from circonus-labs/circonus-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
48 lines (38 loc) · 1.03 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright © 2018 Circonus, Inc. <support@circonus.com>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package api
import (
"io/ioutil"
"net/http"
"strings"
"github.com/pkg/errors"
)
func (c *Client) get(reqpath string) ([]byte, error) {
if reqpath == "" {
return nil, errors.New("invalid request path (empty)")
}
au, err := c.agentURL.Parse(reqpath)
if err != nil {
return nil, errors.Wrap(err, "creating request url")
}
client := &http.Client{}
req, err := http.NewRequest("GET", au.String(), nil)
if err != nil {
return nil, errors.Wrap(err, "preparing request")
}
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "request")
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "reading response")
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("%s - %s - %s", resp.Status, au.String(), strings.TrimSpace(string(data)))
}
return data, nil
}