summaryrefslogtreecommitdiff
path: root/src-py/neo4j_util.py
diff options
context:
space:
mode:
authorLV-426 <lv-426@taproot.org.il>2014-09-23 16:28:16 +0300
committerLV-426 <lv-426@taproot.org.il>2014-09-23 16:28:16 +0300
commit68b2334cd8484ac72814060ced5703a2f94b57d7 (patch)
tree7c224efbebe812e8c2595643d1f1db583694a058 /src-py/neo4j_util.py
parent0ee6fc14b843f8701d1a82001dd438da87c9426e (diff)
initial implementation
Diffstat (limited to 'src-py/neo4j_util.py')
-rw-r--r--src-py/neo4j_util.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/src-py/neo4j_util.py b/src-py/neo4j_util.py
new file mode 100644
index 00000000..009fbe5f
--- /dev/null
+++ b/src-py/neo4j_util.py
@@ -0,0 +1,49 @@
+"""
+ Utility code in speaking the neo4j REST api
+"""
+
+import json
+import urllib2
+
+def post_neo4j(url, data):
+ """
+ return dict translation of the json string returned by neo4j, raise Exception if the 'errors' key is not empty
+ """
+ ret = post(url, data)
+ ret_data = json.load(ret)
+
+ if ret_data['errors']:
+ raise Exception('neo4j exception: ' + str(ret_data['errors']))
+
+ return ret_data
+
+def post(url, data):
+ assert(isinstance(data, dict)) # make sure we're not handed json strings
+
+ post_data_json = json.dumps(data)
+
+ req = urllib2.Request(url)
+ req.add_header('User-Agent', 'rhizi-server/0.1')
+ req.add_header('Accept', 'application/json; charset=UTF-8')
+ req.add_header('Content-Type', 'application/json')
+
+ try:
+ ret = urllib2.urlopen(req, post_data_json)
+ except urllib2.HTTPError as e:
+ raise Exception('post request failed: code: {0}, reason: {1}'.format(e.code, e.reason))
+
+ return ret
+
+def statement_to_REST_form(query, parameters={}):
+ """
+ turn cypher query to neo4j json API format
+ """
+ assert isinstance(query, str)
+ assert isinstance(parameters, dict)
+
+ return {'statement' : query, 'parameters': parameters}
+
+def statement_set_to_REST_form(statement_set):
+ assert isinstance(statement_set, list)
+
+ return {'statements': statement_set}