Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/icefabric/helpers/rise/rise.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,30 @@ async def make_get_req_to_rise(full_url: str):
the response is an error from RISE, the original code and message is
returned as well.
"""
import json

rise_response = {}
async with httpx.AsyncClient() as client:
try:
rise_response = {"status_code": 200, "detail": ""}
print(f"Making GET request to RISE (full URL): {full_url}")
resp = await client.get(full_url, headers=RISE_HEADERS, timeout=15)
resp.raise_for_status()
rise_response["detail"] = resp.json()
# Handle cases where API returns non-JSON response (e.g., HTML error page)
try:
rise_response["detail"] = resp.json()
except json.JSONDecodeError:
print("RISE API returned non-JSON response (likely API down)")
rise_response["status_code"] = 503
rise_response["detail"] = "RISE API returned non-JSON response (service unavailable)"
except httpx.TimeoutException as err:
print(f"RISE API timed out: {err}")
rise_response["status_code"] = 504
rise_response["detail"] = "RISE API request timed out"
except httpx.ReadTimeout as err:
print(f"RISE API read timed out: {err}")
rise_response["status_code"] = 504
rise_response["detail"] = "RISE API request timed out"
except httpx.HTTPStatusError as err:
print(f"RISE API returned an HTTP error: {err}")
rise_response["status_code"] = int(err.response.status_code)
Expand Down
15 changes: 10 additions & 5 deletions tests/app/test_rise_router.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json

import httpx
import pytest
from starlette.testclient import TestClient

from app.routers.rise_wrappers.router import EXT_RISE_BASE_URL, make_get_req_to_rise

Expand All @@ -11,18 +13,21 @@
bad_ids = ["0", "0", "0", "0"]

RISE_TIMEOUT_CODE = 504
RISE_UNAVAILABLE_CODE = 503


def _skip_if_rise_unavailable(response):
def _skip_if_rise_unavailable(response: httpx.Response) -> None:
"""Skip the test if RISE API is down or timing out."""
if response.status_code == RISE_TIMEOUT_CODE:
pytest.skip("RISE API is unavailable (timeout)")
if response.status_code == RISE_UNAVAILABLE_CODE:
pytest.skip("RISE API is unavailable (service unavailable)")


@pytest.mark.integration
@pytest.mark.asyncio
@pytest.mark.parametrize("resource_type,id", list(zip(resources, good_ids, strict=False)))
async def test_get_item_by_id_good(client, resource_type, id):
async def test_get_item_by_id_good(client: TestClient, resource_type: str, id: str) -> None:
"""Test all per-ID endpoints for IDs that exist"""
response = client.get(f"/v1/rise/{resource_type}/{id}")
_skip_if_rise_unavailable(response)
Expand All @@ -34,7 +39,7 @@ async def test_get_item_by_id_good(client, resource_type, id):
@pytest.mark.integration
@pytest.mark.asyncio
@pytest.mark.parametrize("resource_type,id", list(zip(resources, bad_ids, strict=False)))
async def test_get_item_by_id_bad(client, resource_type, id):
async def test_get_item_by_id_bad(client: TestClient, resource_type: str, id: str) -> None:
"""Test all per-ID endpoints for IDs that do not exist"""
response = client.get(f"/v1/rise/{resource_type}/{id}")
_skip_if_rise_unavailable(response)
Expand All @@ -46,7 +51,7 @@ async def test_get_item_by_id_bad(client, resource_type, id):
@pytest.mark.integration
@pytest.mark.asyncio
@pytest.mark.parametrize("resource_type", resources)
async def test_get_collection(client, resource_type):
async def test_get_collection(client: TestClient, resource_type: str) -> None:
"""Test every resource collection endpoint - all default parameters"""
# TODO - remove skip once the RISE api/result endpoint is no longer timing out
if resource_type == "result":
Expand All @@ -61,7 +66,7 @@ async def test_get_collection(client, resource_type):
@pytest.mark.integration
@pytest.mark.asyncio
@pytest.mark.parametrize("resource_type", resources)
async def test_get_collection_set_of_ids(client, resource_type):
async def test_get_collection_set_of_ids(client: TestClient, resource_type: str) -> None:
"""Test every resource collection endpoint with a set of three IDs"""
# TODO - remove skip once the RISE api/result endpoint is no longer timing out
if resource_type == "result":
Expand Down
Loading