-
Notifications
You must be signed in to change notification settings - Fork 0
/
drill.go
74 lines (60 loc) · 1.16 KB
/
drill.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package drill
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"github.com/k0kubun/pp"
)
var (
queryEndpoint = "%s/query.json"
)
type Drill struct {
URL string
}
type queryRequest struct {
QueryType string `json:"queryType"`
Query string `json:"query"`
}
type queryResponse struct {
Columns []string `json:"columns"`
Rows []map[string]interface{} `json:"rows"`
}
type Config struct {
URL string
Port int
}
func New(url string) *Drill {
return &Drill{
URL: url,
}
}
func (this *Drill) Query(query string) (*queryResponse, error) {
body := queryRequest{QueryType: "SQL", Query: query}
endpoint := fmt.Sprintf(queryEndpoint, this.URL)
b, err := json.Marshal(&body)
if err != nil {
log.Println(err)
return nil, err
}
resp, err := Do("POST", endpoint, bytes.NewBuffer(b))
if err != nil {
log.Println(err)
return nil, err
}
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return nil, err
}
result := &queryResponse{}
err = json.Unmarshal(b, result)
if err != nil {
log.Println(err)
return nil, err
}
pp.Println(result.Columns)
pp.Println(result.Rows)
return result, nil
}