Skip to content

Commit

Permalink
add star/unstar collections (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 authored Jan 30, 2025
1 parent def92b0 commit 78603ff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
6 changes: 6 additions & 0 deletions datapi/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ def retrieve(
"""
return self.submit(collection_id, **request).download(target)

def star_collection(self, collection_id: str) -> list[str]:
return self._profile_api.star_collection(collection_id)

def submit(self, collection_id: str, **request: Any) -> datapi.Remote:
"""Submit a request.
Expand Down Expand Up @@ -406,3 +409,6 @@ def submit_and_wait_on_results(
datapi.Results
"""
return self._retrieve_api.submit(collection_id, **request).make_results()

def unstar_collection(self, collection_id: str) -> None:
return self._profile_api.unstar_collection(collection_id)
2 changes: 1 addition & 1 deletion datapi/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _json_dict(self) -> dict[str, Any]:
return json_dict

@property
def _json_list(self) -> list[dict[str, Any]]:
def _json_list(self) -> list[Any]:
json_list = self.json
assert isinstance(json_list, list)
return json_list
Expand Down
10 changes: 10 additions & 0 deletions datapi/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,13 @@ def accepted_licences(self, **params: Any) -> dict[str, Any]:
def check_authentication(self) -> dict[str, Any]:
url = f"{self.url}/account/verification/pat"
return self._get_api_response("post", url)._json_dict

def star_collection(self, collection_id: str) -> list[str]:
url = f"{self.url}/account/starred"
return self._get_api_response(
"post", url, json={"uid": collection_id}, log_messages=False
)._json_list

def unstar_collection(self, collection_id: str) -> None:
url = f"{self.url}/account/starred/{collection_id}"
self._get_api_response("delete", url, log_messages=False)
31 changes: 22 additions & 9 deletions tests/integration_test_50_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,36 @@
from datapi import ApiClient, config


@pytest.mark.parametrize("scope", [None, "all", "dataset", "portal"])
def test_api_client_accept_licence(
scope: Literal["all", "dataset", "portal"] | None,
) -> None:
@pytest.fixture
def api_client() -> ApiClient:
try:
# Can not use anonymous user
config.get_config("key")
except Exception:
pytest.skip("The API key is missing")
return ApiClient(maximum_tries=0)

client = ApiClient(maximum_tries=0)
licence = client.get_licences(scope=scope)[0]

@pytest.mark.parametrize("scope", [None, "all", "dataset", "portal"])
def test_profile_accept_licence(
api_client: ApiClient,
scope: Literal["all", "dataset", "portal"] | None,
) -> None:
licence = api_client.get_licences(scope=scope)[0]
licence_id = licence["id"]
licence_revision = licence["revision"]

expected = {"id": licence_id, "revision": licence_revision}
actual = client.accept_licence(licence_id, licence_revision)
actual = api_client.accept_licence(licence_id, licence_revision)
assert expected == actual

assert any(
licence["id"] == licence_id and licence["revision"] == licence_revision
for licence in client.get_accepted_licences(scope=scope)
for licence in api_client.get_accepted_licences(scope=scope)
)


def test_api_client_check_authentication(
def test_profile_check_authentication(
api_root_url: str, api_anon_client: ApiClient
) -> None:
assert api_anon_client.check_authentication() == {
Expand All @@ -46,3 +50,12 @@ def test_api_client_check_authentication(
bad_client = ApiClient(key="foo", url=api_root_url)
with pytest.raises(HTTPError, match="401 Client Error"):
bad_client.check_authentication()


def test_profile_star_collection(api_client: ApiClient) -> None:
starred = api_client.star_collection("test-adaptor-dummy")
assert "test-adaptor-dummy" in starred

api_client.unstar_collection("test-adaptor-dummy")
with pytest.raises(HTTPError, match="404 Client Error"):
api_client.unstar_collection("test-adaptor-dummy")

0 comments on commit 78603ff

Please sign in to comment.