-
Notifications
You must be signed in to change notification settings - Fork 9
/
neo4j.go
147 lines (131 loc) · 3.49 KB
/
neo4j.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
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
package neo4j
import (
"net/http"
"net/url"
)
// Neo4j base struct
type Neo4j struct {
Client *http.Client
BaseURL string
NodeURL string
BatchURL string
RelationshipURL string
IndexNodeURL string
BasicAuthUser string
BasicAuthPassword string
}
// Connect creates the basic structure to send requests to neo4j rest endpoint.
// This will enable communication with only one server
//
// It is using Golang HTTP Package inside, so it will re-use the persistent
// TCP connection to given server.
//
// This method is generally called only once for Server
// Close method is not required and not implemented
//
// The connection string should be provided as;
//
// [http://][host][:port]
//
// For example;
//
// http://127.0.0.1:7474
//
// If you pass empty string it will try to connect;
//
// http://127.0.0.1:7474
//
// TODO implement cluster connection
func Connect(urlString string) *Neo4j {
if urlString == "" {
urlString = "http://127.0.0.1:7474"
}
var username string
var password string
var baseURL string
u, parseErr := url.Parse(urlString)
if parseErr != nil {
baseURL = urlString + "/db/data"
username = ""
password = ""
} else {
if u.User != nil {
p, ok := u.User.Password()
if ok {
username, password = u.User.Username(), p
} else {
username, password = "", ""
}
baseURL = u.Scheme + "://" + u.Host + "/db/data"
} else {
baseURL = urlString + "/db/data"
username = ""
password = ""
}
}
// TODO get neo4j service root from neo4j itself
// http://docs.neo4j.org/chunked/stable/rest-api-service-root.html
return &Neo4j{
Client: http.DefaultClient,
BaseURL: baseURL,
NodeURL: baseURL + "/node",
BatchURL: baseURL + "/batch",
IndexNodeURL: baseURL + "/index/node",
RelationshipURL: baseURL + "/relationship",
BasicAuthUser: username,
BasicAuthPassword: password,
}
}
// Get This is the basic Get method for all types
// It accepts only Batcher Interface
// Node and Relationship structs implement this interface
//
// Example usages;
//
// Node:
// neo4jConnection := Connect("")
// node := &Node{}
// node.Id = "2229"
// err := neo4jConnection.Get(node)
// fmt.Println(node)
//
// Relationship:
// neo4jConnection := Connect("")
// rel := &Relationship{}
// rel.Id = "2229"
// neo4jConnection.Get(rel)
func (neo4j *Neo4j) Get(obj Batcher) error {
_, err := neo4j.NewBatch().Get(obj).Execute()
return err
}
// Create is the basic Create method for all types
// It accepts only Batcher Interface
// Example Usages;
// Relationship:
// dataRel := make(map[string]interface{})
// dataRel["RelData"] = "DataOfTheRelationship"
//
// neo4jConnection := Connect("")
// rel := &Relationship{}
// rel.Data = dataRel
// rel.Type = "sampleType"
// rel.StartNodeId = node.Id
// rel.EndNodeId = node2.Id
//
// neo4jConnection.Get(rel)
func (neo4j *Neo4j) Create(obj Batcher) error {
_, err := neo4j.NewBatch().Create(obj).Execute()
return err
}
// Delete is the basic Delete method for all types
// It accepts only Batcher Interface
func (neo4j *Neo4j) Delete(obj Batcher) error {
_, err := neo4j.NewBatch().Delete(obj).Execute()
return err
}
// Update is the basic Update method for all types
// It accepts only Batcher Interface
func (neo4j *Neo4j) Update(obj Batcher) error {
_, err := neo4j.NewBatch().Update(obj).Execute()
return err
}