-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
84 lines (66 loc) · 2.11 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
package qps
import (
"encoding/json"
"io"
"github.com/rs/zerolog"
"github.com/soderasen-au/go-common/util"
"github.com/soderasen-au/go-qlik/qlik/rac"
)
const (
QLIK_XRF_KEY string = "ponmlkjihgfedcba"
)
type Client struct {
impl *rac.RestApiClient
}
func NewClient(cfg rac.Config) (*Client, *util.Result) {
cfg.Auth.Xrf = true
restClient, res := rac.New(cfg)
if res != nil {
return nil, res.With("NewRAC")
}
c := &Client{impl: restClient}
return c, nil
}
func (c *Client) Logger() *zerolog.Logger {
return c.impl.Logger
}
func (c *Client) SetLogger(l *zerolog.Logger) {
c.impl.Logger = l
}
func (c *Client) Do(method string, endpoint string, params map[string]string, body interface{}) ([]byte, *util.Result) {
_, buf, res := c.impl.Do(method, endpoint, body, rac.WithParams(params))
if res != nil {
return nil, res.With("Do")
}
return buf, res
}
func (c *Client) DoRaw(method string, endpoint string, extraHeaders map[string]string, params map[string]string, body io.Reader) ([]byte, *util.Result) {
_, buf, res := c.impl.Do(method, endpoint, body, rac.WithParams(params), rac.WithHeaders(extraHeaders))
if res != nil {
return nil, res.With("Do")
}
return buf, res
}
func (c *Client) Get(endpoint string, params map[string]string) ([]byte, *util.Result) {
return c.Do("GET", endpoint, params, nil)
}
func (c *Client) GetRaw(endpoint string, extraHeaders map[string]string, params map[string]string) ([]byte, *util.Result) {
return c.DoRaw("GET", endpoint, extraHeaders, params, nil)
}
func (c *Client) GetObject(endpoint string, o interface{}) *util.Result {
buf, res := c.Do("GET", endpoint, nil, nil)
if res != nil {
return res.With("Do")
}
err := json.Unmarshal(buf, o)
if err != nil {
return util.Error("Unmarshal", err)
}
return nil
}
func (c *Client) Post(endpoint string, params map[string]string, body interface{}) ([]byte, *util.Result) {
return c.Do("POST", endpoint, params, body)
}
func (c *Client) PostRaw(endpoint string, extraHeaders map[string]string, params map[string]string, body io.Reader) ([]byte, *util.Result) {
return c.DoRaw("POST", endpoint, extraHeaders, params, body)
}