-
Notifications
You must be signed in to change notification settings - Fork 3
/
req.go
77 lines (66 loc) · 1.9 KB
/
req.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
package cc
import (
"net/http"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// Body wraps SJSON for building JSON body strings.
// Usage example:
//
// Body{}.Set("name", "ABC").Str
type Body struct {
Str string
}
// Set sets a JSON path to a value.
func (body Body) Set(path, value string) Body {
res, _ := sjson.Set(body.Str, path, value)
body.Str = res
return body
}
// SetRaw sets a JSON path to a raw string value.
// This is primarily used for building up nested structures, e.g.:
//
// Body{}.SetRaw("children", Body{}.Set("name", "New").Str).Str
func (body Body) SetRaw(path, rawValue string) Body {
res, _ := sjson.SetRaw(body.Str, path, rawValue)
body.Str = res
return body
}
// Delete deletes a JSON path.
func (body Body) Delete(path string) Body {
res, _ := sjson.Delete(body.Str, path)
body.Str = res
return body
}
// Res creates a Res object, i.e. a GJSON result object.
func (body Body) Res() Res {
return gjson.Parse(body.Str)
}
// Req wraps http.Request for API requests.
type Req struct {
// HttpReq is the *http.Request obejct.
HttpReq *http.Request
// LogPayload indicates whether logging of payloads should be enabled.
LogPayload bool
// Synchronous indicates whether the request should be performed synchronously.
Synchronous bool
// MaxAsyncWaitTime is the maximum time to wait for an asynchronous operation.
MaxAsyncWaitTime int
}
// NoLogPayload prevents logging of payloads.
// Primarily used by the Login and Refresh methods where this could expose secrets.
func NoLogPayload(req *Req) {
req.LogPayload = false
}
// Asynchronous operation.
// This is only relevant for POST, PUT or DELETE requests.
func Asynchronous(req *Req) {
req.Synchronous = false
}
// Maximum Asynchronous operation wait time.
// This is only relevant for POST, PUT or DELETE requests.
func MaxAsyncWaitTime(seconds int) func(*Req) {
return func(req *Req) {
req.MaxAsyncWaitTime = seconds
}
}