Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 78603ff

Browse files
authored
add star/unstar collections (#13)
1 parent def92b0 commit 78603ff

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

datapi/api_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ def retrieve(
373373
"""
374374
return self.submit(collection_id, **request).download(target)
375375

376+
def star_collection(self, collection_id: str) -> list[str]:
377+
return self._profile_api.star_collection(collection_id)
378+
376379
def submit(self, collection_id: str, **request: Any) -> datapi.Remote:
377380
"""Submit a request.
378381
@@ -406,3 +409,6 @@ def submit_and_wait_on_results(
406409
datapi.Results
407410
"""
408411
return self._retrieve_api.submit(collection_id, **request).make_results()
412+
413+
def unstar_collection(self, collection_id: str) -> None:
414+
return self._profile_api.unstar_collection(collection_id)

datapi/processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def _json_dict(self) -> dict[str, Any]:
208208
return json_dict
209209

210210
@property
211-
def _json_list(self) -> list[dict[str, Any]]:
211+
def _json_list(self) -> list[Any]:
212212
json_list = self.json
213213
assert isinstance(json_list, list)
214214
return json_list

datapi/profile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ def accepted_licences(self, **params: Any) -> dict[str, Any]:
7575
def check_authentication(self) -> dict[str, Any]:
7676
url = f"{self.url}/account/verification/pat"
7777
return self._get_api_response("post", url)._json_dict
78+
79+
def star_collection(self, collection_id: str) -> list[str]:
80+
url = f"{self.url}/account/starred"
81+
return self._get_api_response(
82+
"post", url, json={"uid": collection_id}, log_messages=False
83+
)._json_list
84+
85+
def unstar_collection(self, collection_id: str) -> None:
86+
url = f"{self.url}/account/starred/{collection_id}"
87+
self._get_api_response("delete", url, log_messages=False)

tests/integration_test_50_profile.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,36 @@
88
from datapi import ApiClient, config
99

1010

11-
@pytest.mark.parametrize("scope", [None, "all", "dataset", "portal"])
12-
def test_api_client_accept_licence(
13-
scope: Literal["all", "dataset", "portal"] | None,
14-
) -> None:
11+
@pytest.fixture
12+
def api_client() -> ApiClient:
1513
try:
1614
# Can not use anonymous user
1715
config.get_config("key")
1816
except Exception:
1917
pytest.skip("The API key is missing")
18+
return ApiClient(maximum_tries=0)
2019

21-
client = ApiClient(maximum_tries=0)
22-
licence = client.get_licences(scope=scope)[0]
20+
21+
@pytest.mark.parametrize("scope", [None, "all", "dataset", "portal"])
22+
def test_profile_accept_licence(
23+
api_client: ApiClient,
24+
scope: Literal["all", "dataset", "portal"] | None,
25+
) -> None:
26+
licence = api_client.get_licences(scope=scope)[0]
2327
licence_id = licence["id"]
2428
licence_revision = licence["revision"]
2529

2630
expected = {"id": licence_id, "revision": licence_revision}
27-
actual = client.accept_licence(licence_id, licence_revision)
31+
actual = api_client.accept_licence(licence_id, licence_revision)
2832
assert expected == actual
2933

3034
assert any(
3135
licence["id"] == licence_id and licence["revision"] == licence_revision
32-
for licence in client.get_accepted_licences(scope=scope)
36+
for licence in api_client.get_accepted_licences(scope=scope)
3337
)
3438

3539

36-
def test_api_client_check_authentication(
40+
def test_profile_check_authentication(
3741
api_root_url: str, api_anon_client: ApiClient
3842
) -> None:
3943
assert api_anon_client.check_authentication() == {
@@ -46,3 +50,12 @@ def test_api_client_check_authentication(
4650
bad_client = ApiClient(key="foo", url=api_root_url)
4751
with pytest.raises(HTTPError, match="401 Client Error"):
4852
bad_client.check_authentication()
53+
54+
55+
def test_profile_star_collection(api_client: ApiClient) -> None:
56+
starred = api_client.star_collection("test-adaptor-dummy")
57+
assert "test-adaptor-dummy" in starred
58+
59+
api_client.unstar_collection("test-adaptor-dummy")
60+
with pytest.raises(HTTPError, match="404 Client Error"):
61+
api_client.unstar_collection("test-adaptor-dummy")

0 commit comments

Comments
 (0)