-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
358 lines (327 loc) · 8.46 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"runtime"
"sort"
"strings"
)
func RequestPOST[Req, Resp any](cfg *Config, op Output, hc httpClient, path string, req Req, resp *Resp, headers http.Header) (error, int) {
body, err := json.Marshal(req)
if err != nil {
return err, -1
}
hresp, err := Query(hc).Config(cfg).Output(op).Path(path).Body(bytes.NewBuffer(body)).Header(headers).requestCommon("POST")
if err != nil {
op.Debug("error=%s\n", err)
return err, -1
}
defer hresp.Body.Close()
if hresp.StatusCode > 299 {
return HttpStatusError(op, path, hresp), hresp.StatusCode
}
data, err := io.ReadAll(hresp.Body)
if err != nil {
op.Debug("error=%s\n", err)
return err, hresp.StatusCode
}
op.Debug("response=%s\n", string(data))
return json.Unmarshal(data, resp), hresp.StatusCode
}
func RequestPOSTWithBodyOutput[Req any](cfg *Config, op Output, hc httpClient, path string, req Req, headers http.Header, resp io.Writer) (error, int) {
body, err := json.Marshal(req)
if err != nil {
return err, -1
}
var hresp *http.Response
hresp, err = Query(hc).Config(cfg).Output(op).Path(path).Body(bytes.NewBuffer(body)).Header(headers).requestCommon("POST")
if err != nil {
op.Debug("error=%s\n", err)
return NewObserveError(err, "Network error"), -1
}
defer hresp.Body.Close()
if hresp.StatusCode > 299 {
return HttpStatusError(op, path, hresp), hresp.StatusCode
}
var written int64
written, err = io.Copy(resp, hresp.Body)
op.Debug("bytes_written=%d\n", written)
if err != nil {
return NewObserveError(err, "Error writing response"), hresp.StatusCode
}
if sfqid := hresp.Header.Get("X-Observe-Sfqid"); sfqid != "" {
op.Debug("X-Observe-Sfqid=%s\n", sfqid)
}
return err, hresp.StatusCode
}
func RequestGET[Resp any](cfg *Config, op Output, hc httpClient, path string, args map[string]string, headers http.Header, resp *Resp) (error, int) {
hresp, err := Query(hc).Config(cfg).Output(op).Path(path).Args(args).Header(headers).requestCommon("GET")
if err != nil {
return err, -1
}
defer hresp.Body.Close()
if hresp.StatusCode > 299 {
return HttpStatusError(op, path, hresp), hresp.StatusCode
}
data, err := io.ReadAll(hresp.Body)
if err != nil {
return err, hresp.StatusCode
}
op.Debug("response=%s\n", string(data))
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
return dec.Decode(resp), hresp.StatusCode
}
var dottedQuadRex = regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+(:\d+)?$`)
func SiteUrl(cfg *Config, path string) *url.URL {
protocol := "https://"
cidStr := cfg.CustomerIdStr + "."
// hack it to allow sandboxes to work easily
if strings.HasSuffix(cfg.SiteStr, ":4444") {
protocol = "http://"
}
if dottedQuadRex.MatchString(cfg.SiteStr) {
cidStr = ""
protocol = "http://"
}
str := fmt.Sprintf("%s%s%s%s", protocol, cidStr, cfg.SiteStr, path)
ret, err := url.Parse(str)
if err != nil {
panic(fmt.Sprintf("Somehow, a bad URL was constructed: %q: %s", str, err))
}
return ret
}
func logHeaders(op Output, headers http.Header) {
var keys []string
for k := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
op.Debug("%s=%s\n", k, strings.Join(headers[k], "\n "))
}
}
func HttpStatusError(op Output, url string, hresp *http.Response) error {
var msg map[string]any
data, err := io.ReadAll(hresp.Body)
if err == nil {
op.Debug("body=%s\n", data)
err = json.Unmarshal(data, &msg)
}
if err == nil {
if m, ok := msg["message"]; ok {
return NewObserveError(nil, "%s", m)
} else if m, ok := msg["errors"]; ok {
return NewObserveError(nil, "%v", m)
} else if m, ok := msg["error"]; ok {
return NewObserveError(nil, "%s", m)
}
}
return NewObserveError(nil, "%s: HTTP error: %d", url, hresp.StatusCode)
}
type ApiResponse struct {
Ok bool `json:"ok"`
Message string `json:"message,omitempty"`
Data any `json:"data,omitempty"`
}
type pendingQuery struct {
hc httpClient
cfg *Config
op Output
path string
args map[string]string
header http.Header
body io.Reader
propmap PropertyMap
}
func Query(hc httpClient) *pendingQuery {
return &pendingQuery{
hc: hc,
}
}
func (p *pendingQuery) Config(cfg *Config) *pendingQuery {
p.cfg = cfg
return p
}
func (p *pendingQuery) Output(op Output) *pendingQuery {
p.op = op
return p
}
func (p *pendingQuery) Path(pa string) *pendingQuery {
p.path = pa
return p
}
func (p *pendingQuery) Args(args map[string]string) *pendingQuery {
p.args = args
return p
}
func (p *pendingQuery) Header(h http.Header) *pendingQuery {
p.header = h
return p
}
func (p *pendingQuery) Body(d io.Reader) *pendingQuery {
p.body = d
return p
}
func (p *pendingQuery) PropMap(pm PropertyMap) *pendingQuery {
p.propmap = pm
return p
}
func (p *pendingQuery) GetList() (array, error) {
p.verifyWithProps()
if p.body != nil {
panic("body is not used in GetList")
}
var ar ApiResponse
if err := p.queryDecode("GET", &ar); err != nil {
return nil, err
}
if ar.Data == nil {
return nil, ErrNotAnArray
}
in, is := ar.Data.(array)
if !is {
return nil, ErrNotAnArray
}
ret := make(array, len(in))
for i := range in {
var err error
ret[i], err = propmapObject(in[i], p.propmap)
if err != nil {
return nil, NewObserveError(err, "%s: item %d", p.path, i)
}
}
return ret, nil
}
func (p *pendingQuery) Get() (object, error) {
p.verifyWithProps()
if p.body != nil {
panic("body is not used in GetList")
}
var ar ApiResponse
if err := p.queryDecode("GET", &ar); err != nil {
return nil, err
}
if ar.Data == nil {
return nil, ErrNotAnObject
}
return propmapObject(ar.Data, p.propmap)
}
func (p *pendingQuery) Post() (object, error) {
return p.putPostQuery("POST")
}
func (p *pendingQuery) Put() (object, error) {
return p.putPostQuery("PUT")
}
func (p *pendingQuery) Delete() error {
p.verifyBase()
if p.body != nil {
panic("body is not used in Delete")
}
var ar ApiResponse
if err := p.queryDecode("DELETE", &ar); err != nil {
return err
}
return nil
}
func (p *pendingQuery) putPostQuery(verb string) (object, error) {
p.verifyWithBody()
var ar ApiResponse
if err := p.queryDecode(verb, &ar); err != nil {
return nil, err
}
if ar.Data == nil {
return nil, ErrNotAnObject
}
return propmapObject(ar.Data, p.propmap)
}
func (p *pendingQuery) queryDecode(verb string, ar *ApiResponse) error {
hresp, err := p.queryInner(verb)
if err != nil {
return err
}
defer hresp.Body.Close()
dec := json.NewDecoder(hresp.Body)
if err = dec.Decode(ar); err != nil {
return NewObserveError(err, "%s: resonse decode", p.path)
}
if !ar.Ok {
return NewObserveError(nil, "%s: response not ok", p.path)
}
return nil
}
func (p *pendingQuery) queryInner(verb string) (*http.Response, error) {
hresp, err := p.requestCommon(verb)
if err != nil {
return nil, NewObserveError(err, "%s: request", p.path)
}
if hresp.StatusCode > 299 {
return nil, HttpStatusError(p.op, p.path, hresp)
}
return hresp, nil
}
func (p *pendingQuery) requestCommon(verb string) (*http.Response, error) {
url := SiteUrl(p.cfg, p.path)
if len(p.args) > 0 {
q := url.Query()
for k, v := range p.args {
q.Set(k, v)
}
url.RawQuery = q.Encode()
}
p.op.Debug("url=%s\n", url)
request, err := http.NewRequest(verb, url.String(), p.body)
if err != nil {
p.op.Debug("error=%s\n", err)
return nil, NewObserveError(err, "request error")
}
request.Header.Set("Host", p.cfg.SiteStr)
request.Header.Set("User-Agent", fmt.Sprintf("observe/%s (%s) g=%s", strings.TrimSpace(ReleaseVersion), runtime.GOOS, strings.TrimSpace(GitCommit)))
request.Header.Set("Authorization", p.cfg.AuthHeader())
// this is a reasonable default, but we can override it with the headers parameter
request.Header.Set("Content-Type", "application/json")
for k, v := range p.header {
request.Header.Set(k, v[0])
}
logHeaders(p.op, request.Header)
resp, err := p.hc.Do(request)
if resp != nil {
p.op.Debug("status=%d\n", resp.StatusCode)
logHeaders(p.op, resp.Header)
} else {
p.op.Debug("error=%s\n", err)
}
return resp, err
}
func (p *pendingQuery) verifyBase() {
if p.hc == nil {
panic("missing httpClient")
}
if p.cfg == nil {
panic("missing Config")
}
if p.op == nil {
panic("missing Output")
}
if p.path == "" {
panic("missing path")
}
}
func (p *pendingQuery) verifyWithProps() {
p.verifyBase()
if p.propmap == nil {
panic("missing PropertyMap")
}
}
func (p *pendingQuery) verifyWithBody() {
p.verifyWithProps()
if p.body == nil {
panic("missing body")
}
// headers are optional
}