Skip to content

Commit

Permalink
NN-527: Update version() for improved error handling
Browse files Browse the repository at this point in the history
* Implement timeout for URL requests
* Raise ValueError if no match is found
  • Loading branch information
dyusuf committed Jul 24, 2024
1 parent d536a6b commit 4a79dc2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions backend/src/pathway_data/kegg.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ def version():
Check the latest build of KEGG
Returns:
Latest KEGG Version (float)
Latest KEGG Version (str) in the format "%b %d"
"""
content = requests.get("https://rest.kegg.jp/info/kegg").text
match = re.search(r"Release \d+\.\d+\+/\d{2}-\d{2}, (\w{3} \d{2})", content)
release_number = match.group(1)
return release_number
try:
# Assuming the URL and parameters are correctly set up to fetch the version
url = "https://rest.kegg.jp/info/kegg"
content = requests.get(url, timeout=5).text
# Example regex, adjust based on actual content format
match = re.search(r"Release \d+\.\d+\+/\d{2}-\d{2}, (\w{3} \d{2})", content)

if match:
release_number = match.group(1)
return release_number # Returns the matched date string in "%b %d" format
else:
raise ValueError(f"Could not find the release number of KEGG in\n{content}.")
except requests.exceptions.Timeout:
raise TimeoutError(f"The request to {url} timed out. Please try again later.")


def get_pathways(organism_id):
Expand Down

0 comments on commit 4a79dc2

Please sign in to comment.