Skip to content

Commit d024672

Browse files
authored
fix: remove decorator from get_key_in_revision and return raw response (#21)
* fix(client): update get_key_in_revision to return parsed YAML response * test: update test_get_key_in_revision to return raw string response * test: enhance get_key_in_revision tests for string, integer, and boolean responses
1 parent 931f108 commit d024672

File tree

4 files changed

+98
-23
lines changed

4 files changed

+98
-23
lines changed

rapyuta_io_sdk_v2/async_client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import httpx
1919
from munch import Munch
20+
from yaml import safe_load
2021

2122
from rapyuta_io_sdk_v2.config import Configuration
2223
from rapyuta_io_sdk_v2.utils import handle_and_munchify_response, handle_server_errors
@@ -1235,15 +1236,14 @@ async def commit_revision(
12351236
json=config_tree_revision,
12361237
)
12371238

1238-
@handle_and_munchify_response
12391239
async def get_key_in_revision(
12401240
self,
12411241
tree_name: str,
12421242
revision_id: str,
12431243
key: str,
12441244
project_guid: str = None,
12451245
**kwargs,
1246-
) -> Munch:
1246+
):
12471247
"""Get a key in a revision.
12481248
12491249
Args:
@@ -1256,10 +1256,14 @@ async def get_key_in_revision(
12561256
Munch: Key details as a Munch object.
12571257
"""
12581258

