Skip to content

Commit

Permalink
Adds dedicated kwargs for searching and ordering (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
djperrefort authored Dec 4, 2024
1 parent e4ace26 commit 1665a08
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions keystone_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ def _retrieve_factory(self, endpoint: Endpoint) -> callable:
def retrieve_record(
pk: int | None = None,
filters: dict | None = None,
search: str | None = None,
order: str | None = None,
timeout=DEFAULT_TIMEOUT
) -> Union[None, dict, list[dict]]:
"""Retrieve one or more API records.
Expand All @@ -317,6 +319,8 @@ def retrieve_record(
Args:
pk: Optional primary key to fetch a specific record.
filters: Optional query parameters to include in the request.
search: Optionally search records for the given string.
order: Optional order returned values by the given parameter.
timeout: Seconds before the request times out.
Returns:
Expand All @@ -325,6 +329,11 @@ def retrieve_record(

url = endpoint.join_url(self.url, pk)

for param_name, value in zip(('_search', '_order'), (search, order)):
if value is not None:
filters = filters or {}
filters[param_name] = value

try:
response = self.http_get(url, params=filters, timeout=timeout)
response.raise_for_status()
Expand Down
2 changes: 1 addition & 1 deletion tests/client/test_KeystoneClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_retrieve_by_pk(self) -> None:
self.assertIsNotNone(retrieved_cluster)
self.assertEqual(retrieved_cluster['id'], pk)

def test_retrieve_by_filters(self) -> None:
def test_retrieve_with_filters(self) -> None:
"""Test the filtering of returned records via search params."""

retrieved_clusters = self.client.retrieve_cluster(filters={"name": "Test-Cluster"})
Expand Down

0 comments on commit 1665a08

Please sign in to comment.