Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update neo.py with basicAuthentification #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions neonx/neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ 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.

: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::
Expand Down Expand Up @@ -164,36 +164,53 @@ 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 <http://bit.ly/1fo5324>`_.
: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()


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 \
<http://networkx.github.io/documentation/latest/\
reference/classes.digraph.html>`_.
"""

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),
Expand All @@ -202,7 +219,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()
Expand Down