Skip to content

Commit

Permalink
[PY-623] Get team properties backported to client (#749)
Browse files Browse the repository at this point in the history
* base annotation darwin

* fixes for annotations

* base str vs url

* tests for base annotation loading

* cleanup

* cleanup 2: electric boogaloo

* test bounding box auto-calculate

* typing

* cleanup

* property addition

* property metadata parsing

* cleanup validator

* test for properties base example

* linting

* Changes for list/get endpoint

* linting

* get_all_properties

* get properties and get by id

* linting

* base annotation darwin

* fixes for annotations

* base str vs url

* tests for base annotation loading

* cleanup

* cleanup 2: electric boogaloo

* test bounding box auto-calculate

* typing

* cleanup

* property metadata parsing

* cleanup validator

* test for properties base example

* Changes for list/get endpoint

* get_all_properties

* get properties and get by id

* linting

* linting

* QS helper and params checker on team_full_properties

* add client.get_team_properties

* add DarwinConfig.from_old method

* add tests for config conversion + get_team_properties

* Update client.py for property_value renaming

---------

Co-authored-by: Nathan Perkins <nathanjp91@gmail.com>
Co-authored-by: Nathan Perkins <nathan@v7labs.com>
  • Loading branch information
3 people committed Dec 15, 2023
1 parent 72080cb commit 7eac38c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
25 changes: 25 additions & 0 deletions darwin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
20 changes: 20 additions & 0 deletions darwin/future/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
38 changes: 37 additions & 1 deletion darwin/future/tests/core/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
)
}
50 changes: 50 additions & 0 deletions tests/darwin/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7eac38c

Please sign in to comment.