-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
48 lines (44 loc) · 981 Bytes
/
client.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
package puppetquery
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
type HttpError struct {
code int
query QueryString
body string
}
func (e HttpError) Error() string {
return fmt.Sprintf("http status '%s', query %s, body %s", http.StatusText(e.code), e.query.ToJson(), e.body)
}
func do(service string, query QueryString) ([]byte, error) {
req, err := http.NewRequest("GET", endpoint+"/"+service+"?query="+url.QueryEscape(query.ToJson()), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
consume := func(r io.Reader) string {
b, _ := ioutil.ReadAll(r)
if len(b) > 0 {
return string(b)
} else {
return ""
}
}
return nil, HttpError{
code: resp.StatusCode,
query: query,
body: consume(resp.Body),
}
}
return ioutil.ReadAll(resp.Body)
}