/
request.go
113 lines (97 loc) · 2.82 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
package gremlin
import (
"encoding/json"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
)
// Request ...
type Request struct {
RequestID string `json:"requestId"`
Op string `json:"op"`
Processor string `json:"processor"`
Args *RequestArgs `json:"args"`
}
// RequestArgs ...
type RequestArgs struct {
Gremlin string `json:"gremlin,omitempty"`
Session string `json:"session,omitempty"`
Bindings Bind `json:"bindings,omitempty"`
Language string `json:"language,omitempty"`
Rebindings Bind `json:"rebindings,omitempty"`
Sasl string `json:"sasl,omitempty"`
BatchSize int `json:"batchSize,omitempty"`
ManageTransaction bool `json:"manageTransaction,omitempty"`
Aliases map[string]string `json:"aliases,omitempty"`
}
// FormattedReq the requests in the appropriate way
type FormattedReq struct {
Op string `json:"op"`
RequestID interface{} `json:"requestId"`
Args *RequestArgs `json:"args"`
Processor string `json:"processor"`
}
// GraphSONSerializer ...
func GraphSONSerializer(req *Request) ([]byte, error) {
form := NewFormattedReq(req)
msg, err := json.Marshal(form)
if err != nil {
return nil, err
}
mimeType := []byte("application/vnd.gremlin-v2.0+json")
var mimeLen = []byte{0x21}
res := append(mimeLen, mimeType...)
res = append(res, msg...)
return res, nil
}
// NewFormattedReq ...
func NewFormattedReq(req *Request) FormattedReq {
rID := map[string]string{"@type": "g:UUID", "@value": req.RequestID}
sr := FormattedReq{RequestID: rID, Processor: req.Processor, Op: req.Op, Args: req.Args}
return sr
}
// Bind ...
type Bind map[string]interface{}
// BuildQuery ...
func BuildQuery(query string) (*Request, error) {
args := &RequestArgs{
Gremlin: query,
Language: "gremlin-groovy",
}
u, err := uuid.NewV4()
if err != nil {
return nil, errors.Wrapf(err, "failed to create uuid version4")
}
uuidString := u.String()
req := &Request{
RequestID: uuidString,
Op: "eval",
Processor: "",
Args: args,
}
return req, nil
}
// Bindings is args setter
func (req *Request) Bindings(bindings Bind) *Request {
req.Args.Bindings = bindings
return req
}
// ManageTransaction is args setter
func (req *Request) ManageTransaction(flag bool) *Request {
req.Args.ManageTransaction = flag
return req
}
// Aliases is args setter
func (req *Request) Aliases(aliases map[string]string) *Request {
req.Args.Aliases = aliases
return req
}
// Session is args setter
func (req *Request) Session(session string) *Request {
req.Args.Session = session
return req
}
// SetProcessor is args setter
func (req *Request) SetProcessor(processor string) *Request {
req.Processor = processor
return req
}