Skip to content

Commit

Permalink
not found error handling in api/go-cam/id endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sierra-moxon committed Nov 22, 2024
1 parent e111882 commit fd40c64
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 13 additions & 7 deletions app/routers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,21 @@ async def get_model_details_by_model_id_json(
:param id: A GO-CAM identifier (e.g. 581e072c00000820, 581e072c00000295, 5900dc7400000968)
:return: model details based on a GO-CAM model ID in JSON format from the S3 bucket.
"""
stripped_ids = []
if id.startswith("gomodel:"):
replaced_id = id.replace("gomodel:", "")
model_id = id.replace("gomodel:", "")
stripped_ids.append(model_id)
else:
replaced_id = id

path_to_s3 = "https://go-public.s3.amazonaws.com/files/go-cam/%s.json" % replaced_id
response = requests.get(path_to_s3, timeout=30, headers={"User-Agent": USER_AGENT})
response.raise_for_status() # This will raise an HTTPError if the HTTP request returned an unsuccessful status code
return response.json()
stripped_ids.append(id)
for stripped_id in stripped_ids:
path_to_s3 = "https://go-public.s3.amazonaws.com/files/go-cam/%s.json" % stripped_id
response = requests.get(path_to_s3, timeout=30, headers={"User-Agent": USER_AGENT})
if response.status_code == 403 or response.status_code == 404:
raise DataNotFoundException("GO-CAM model not found.")
else:
response = requests.get(path_to_s3, timeout=30, headers={"User-Agent": USER_AGENT})
response.raise_for_status() # This will raise an HTTPError if the HTTP request returned an unsuccessful status code
return response.json()


@router.get("/api/models/{id}", tags=["models"], description="Returns model details based on a GO-CAM model ID.")
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/test_models_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
test_client = TestClient(app)
gene_ids = ["ZFIN:ZDB-GENE-980526-388", "ZFIN:ZDB-GENE-990415-8", "MGI:3588192"]
go_cam_ids = ["59a6110e00000067", "SYNGO_369", "581e072c00000820", "gomodel:59a6110e00000067", "gomodel:SYNGO_369"]

go_cam_not_found_ids = ["NGO_369", "581e072c000008", "gomodel:59a6110e000000",]

class TestApp(unittest.TestCase):
"""Test the models endpoints."""
Expand Down Expand Up @@ -150,6 +150,17 @@ def test_get_model_details_by_model_id_json(self):
self.assertGreater(len(response.json().get("individuals")), 0)
self.assertGreater(len(response.json().get("facts")), 0)


def test_get_model_details_by_model_id_not_found_json(self):
"""
Test the endpoint to retrieve model details by model ID in JSON format from the S3 bucket, check for success.
:return: None
"""
for id in go_cam_not_found_ids:
response = test_client.get(f"/api/go-cam/{id}")
self.assertEqual(response.status_code, 404)

def test_get_model_details_by_model_id_json_failure(self):
"""
Test the endpoint to retrieve model details by model ID that does not exist, check for failure.
Expand Down

0 comments on commit fd40c64

Please sign in to comment.