Skip to content

Commit 81f3687

Browse files
committed
Consolidate WKVS calls
1 parent 1617642 commit 81f3687

File tree

3 files changed

+30
-44
lines changed

3 files changed

+30
-44
lines changed

src/ghga_connector/config.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,47 +115,45 @@ async def set_runtime_config(client: httpx.AsyncClient):
115115
- wps_api_url
116116
- dcs_api_url
117117
- ucs_api_url
118+
119+
Raises:
120+
WellKnownValueNotFound: If one of the well-known values is not found in the
121+
response from the WKVS.
122+
ConnectionFailedError: If the request fails due to a timeout/connection problem
123+
RequestFailedError: If the request fails for any other reason
118124
"""
119-
ghga_pubkey = await _get_wkvs_value(client, value_name="crypt4gh_public_key")
120-
wps_api_url = (await _get_wkvs_value(client, value_name="wps_api_url")).rstrip("/")
121-
dcs_api_url = (await _get_wkvs_value(client, value_name="dcs_api_url")).rstrip("/")
122-
ucs_api_url = (await _get_wkvs_value(client, value_name="ucs_api_url")).rstrip("/")
125+
values = await _get_wkvs_values(client)
126+
for value_name in [
127+
"crypt4gh_public_key",
128+
"wps_api_url",
129+
"dcs_api_url",
130+
"ucs_api_url",
131+
]:
132+
if value_name not in values:
133+
raise exceptions.WellKnownValueNotFound(value_name=value_name)
123134

124135
async with (
125-
set_context_var(ghga_pubkey_var, ghga_pubkey),
126-
set_context_var(wps_api_url_var, wps_api_url),
127-
set_context_var(dcs_api_url_var, dcs_api_url),
128-
set_context_var(ucs_api_url_var, ucs_api_url),
136+
set_context_var(ghga_pubkey_var, values["crypt4gh_public_key"]),
137+
set_context_var(wps_api_url_var, values["wps_api_url"].rstrip("/")),
138+
set_context_var(dcs_api_url_var, values["dcs_api_url"].rstrip("/")),
139+
set_context_var(ucs_api_url_var, values["ucs_api_url"].rstrip("/")),
129140
):
130141
yield
131142

132143

133-
async def _get_wkvs_value(client: httpx.AsyncClient, *, value_name: str) -> Any:
134-
"""Retrieve a value from the well-known-value-service.
135-
136-
Args:
137-
value_name (str): the name of the value to be retrieved
144+
async def _get_wkvs_values(client: httpx.AsyncClient) -> dict[str, Any]:
145+
"""Retrieve a value from the well-known-value-service using the supplied client.
138146
139147
Raises:
140-
WellKnownValueNotFound: when a 404 response is received from the WKVS
141-
KeyError: when a successful response is received but doesn't contain the expected value
148+
ConnectionFailedError: If the request fails due to a timeout/connection problem
149+
RequestFailedError: If the request fails for any other reason
142150
"""
143-
url = f"{CONFIG.wkvs_api_url}/values/{value_name}"
151+
url = f"{CONFIG.wkvs_api_url}/values/"
144152

145153
try:
146-
response = await client.get(url) # verify is True by default
154+
response = await client.get(url)
147155
except httpx.RequestError as request_error:
148156
exceptions.raise_if_connection_failed(request_error=request_error, url=url)
149157
raise exceptions.RequestFailedError(url=url) from request_error
150158

151-
if response.status_code == 404:
152-
raise exceptions.WellKnownValueNotFound(value_name=value_name)
153-
154-
try:
155-
value = response.json()[value_name]
156-
except KeyError as err:
157-
raise KeyError(
158-
"Response from well-known-value-service did not include expected field"
159-
+ f" '{value_name}'"
160-
) from err
161-
return value
159+
return response.json()

tests/fixtures/mock_api/app.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,9 @@ async def create_work_order_token(package_id: str, file_id: str):
426426
)
427427

428428

429-
@mock_external_app.get("/values/{value_name}")
430-
async def mock_wkvs(value_name: str):
431-
"""Mock the WKVS /values/value_name endpoint"""
429+
@mock_external_app.get("/values/")
430+
async def mock_wkvs():
431+
"""Mock the WKVS /values endpoint"""
432432
api_url = "http://127.0.0.1"
433433
values: dict[str, str] = {
434434
"crypt4gh_public_key": "qx5g31H7rdsq7sgkew9ElkLIXvBje4RxDVcAHcJD8XY=",
@@ -437,15 +437,7 @@ async def mock_wkvs(value_name: str):
437437
"ucs_api_url": api_url,
438438
}
439439

440-
if value_name not in values:
441-
raise HttpException(
442-
status_code=404,
443-
exception_id="valueNotConfigured",
444-
description=f"The value {value_name} is not configured.",
445-
data={"value_name": value_name},
446-
)
447-
448-
return JSONResponse(status_code=200, content={value_name: values[value_name]})
440+
return JSONResponse(status_code=200, content=values)
449441

450442

451443
config = ApiConfigBase()

tests/unit/test_api_calls.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
from ghga_connector import exceptions
3131
from ghga_connector.config import (
32-
_get_wkvs_value,
3332
get_dcs_api_url,
3433
get_ghga_pubkey,
3534
get_ucs_api_url,
@@ -325,9 +324,6 @@ async def test_set_runtime_config(mock_external_calls): # noqa: F811
325324
get_wps_api_url,
326325
]
327326
async with async_client() as client:
328-
with pytest.raises(exceptions.WellKnownValueNotFound):
329-
_ = await _get_wkvs_value(client, value_name="bogus")
330-
331327
# Verify that all the context vars are empty before calling config setup
332328
for func in ctx_var_getter_fns:
333329
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)