-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
50 lines (39 loc) · 796 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
49
50
package druid
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
type Client struct {
URL string
}
type DruidError struct {
Error string `json:"error"`
}
func New(url string) *Client {
return &Client{url}
}
func (c *Client) RunQuery(query *AggregationQuery) ([]byte, error) {
jsonBody, err := query.GetJSON()
if err != nil {
return nil, err
}
readerBody := bytes.NewReader(jsonBody)
resp, err := http.Post(c.URL, "application/json", readerBody)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
var queryError DruidError
err = json.Unmarshal(body, &queryError)
if err != nil {
return nil, err
}
return nil, errors.New(queryError.Error)
}
return body, err
}