1259-
return await self.c.get(
1259+
result = await self.c.get(
12601260
url=f"{self.v2api_host}/v2/configtrees/{tree_name}/revisions/{revision_id}/{key}/",
12611261
headers=self.config.get_headers(project_guid=project_guid, **kwargs),
12621262
)
1263+
# The data received from the API is always in string format. To use
1264+
# appropriate data-type in Python (as well in exports), we are
1265+
# passing it through YAML parser.
1266+
return safe_load(result.text)
12631267

12641268
@handle_and_munchify_response
12651269
async def put_key_in_revision(

rapyuta_io_sdk_v2/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import httpx
1919
from munch import Munch
20+
from yaml import safe_load
2021

2122
from rapyuta_io_sdk_v2.config import Configuration
2223
from rapyuta_io_sdk_v2.utils import handle_and_munchify_response, handle_server_errors
@@ -1218,15 +1219,14 @@ def commit_revision(
12181219
json=config_tree_revision,
12191220
)
12201221

1221-
@handle_and_munchify_response
12221222
def get_key_in_revision(
12231223
self,
12241224
tree_name: str,
12251225
revision_id: str,
12261226
key: str,
12271227
project_guid: str = None,
12281228
**kwargs,
1229-
) -> Munch:
1229+
):
12301230
"""Get a key in a revision.
12311231
12321232
Args:
@@ -1239,10 +1239,14 @@ def get_key_in_revision(
12391239
Munch: Key details as a Munch object.
12401240
"""
12411241

1242-
return self.c.get(
1243-
url=f"{self.v2api_host}/v2/configtrees/{tree_name}/revisions/{revision_id}/{key}/",
1242+
result = self.c.get(
1243+
url=f"{self.v2api_host}/v2/configtrees/{tree_name}/revisions/{revision_id}/{key}",
12441244
headers=self.config.get_headers(project_guid=project_guid, **kwargs),
12451245
)
1246+
# The data received from the API is always in string format. To use
1247+
# appropriate data-type in Python (as well in exports), we are
1248+
# passing it through YAML parser.
1249+
return safe_load(result.text)
12461250

12471251
@handle_and_munchify_response
12481252
def put_key_in_revision(

tests/async_tests/test_configtree_async.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,27 +276,57 @@ async def test_commit_revision_success(client, mocker: AsyncMock): # noqa: F811
276276

277277

278278
@pytest.mark.asyncio
279-
async def test_get_key_in_revision(client, mocker: AsyncMock): # noqa: F811
279+
async def test_get_key_in_revision_str(client, mocker: AsyncMock): # noqa: F811
280280
# Mock the httpx.AsyncClient.get method
281281
mock_get = mocker.patch("httpx.AsyncClient.get")
282282

283283
# Set up the mock response
284-
mock_get.return_value = httpx.Response(
285-
status_code=200,
286-
json={
287-
"metadata": {"guid": "test_revision_guid", "name": "test_revision"},
288-
},
284+
mock_get.return_value = httpx.Response(status_code=200, text="test_value")
285+
286+
# Call the get_key_in_revision method
287+
response = await client.get_key_in_revision(
288+
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
289289
)
290290

291+
# Validate the response
292+
assert isinstance(response, str)
293+
assert response == "test_value"
294+
295+
296+
@pytest.mark.asyncio
297+
async def test_get_key_in_revision_int(client, mocker: AsyncMock): # noqa: F811
298+
# Mock the httpx.AsyncClient.get method
299+
mock_get = mocker.patch("httpx.AsyncClient.get")
300+
301+
# Set up the mock response
302+
mock_get.return_value = httpx.Response(status_code=200, text="999")
303+
291304
# Call the get_key_in_revision method
292305
response = await client.get_key_in_revision(
293306
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
294307
)
295308

296309
# Validate the response
297-
assert isinstance(response, Munch)
298-
assert response.metadata.guid == "test_revision_guid"
299-
assert response.metadata.name == "test_revision"
310+
assert isinstance(response, int)
311+
assert response == 999
312+
313+
314+
@pytest.mark.asyncio
315+
async def test_get_key_in_revision_bool(client, mocker: AsyncMock): # noqa: F811
316+
# Mock the httpx.AsyncClient.get method
317+
mock_get = mocker.patch("httpx.AsyncClient.get")
318+
319+
# Set up the mock response
320+
mock_get.return_value = httpx.Response(status_code=200, text="false")
321+
322+
# Call the get_key_in_revision method
323+
response = await client.get_key_in_revision(
324+
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
325+
)
326+
327+
# Validate the response
328+
assert isinstance(response, bool)
329+
assert not response
300330

301331

302332
@pytest.mark.asyncio

tests/sync_tests/test_configtree.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,14 @@ def test_commit_revision_success(client, mocker: MockFixture): # noqa: F811
258258
assert response.metadata.name == "test_revision"
259259

260260

261-
def test_get_key_in_revision(client, mocker: MockFixture): # noqa: F811
261+
def test_get_key_in_revision_str(client, mocker: MockFixture): # noqa: F811
262262
# Mock the httpx.Client.get method
263263
mock_get = mocker.patch("httpx.Client.get")
264264

265265
# Set up the mock response
266266
mock_get.return_value = httpx.Response(
267267
status_code=200,
268-
json={
269-
"metadata": {"guid": "test_revision_guid", "name": "test_revision"},
270-
},
268+
text="test_value",
271269
)
272270

273271
# Call the get_key_in_revision method
@@ -276,9 +274,48 @@ def test_get_key_in_revision(client, mocker: MockFixture): # noqa: F811
276274
)
277275

278276
# Validate the response
279-
assert isinstance(response, Munch)
280-
assert response.metadata.guid == "test_revision_guid"
281-
assert response.metadata.name == "test_revision"
277+
assert isinstance(response, str)
278+
assert response == "test_value"
279+
280+
281+
def test_get_key_in_revision_int(client, mocker: MockFixture): # noqa: F811
282+
# Mock the httpx.Client.get method
283+
mock_get = mocker.patch("httpx.Client.get")
284+
285+
# Set up the mock response
286+
mock_get.return_value = httpx.Response(
287+
status_code=200,
288+
text="1500",
289+
)
290+
291+
# Call the get_key_in_revision method
292+
response = client.get_key_in_revision(
293+
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
294+
)
295+
296+
# Validate the response
297+
assert isinstance(response, int)
298+
assert response == 1500
299+
300+
301+
def test_get_key_in_revision_bool(client, mocker: MockFixture): # noqa: F811
302+
# Mock the httpx.Client.get method
303+
mock_get = mocker.patch("httpx.Client.get")
304+
305+
# Set up the mock response
306+
mock_get.return_value = httpx.Response(
307+
status_code=200,
308+
text="true",
309+
)
310+
311+
# Call the get_key_in_revision method
312+
response = client.get_key_in_revision(
313+
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
314+
)
315+
316+
# Validate the response
317+
assert isinstance(response, bool)
318+
assert response
282319

283320

284321
def test_put_key_in_revision_success(client, mocker: MockFixture): # noqa: F811

0 commit comments

Comments
 (0)