Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/18-link-to-help-documentation-is-broken
Browse files Browse the repository at this point in the history
  • Loading branch information
bergsalex authored Sep 17, 2024
2 parents 372a903 + a19f8ce commit a1d79a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ RUN poetry install --only-root

WORKDIR /app/src

CMD ["poetry", "run", "gunicorn", "--limit-request-line", "8190", "--bind", "0.0.0.0:8000", "application:app"]
CMD ["poetry", "run", "gunicorn", "--timeout", "300", "--limit-request-line", "8190", "--bind", "0.0.0.0:8000", "application:app"]
41 changes: 22 additions & 19 deletions src/annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# END Upload Fix

import geneweaverdb
from time import sleep


NCBO_URL = 'http://data.bioontology.org'
Expand Down Expand Up @@ -132,15 +133,15 @@ def fetch_ncbo_annotations(text, ncboids):
## for-else construct: if the loop doesn't break (in our case this
## indicates success) then this statement is executed
else:
print('Failed to retrieve annotation data after three attempts')
print('Failed to retrieve NCBO annotation data after three attempts')
return []

return json.loads(res)


def get_ncbo_link(link):
"""
Since NCBO enjoys testing the sanity of it's users, (almost) every member
Since NCBO enjoys testing the sanity of its users, (almost) every member
of an object returned by the NCBO API is in the form of a link which then
requires further API calls to retrieve the data.
This function calls any API returned link and (hopefully) returns the
Expand All @@ -149,29 +150,31 @@ def get_ncbo_link(link):
:arg str: JSON-LD link
:ret dict: json object representing whatever data we retrieved
"""
headers = {
'Authorization': f'apikey token={API_KEY}'
}

opener = urllib.request.build_opener()
opener.addheaders = [('Authorization', 'apikey token=' + API_KEY)]

for _ in range(3):
response = None
for attempt in range(3):
try:
res = opener.open(link)
res = res.read()

except urllib.error.HTTPError as e:
print('Failed to retrieve annotation data from an NCBO link:')
print(e)
response = requests.get(link, headers=headers)
response.raise_for_status()
return response.json()
except requests.HTTPError as e:
print(f'HTTPError on attempt {attempt + 1}: {e}')
if response is not None:
print(f'Response content: {response.content}') # Log the response content
sleep(1)
continue
except Exception as e:
print(f'Unknown error on attempt {attempt + 1}: {e}')
print(traceback.format_exc()) # Print the full traceback for debugging
sleep(1)
continue

## Success
break

else:
print('Failed to retrieve data from an NCBO URL')
return None

return json.loads(res)


def parse_ncbo_annotations(annots):
"""
Expand Down Expand Up @@ -261,7 +264,7 @@ def fetch_monarch_annotations(text):
## for-else construct: if the loop doesn't break (in our case this
## indicates success) then this statement is executed
else:
print('Failed to retrieve annotation data after three attempts')
print('Failed to retrieve Monarch annotation data after three attempts')
return []

return json.loads(res)
Expand Down

0 comments on commit a1d79a7

Please sign in to comment.