-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
185 lines (158 loc) · 4.36 KB
/
request.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package rac
import (
"bytes"
"encoding/json"
"github.com/soderasen-au/go-common/util"
"io"
"net/http"
"path"
"strings"
)
const (
REQ_ROOT_PATH_PREFIX string = "^"
REQ_HOST_PATH_PREFIX string = "*"
)
// GetUrl endpoint starts with `*` means host endpoint (ignore any port number)
// endpoint starts with `^` means root endpoint (from / path)
// `*^` is accepted as host root endpoint
// `^*` is invalid
func (c RestApiClient) GetUrl(endpoint string) string {
u := *c.baseUrl
if strings.HasPrefix(endpoint, REQ_HOST_PATH_PREFIX) {
u.Host = u.Hostname()
endpoint = strings.TrimLeft(endpoint, REQ_HOST_PATH_PREFIX)
}
if strings.HasPrefix(endpoint, REQ_ROOT_PATH_PREFIX) {
u.Path = "/"
endpoint = strings.TrimLeft(endpoint, REQ_ROOT_PATH_PREFIX)
}
u.Path = path.Join(u.Path, endpoint)
return u.String()
}
func GetRootPath(path string) string {
return REQ_ROOT_PATH_PREFIX + path
}
func GetHostPath(path string) string {
return REQ_HOST_PATH_PREFIX + path
}
func GetHostRootPath(path string) string {
return GetHostPath(GetRootPath(path))
}
type RequestOption func(*http.Request) *http.Request
func WithParam(k, v string) RequestOption {
return func(req *http.Request) *http.Request {
query := req.URL.Query()
query.Set(k, v)
req.URL.RawQuery = query.Encode()
return req
}
}
func WithParams(params map[string]string) RequestOption {
return func(req *http.Request) *http.Request {
if params != nil {
query := req.URL.Query()
for k, v := range params {
query.Set(k, v)
}
req.URL.RawQuery = query.Encode()
}
return req
}
}
func WithHeader(k, v string) RequestOption {
return func(req *http.Request) *http.Request {
req.Header.Set(k, v)
return req
}
}
func WithHeaders(headers map[string]string) RequestOption {
return func(req *http.Request) *http.Request {
if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}
return req
}
}
func (c RestApiClient) AddQlikOptions(req *http.Request, Opts ...RequestOption) {
query := req.URL.Query()
if c.Config.Auth.Xrf {
query.Set("Xrfkey", QlikXrfKey)
}
if c.Config.Auth.Method == AuthMethodJWT && util.MaybeNil(c.Config.IsCloud) {
if c.csrfToken != "" {
query.Set("qlik-csrf-token", c.csrfToken)
}
}
req.URL.RawQuery = query.Encode()
for k, v := range c.Config.ExtraHeaders {
req.Header.Set(k, v)
}
if Opts != nil {
for _, opt := range Opts {
if opt != nil {
_ = opt(req)
}
}
}
return
}
func (c *RestApiClient) NewRawRequest(method, url string, body interface{}) (*http.Request, *util.Result) {
var err error
var req *http.Request
if readerBody, isIOReader := body.(io.Reader); isIOReader {
req, err = http.NewRequest(method, url, readerBody)
} else if body != nil {
marshaledBody, _err := json.Marshal(body)
if _err != nil {
return nil, util.Error("MarshalRequestBody", _err)
}
bodyReader := bytes.NewBuffer(marshaledBody)
req, err = http.NewRequest(method, url, bodyReader)
} else {
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
return nil, util.Error("http.NewRequest", err)
}
return req, nil
}
// NewRequest accept object as body since it set json content type header in constructor
func (c *RestApiClient) NewRequest(method, endpoint string, body interface{}, Opts ...RequestOption) (*http.Request, *util.Result) {
url := c.GetUrl(endpoint)
req, res := c.NewRawRequest(method, url, body)
if res != nil {
return nil, res.With("NewRawRequest")
}
c.AddQlikOptions(req, Opts...)
return req, nil
}
func (c *RestApiClient) DoRequest(req *http.Request) (*http.Response, []byte, *util.Result) {
c.Logger.Trace().Msgf("Request ===> %v", req)
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, util.Error("Do", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, nil, util.Errorf("%d: %s", resp.StatusCode, resp.Status)
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, util.Error("ReadResp", err)
}
c.Logger.Trace().Msgf("Response <=== %v", string(buf))
return resp, buf, nil
}
func (c *RestApiClient) Do(method, endpoint string, body interface{}, opts ...RequestOption) (*http.Response, []byte, *util.Result) {
req, res := c.NewRequest(method, endpoint, body, opts...)
if res != nil {
return nil, nil, res.With("NewRequest")
}
resp, buf, res := c.DoRequest(req)
if res != nil {
return nil, nil, res.With("Do")
}
return resp, buf, nil
}