diff --git a/src/uk/ac/ebi/vfb/neo4j/neo4j_tools.py b/src/uk/ac/ebi/vfb/neo4j/neo4j_tools.py index 6362a4a3..818eadc7 100644 --- a/src/uk/ac/ebi/vfb/neo4j/neo4j_tools.py +++ b/src/uk/ac/ebi/vfb/neo4j/neo4j_tools.py @@ -74,46 +74,46 @@ def __init__(self, base_uri, usr, pwd): if not self.test_connection(): raise Exception("Failed to connect to Neo4j.") -def commit_list(self, statements, return_graphs=False, max_retries=3): - """Commit a list of statements to Neo4j DB via REST API. - Prints requests status and warnings if any problems with commit. - - statements: list of Cypher statements as strings - - return_graphs: optionally specify graphs to be returned in JSON results. - Errors prompt warnings, not exceptions, and cause return = False. - Returns results list of results or False if any errors are encountered.""" - - cstatements = [] - if return_graphs: - for s in statements: - cstatements.append({'statement': s, "resultDataContents": ["row", "graph"]}) - else: - for s in statements: - cstatements.append({'statement': s}) # Rows and columns are returned by default. - payload = {'statements': cstatements} - headers = self.headers - - attempt = 0 - while attempt < max_retries: - try: - response = requests.post( - url=self.base_uri + self.commit, - auth=(self.usr, self.pwd), - data=json.dumps(payload), - headers=headers - ) - if self.rest_return_check(response): - return response.json()['results'] - else: - return False - except requests.exceptions.ConnectionError as e: - attempt += 1 - print(f"ConnectionError on attempt {attempt}: {e}") - if attempt == max_retries: - print("Max retries reached. Operation failed.") - return False - wait_time = 2 ** attempt # Exponential backoff - print(f"Retrying in {wait_time} seconds...") - time.sleep(wait_time) + def commit_list(self, statements, return_graphs=False, max_retries=3): + """Commit a list of statements to Neo4j DB via REST API. + Prints requests status and warnings if any problems with commit. + - statements: list of Cypher statements as strings + - return_graphs: optionally specify graphs to be returned in JSON results. + Errors prompt warnings, not exceptions, and cause return = False. + Returns results list of results or False if any errors are encountered.""" + + cstatements = [] + if return_graphs: + for s in statements: + cstatements.append({'statement': s, "resultDataContents": ["row", "graph"]}) + else: + for s in statements: + cstatements.append({'statement': s}) # Rows and columns are returned by default. + payload = {'statements': cstatements} + headers = self.headers + + attempt = 0 + while attempt < max_retries: + try: + response = requests.post( + url=self.base_uri + self.commit, + auth=(self.usr, self.pwd), + data=json.dumps(payload), + headers=headers + ) + if self.rest_return_check(response): + return response.json()['results'] + else: + return False + except requests.exceptions.ConnectionError as e: + attempt += 1 + print(f"ConnectionError on attempt {attempt}: {e}") + if attempt == max_retries: + print("Max retries reached. Operation failed.") + return False + wait_time = 2 ** attempt # Exponential backoff + print(f"Retrying in {wait_time} seconds...") + time.sleep(wait_time) def commit_list_in_chunks(self, statements, verbose=False, chunk_length=1000): """Commit a list of statements to neo4J DB via REST API, split into chunks.