-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
88 lines (69 loc) · 1.72 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gofb
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type Response struct {
Schema struct {
Fields []Field `json:"fields"`
} `json:"schema"`
Data [][]interface{} `json:"data", omitempty`
ExecutionTime int `json:"execution-time"`
Error interface{} `json:"error", omitempty`
}
type Field struct {
Name string `json:"name"`
Type string `json:"type"`
BaseType string `json:"base-type"`
TypeInfo string `json:"-"` // only decimal is using this field, and it is seen as float
}
type client struct {
opt *Options
queryURL string
apiKey string
}
func NewClient(opt *Options) *client {
opt.init()
return &client{opt: opt, queryURL: opt.QueryURL, apiKey: opt.APIKey}
}
func (c *client) Query(query string) (*Response, error) {
u, err := url.Parse(c.queryURL)
if err != nil {
return nil, errors.Join(err, errors.New("invalid query url"))
}
fmt.Printf("query url: %s\n", c.queryURL)
req, err := http.NewRequest(http.MethodPost, c.queryURL, strings.NewReader(query))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "text/plain")
req.Header.Add("Accept", "application/json")
client := &http.Client{}
if c.apiKey != "" && u.Scheme == "https" {
req.Header.Add("X-API-Key", c.apiKey)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
fmt.Println(query)
if resp.StatusCode != 200 {
return nil, errors.Join(errors.New(resp.Status), errors.New("query failed"))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response Response
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return &response, nil
}