Skip to content

Commit

Permalink
adding error checking for getting the nanopub user
Browse files Browse the repository at this point in the history
so the server doesn't crash on this in case that service is not available.
  • Loading branch information
micheldumontier committed Oct 10, 2024
1 parent a912b80 commit 4944f2b
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions backend/app/trapi/reasonerapi_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,33 @@ def get_metakg_from_nanopubs():
def get_np_users():
pubkeys = {}
headers = {"Accept": "application/json"}
res = requests.get(
f"{settings.NANOPUB_GRLC_URL}/get_all_users", headers=headers
).json()
for user in res["results"]["bindings"]:
# print(user)
# Remove bad ORCID URLs
if not user["user"]["value"].startswith("https://orcid.org/https://orcid.org/"):
if "name" not in user:
user["name"] = {"value": user["user"]["value"]}

pubkeys[user["pubkey"]["value"]] = user
# users_orcid[user['user']['value']] = user
try:
res = requests.get(
f"{settings.NANOPUB_GRLC_URL}/get_all_users", headers=headers
).json()
except Exception() as e:
print(f"error in getting np_users {e}")
return pubkeys

if "results" in res and "bindings" in res["results"]:
for user in res["results"]["bindings"]:
# print(user)
# Remove bad ORCID URLs
if not user["user"]["value"].startswith("https://orcid.org/https://orcid.org/"):
if "name" not in user:
user["name"] = {"value": user["user"]["value"]}

pubkeys[user["pubkey"]["value"]] = user
# users_orcid[user['user']['value']] = user
else:
if settings.DEV_MODE is True:
print(
f"Error in getting all users from {settings.NANOPUB_GRLC_URL}/get_all_users"
)
if settings.DEV_MODE is True:
print(
f"Found {len(pubkeys)} users in the nanopublication network"
)
return pubkeys


Expand All @@ -395,8 +410,9 @@ def reasonerapi_to_sparql(reasoner_query):
:param: reasoner_query Query from Reasoner API
:return: Results as ReasonerAPI object
"""
np_users = get_np_users()
# print(np_users)
np_users = {}
np_users = get_np_users() # this is just to (optionally) add the author to the edge

query_graph = reasoner_query["message"]["query_graph"]
query_options = {}
n_results = None
Expand Down

0 comments on commit 4944f2b

Please sign in to comment.