-
Notifications
You must be signed in to change notification settings - Fork 1
/
req.go
87 lines (76 loc) · 2.21 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
78
79
80
81
82
83
84
85
86
87
package nxos
import (
"net/http"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// Body wraps SJSON for building JSON body strings.
// Usage example:
//
// Body{}.Set("bgpInst.attributes.asn", "100").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("bgpInst.attributes", Body{}.Set("asn", "100").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
// Refresh indicates whether token refresh should be checked for this request.
// Pass NoRefresh to disable Refresh check.
Refresh bool
// LogPayload indicates whether logging of payloads should be enabled.
LogPayload bool
// OverrideUrl indicates a URL to use instead
OverrideUrl string
}
// NoRefresh prevents token refresh check.
// Primarily used by the Login and Refresh methods where this would be redundant.
func NoRefresh(req *Req) {
req.Refresh = false
}
// 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
}
// Query sets an HTTP query parameter.
//
// client.GetClass("bgpInst", nxos.Query("query-target-filter", `eq(bgpInst.asn,"100")`))
//
// Or set multiple parameters:
//
// client.GetClass("bgpInst",
// nxos.Query("rsp-subtree-include", "faults"),
// nxos.Query("query-target-filter", `eq(bgpInst.asn,"100")`))
func Query(k, v string) func(req *Req) {
return func(req *Req) {
q := req.HttpReq.URL.Query()
q.Add(k, v)
req.HttpReq.URL.RawQuery = q.Encode()
}
}