diff --git a/darwin/client.py b/darwin/client.py index 173ab06fe..1c9ce360d 100644 --- a/darwin/client.py +++ b/darwin/client.py @@ -29,6 +29,14 @@ Unauthorized, ValidationError, ) +from darwin.future.core.client import ClientCore, DarwinConfig +from darwin.future.core.properties import ( + get_team_full_properties as get_team_full_properties_future, +) +from darwin.future.core.properties import ( + get_team_properties as get_team_properties_future, +) +from darwin.future.data_objects.properties import FullProperty from darwin.item import DatasetItem from darwin.utils import ( get_response_content, @@ -1474,3 +1482,20 @@ def api_v2(self) -> BackendV2: if not team: raise ValueError("No team was found.") return BackendV2(self, team.slug) + + def get_team_properties( + self, team_slug: Optional[str] = None, include_property_values: bool = True + ) -> List[FullProperty]: + darwin_config = DarwinConfig.from_old(self.config) + future_client = ClientCore(darwin_config) + + if not include_property_values: + return get_team_properties_future( + client=future_client, + team_slug=team_slug or self.default_team, + ) + + return get_team_full_properties_future( + client=future_client, + team_slug=team_slug or self.default_team, + ) diff --git a/darwin/future/core/client.py b/darwin/future/core/client.py index b80b81031..3506c9193 100644 --- a/darwin/future/core/client.py +++ b/darwin/future/core/client.py @@ -9,6 +9,7 @@ from pydantic import BaseModel, root_validator, validator from requests.adapters import HTTPAdapter, Retry +from darwin.config import Config as OldConfig from darwin.future.core.types.common import JSONType, QueryString from darwin.future.exceptions import ( BadRequest, @@ -121,6 +122,25 @@ def from_api_key_with_defaults(api_key: str) -> DarwinConfig: datasets_dir=DarwinConfig._default_config_path(), ) + @staticmethod + def from_old(old_config: OldConfig) -> DarwinConfig: + teams = old_config.get("teams") + if not teams: + raise ValueError("No teams found in the old config") + + default_team = old_config.get("global/default_team") + if not default_team: + default_team = list(teams.keys())[0] + + return DarwinConfig( + api_key=teams[default_team]["api_key"], + api_endpoint=old_config.get("global/api_endpoint"), + base_url=old_config.get("global/base_url"), + default_team=default_team, + teams=teams, + datasets_dir=teams[default_team]["datasets_dir"], + ) + class Config: validate_assignment = True diff --git a/darwin/future/tests/core/test_client.py b/darwin/future/tests/core/test_client.py index 6b5195698..ffd524231 100644 --- a/darwin/future/tests/core/test_client.py +++ b/darwin/future/tests/core/test_client.py @@ -5,10 +5,12 @@ from pydantic import ValidationError from requests import HTTPError -from darwin.future.core.client import ClientCore, DarwinConfig +from darwin.config import Config as OldConfig +from darwin.future.core.client import ClientCore, DarwinConfig, TeamsConfig from darwin.future.exceptions import DarwinException, NotFound, Unauthorized from darwin.future.tests.core.fixtures import * from darwin.future.tests.fixtures import * +from tests.fixtures import * def test_create_config(base_config: DarwinConfig) -> None: @@ -140,3 +142,37 @@ def test_client_raises_generic(base_client: ClientCore) -> None: rsps.add(responses.PATCH, endpoint, json={"test": "test"}, status=status_code) with pytest.raises(HTTPError): base_client.patch("test_endpoint", {"test": "test"}) + + +@pytest.mark.usefixtures("file_read_write_test") +def test_config_from_old_error( + base_config: DarwinConfig, darwin_config_path: Path +) -> None: + old_config = OldConfig(darwin_config_path) + with pytest.raises(ValueError) as excinfo: + base_config.from_old(old_config) + (msg,) = excinfo.value.args + assert msg == "No teams found in the old config" + + +@pytest.mark.usefixtures("file_read_write_test") +def test_config_from_old( + base_config: DarwinConfig, darwin_config_path: Path, darwin_datasets_path: Path +) -> None: + old_config = OldConfig(darwin_config_path) + team_slug = "test-team" + old_config.put(["global", "api_endpoint"], "http://localhost/api") + old_config.put(["global", "base_url"], "http://localhost") + old_config.put(["teams", team_slug, "api_key"], "mock_api_key") + old_config.put(["teams", team_slug, "datasets_dir"], str(darwin_datasets_path)) + darwin_config = base_config.from_old(old_config) + + assert darwin_config.api_key == "mock_api_key" + assert darwin_config.base_url == "http://localhost/" + assert darwin_config.api_endpoint == "http://localhost/api" + assert darwin_config.default_team == team_slug + assert darwin_config.teams == { + team_slug: TeamsConfig( + api_key="mock_api_key", datasets_dir=darwin_datasets_path + ) + } diff --git a/tests/darwin/client_test.py b/tests/darwin/client_test.py index deb8d3e34..c314dac22 100644 --- a/tests/darwin/client_test.py +++ b/tests/darwin/client_test.py @@ -414,3 +414,53 @@ def test__get_item_count_should_tolerate_missing_members() -> None: ) == 2 ) + + +@pytest.mark.usefixtures("file_read_write_test") +class TestGetTeamProperties: + @responses.activate + def test_get_team_properties(self, darwin_client: Client) -> None: + + responses.add( + responses.GET, + "http://localhost/apiv2/teams/v7-darwin-json-v1/properties?include_values=true", + json={ + "properties": [ + { + "annotation_class_id": 2558, + "description": "test question", + "id": "d7368686-d087-4d92-bfd9-8ae776c3ed3a", + "name": "property question", + "property_values": [ + { + "color": "rgba(143,255,0,1.0)", + "id": "3e40c575-41dc-43c9-87f9-7dc2b625650d", + "position": 1, + "type": "string", + "value": {"value": "answer 1"}, + }, + { + "color": "rgba(173,255,0,1.0)", + "id": "18ebaad0-c22a-49db-b6c5-1de0da986f4e", + "position": 2, + "type": "string", + "value": {"value": "answer 2"}, + }, + { + "color": "rgba(82,255,0,1.0)", + "id": "b67ae529-f612-4c9a-a175-5b98f1d81a6e", + "position": 3, + "type": "string", + "value": {"value": "answer 3"}, + }, + ], + "required": False, + "slug": "property-question", + "team_id": 128, + "type": "multi_select", + }, + ] + }, + status=200, + ) + assert len(darwin_client.get_team_properties()) == 1