-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection.go
More file actions
executable file
·251 lines (215 loc) · 6.64 KB
/
Copy pathconnection.go
File metadata and controls
executable file
·251 lines (215 loc) · 6.64 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package gopherneo
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
)
type Connection struct {
httpClient *http.Client
DebugMode bool
Uri string
AuthTokenEncoded string
RestUsername string
RestPassword string
Version string `json:"neo4j_version"`
NodeURI string `json:"node"`
NodeLabelsURI string `json:"node_labels"`
CypherURI string `json:"cypher"`
TransactionURI string `json:"transaction"`
// Extensions interface{} `json:"extensions"`
// RefNodeURI string `json:"reference_node"`
// NodeIndexURI string `json:"node_index"`
// RelIndexURI string `json:"relationship_index"`
// ExtInfoURI string `json:"extensions_info"`
// RelTypesURI string `json:"relationship_types"`
// BatchURI string `json:"batch"`
}
type ErrorResponse struct {
Errors []ErrorMessage `json:"errors"`
Authentication string `json:"authentication"`
}
type ErrorMessage struct {
Code string `json:"code"`
Message string `json:"message"`
}
type TransactionStatement struct {
Cypher string `json:"statement"`
Params map[string]interface{} `json:"parameters"`
ResultTypes []string `json:"resultDataContents"`
}
type TransactionResponse struct {
Results []TransactionResult `json:"results"`
Errors []TransactionError `json:"errors"`
}
type TransactionError struct {
Code string `json:"code"`
Message string `json:"message"`
}
// represents a transactional response result
type TransactionResult struct {
Columns []string `json:"columns"`
Data []map[string][]*json.RawMessage `json:"data"`
}
type CypherResult struct {
ColumnNames []string
Rows [][]*json.RawMessage
}
// get the Neo4j "service root"
// http://docs.neo4j.org/chunked/stable/rest-api-service-root.html
func NewConnection(baseUri string) (c *Connection, err error) {
uri := fmt.Sprintf("%v/db/data/", baseUri) // WARNING: stupid, but trailing '/' is req with neo4j
c = &Connection{httpClient: &http.Client{}, Uri: uri}
err = c.connect(uri)
return
}
func NewConnectionWithToken(baseUri string, token string) (c *Connection, err error) {
uri := fmt.Sprintf("%v/db/data/", baseUri) // WARNING: stupid, but trailing '/' is req with neo4j
c = &Connection{httpClient: &http.Client{}, Uri: uri}
c.SetAuthToken(token)
err = c.connect(uri)
return
}
func (c *Connection) SetAuthToken(token string) {
if len(token) > 0 {
s := fmt.Sprintf(":%s", token)
c.AuthTokenEncoded = base64.StdEncoding.EncodeToString([]byte(s))
}
}
func (c *Connection) SetRestCredentials(username string, password string) {
c.RestUsername = username
c.RestPassword = password
}
// ExecuteCypher will return a slice of "rows", each "row" is a []*json.RawMessage representing
// a slice of node properties that the user can unmarshal themselves
func (c *Connection) ExecuteCypher(cypher string, params *map[string]interface{}) (cr CypherResult, err error) {
// reproducible example using curl
// curl -H "Accept: application/json" \
// -X POST \
// -H "Content-Type: application/json" \
// -H "Accept: application/json; charset=UTF-8" \
// -d "{{\"statements\":[{\"statement\":\"MATCH (t:Thing) WHERE t.name='This \\* That' RETURN t\",\"parameters\":{},\"resultDataContents\":[\"ROW\"]}]}}" \
// http://localhost:7474/db/data/transaction/commit
statement := &TransactionStatement{
Cypher: cypher,
Params: *params,
ResultTypes: []string{"ROW"},
}
// create a new transaction for one single statement
// http://neo4j.com/docs/stable/rest-api-transactional.html#rest-api-begin-and-commit-a-transaction-in-one-request
transaction := struct {
Statements []*TransactionStatement `json:"statements"`
}{}
transaction.Statements = []*TransactionStatement{statement}
// prepare request
reqData, err := json.Marshal(transaction)
if err != nil {
return
}
reqBuf := bytes.NewBuffer(reqData)
uri := joinPath([]string{c.TransactionURI, "commit"})
uri = addURIRestCredentials(c, uri)
req, err := http.NewRequest("POST", uri, reqBuf)
if err != nil {
return
}
c.addDefaultHeaders(req)
// make request
if c.DebugMode {
log.Printf("\n\n%v\n\n", req)
}
data, err := c.performRequest(req)
if err != nil {
return
}
resp := &TransactionResponse{}
err = json.Unmarshal(data, &resp)
if err != nil {
return
}
if len(resp.Errors) > 0 {
err = fmt.Errorf("%v: %v", resp.Errors[0].Code, resp.Errors[0].Message)
return
}
if len(resp.Results) == 0 {
return
}
// expecting only one result, since it's a single statement transaction
tr := resp.Results[0] // TransactionResult
// copy cols and rows into a CypherResult
cr.ColumnNames = tr.Columns
cr.Rows = make([][]*json.RawMessage, 0)
// iterate all 'types' of result sets returned (i.e graph, row, etc)
for _, rType := range tr.Data {
if rawRow, ok := rType["row"]; ok {
if rawRow[0] != nil {
cr.Rows = append(cr.Rows, rawRow)
}
}
}
return
}
// get the Neo4j "service root"
// http://docs.neo4j.org/chunked/stable/rest-api-service-root.html
func (c *Connection) connect(uri string) (err error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return
}
c.addDefaultHeaders(req)
// perform request
data, err := c.performRequest(req)
// check for errors first
e := &ErrorResponse{}
err = json.Unmarshal(data, &e)
if err != nil {
return
}
if len(e.Errors) > 0 {
err = fmt.Errorf("%s: '%s'", e.Errors[0].Code, e.Errors[0].Message)
return
}
// no errors, so unmarshal to Connection obj
err = json.Unmarshal(data, &c)
if err != nil {
return
}
if len(c.TransactionURI) == 0 {
err = fmt.Errorf("Couldn't get TransactionURI from Neo4j, response was: %v", string(data))
}
return
}
func (c *Connection) performRequest(req *http.Request) (data []byte, err error) {
// perform request
res, err := c.httpClient.Do(req)
if err != nil {
return
}
// get bytes from body
data, err = ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return
}
return
}
func (c *Connection) addDefaultHeaders(req *http.Request) {
// add headers used in all Neo4j requests
req.Header.Add("Accept", "application/json; charset=UTF-8")
req.Header.Add("Content-Type", "application/json")
if len(c.AuthTokenEncoded) > 0 {
req.Header.Add("Authorization", fmt.Sprintf("Basic realm=\"Neo4j\" %s", c.AuthTokenEncoded))
}
}
func addURIRestCredentials(c *Connection, uri string) string {
if len(c.RestUsername) > 0 {
// insert URI credentials
replWith := fmt.Sprintf("://%s:%s@", c.RestUsername, c.RestPassword)
return regexp.MustCompile("://").ReplaceAllString(uri, replWith)
}
return uri
}