-
Notifications
You must be signed in to change notification settings - Fork 54
/
request.go
98 lines (83 loc) · 2.45 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
package gremlin
import (
"encoding/json"
_ "fmt"
"github.com/satori/go.uuid"
)
type Request struct {
RequestId string `json:"requestId"`
Op string `json:"op"`
Processor string `json:"processor"`
Args *RequestArgs `json:"args"`
}
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"`
}
// Formats 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"`
}
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
}
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
}
type Bind map[string]interface{}
func Query(query string) *Request {
args := &RequestArgs{
Gremlin: query,
Language: "gremlin-groovy",
}
u, _ := uuid.NewV4()
uuidString := u.String()
req := &Request{
RequestId: uuidString,
Op: "eval",
Processor: "",
Args: args,
}
return req
}
func (req *Request) Bindings(bindings Bind) *Request {
req.Args.Bindings = bindings
return req
}
func (req *Request) ManageTransaction(flag bool) *Request {
req.Args.ManageTransaction = flag
return req
}
func (req *Request) Aliases(aliases map[string]string) *Request {
req.Args.Aliases = aliases
return req
}
func (req *Request) Session(session string) *Request {
req.Args.Session = session
return req
}
func (req *Request) SetProcessor(processor string) *Request {
req.Processor = processor
return req
}