From a4260988d6f7339508b7f139d7c6fd742abb38f5 Mon Sep 17 00:00:00 2001 From: sevaader Date: Mon, 22 Feb 2016 15:01:45 +0100 Subject: [PATCH 1/2] Update neo4.py with basicAuthentification Just adding basicAuthentification (login, pwd) to connect neo4j server. --- neonx/neo.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/neonx/neo.py b/neonx/neo.py index a850fa8..5a6c8ad 100644 --- a/neonx/neo.py +++ b/neonx/neo.py @@ -117,7 +117,7 @@ def check_exception(result): raise e -def get_server_urls(server_url): +def get_server_urls(server_url, **kwargs): """connects to the server with a GET request and returns its answer (e.g. a number of URLs of REST endpoints, the server version etc.) as a dictionary. @@ -125,13 +125,13 @@ def get_server_urls(server_url): :param server_url: the URL of the Neo4j server :rtype: a dictionary of parameters of the Neo4j server """ - result = requests.get(server_url) + result = requests.get(server_url, **kwargs) check_exception(result) return result.json() def write_to_neo(server_url, graph, edge_rel_name, label=None, - encoder=None): + encoder=None, server_login=None, server_pwd=None): """Write the `graph` as Geoff string. The edges between the nodes have relationship name `edge_rel_name`. The code below shows a simple example:: @@ -164,17 +164,25 @@ def write_to_neo(server_url, graph, edge_rel_name, label=None, :param optional label: It will add this label to the node. \ See `here `_. :param optional encoder: JSONEncoder object. Defaults to JSONEncoder. + :param server_login: login if server's authentification required + :param server_pwd: password if server's authentification required :rtype: A list of Neo4j created resources. """ if encoder is None: encoder = json.JSONEncoder() - - all_server_urls = get_server_urls(server_url) + + if all((server_login, server_pwd)): + from requests.auth import HTTPBasicAuth + basicAuth = HTTPBasicAuth(server_login, server_pwd) + else: + basicAuth = None + + all_server_urls = get_server_urls(server_url, auth=basicAuth) batch_url = all_server_urls['batch'] data = generate_data(graph, edge_rel_name, label, encoder) - result = requests.post(batch_url, data=data, headers=HEADERS) + result = requests.post(batch_url, data=data, headers=HEADERS, auth=basicAuth) check_exception(result) return result.json() @@ -182,18 +190,26 @@ def write_to_neo(server_url, graph, edge_rel_name, label=None, LABEL_QRY = """MATCH (a:{0})-[r]->(b:{1}) RETURN ID(a), r, ID(b);""" -def get_neo_graph(server_url, label): +def get_neo_graph(server_url, label, server_login=None, server_pwd=None): """Return a graph of all nodes with a given Neo4j label and edges between the same nodes. :param server_url: Server URL for the Neo4j server. :param label: The label to retrieve the nodes for. + :param server_login: login if server's authentification required + :param server_pwd: password if server's authentification required :rtype: A `Digraph \ `_. """ - all_server_urls = get_server_urls(server_url) + if all((server_login, server_pwd)): + from requests.auth import HTTPBasicAuth + basicAuth = HTTPBasicAuth(server_login, server_pwd) + else: + basicAuth = None + + all_server_urls = get_server_urls(server_url, auth=basicAuth) batch_url = all_server_urls['batch'] data = [{"method": "GET", "to": '/label/{0}/nodes'.format(label), @@ -202,7 +218,8 @@ def get_neo_graph(server_url, label): LABEL_QRY.format(label, label), "params": {}}}, ] - result = requests.post(batch_url, data=json.dumps(data), headers=HEADERS) + result = requests.post(batch_url, data=json.dumps(data), + headers=HEADERS, auth=basicAuth) check_exception(result) node_data, edge_date = result.json() @@ -220,3 +237,4 @@ def get_neo_graph(server_url, label): graph.add_edge(from_node_id, to_node_id, **properties) return graph + From bfe0e46017aabc7496c5097027e70a0357d535ca Mon Sep 17 00:00:00 2001 From: sevaader Date: Mon, 22 Feb 2016 17:06:35 +0100 Subject: [PATCH 2/2] Update neo.py --- neonx/neo.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/neonx/neo.py b/neonx/neo.py index 5a6c8ad..bae9d99 100644 --- a/neonx/neo.py +++ b/neonx/neo.py @@ -171,18 +171,19 @@ def write_to_neo(server_url, graph, edge_rel_name, label=None, if encoder is None: encoder = json.JSONEncoder() - + if all((server_login, server_pwd)): from requests.auth import HTTPBasicAuth basicAuth = HTTPBasicAuth(server_login, server_pwd) else: basicAuth = None - + all_server_urls = get_server_urls(server_url, auth=basicAuth) batch_url = all_server_urls['batch'] data = generate_data(graph, edge_rel_name, label, encoder) - result = requests.post(batch_url, data=data, headers=HEADERS, auth=basicAuth) + result = requests.post(batch_url, data=data, headers=HEADERS, + auth=basicAuth) check_exception(result) return result.json() @@ -208,7 +209,7 @@ def get_neo_graph(server_url, label, server_login=None, server_pwd=None): basicAuth = HTTPBasicAuth(server_login, server_pwd) else: basicAuth = None - + all_server_urls = get_server_urls(server_url, auth=basicAuth) batch_url = all_server_urls['batch'] @@ -237,4 +238,3 @@ def get_neo_graph(server_url, label, server_login=None, server_pwd=None): graph.add_edge(from_node_id, to_node_id, **properties) return graph -