-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_to_neo4j_db.py
42 lines (32 loc) · 1.09 KB
/
connect_to_neo4j_db.py
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
"""
Connect to Neo4j DB file.
This file contains connection logic to the Neo4j database.
Author: Petter Lovehagen
"""
import keyring
from neo4j import GraphDatabase
from neo4j.exceptions import AuthError, ServiceUnavailable
def connect_to_neo4j():
"""
Establishes a connection to the Neo4j database
Returns:
driver: A Neo4j driver object used to interact with the database.
"""
uri = keyring.get_password("neo4j", "uri")
username = keyring.get_password("neo4j", "username")
password = keyring.get_password("neo4j", "password")
print("Connecting to Neo4j database....")
try:
driver = GraphDatabase.driver(
uri,
auth=(username, password),
database = 'neo4j'
)
print(f"Connected to Neo4j database successfully! Driver: {driver}")
return driver
except AuthError as e:
print("Failed to authenticate with Neo4j: %s", e)
raise # Re-raise the exception after logging
except ServiceUnavailable as e:
print("Neo4j service is unavailable: %s", e)
raise