-
Notifications
You must be signed in to change notification settings - Fork 1
/
req.go
81 lines (70 loc) · 2.07 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
package restconf
import (
"encoding/json"
"net/http"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
type YangPatchRootModel struct {
YangPatch YangPatchModel `json:"ietf-yang-patch:yang-patch"`
}
type YangPatchModel struct {
PatchId string `json:"patch-id"`
Comment string `json:"comment,omitempty"`
Edit []YangPatchEditModel `json:"edit"`
}
type YangPatchEditModel struct {
EditId string `json:"edit-id"`
Operation string `json:"operation"`
Target string `json:"target"`
Point string `json:"point,omitempty"`
Where string `json:"where,omitempty"`
Value json.RawMessage `json:"value,omitempty"`
}
// Body wraps SJSON for building JSON body strings.
// Usage example:
//
// Body{}.Set(Cisco-IOS-XE-native:native.hostname", "ROUTER-1").Str
type Body struct {
Str string
}
// Set sets a JSON path to a value.
func (body Body) Set(path string, value interface{}) 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("Cisco-IOS-XE-native:native", Body{}.Set("hostname", "ROUTER-1").Str).Str
func (body Body) SetRaw(path, rawValue string) Body {
res, _ := sjson.SetRaw(body.Str, path, rawValue)
body.Str = res
return body
}
// Res creates a Res object, i.e. a GJSON result object.
func (body Body) Res() Res {
return Res{Res: gjson.Parse(body.Str)}
}
// Req wraps http.Request for API requests.
type Req struct {
// HttpReq is the *http.Request object.
HttpReq *http.Request
}
// Query sets an HTTP query parameter.
//
// client.GetData("Cisco-IOS-XE-native:native", restconf.Query("content", "config"))
//
// Or set multiple parameters:
//
// client.GetData("Cisco-IOS-XE-native:native",
// restconf.Query("content", "config"),
// restconf.Query("depth", "1"))
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()
}
}