Skip to content

Commit

Permalink
Merge branch 'master' into PY-398
Browse files Browse the repository at this point in the history
  • Loading branch information
saurbhc authored Nov 14, 2023
2 parents 38decdc + 0efb835 commit c01ce1e
Show file tree
Hide file tree
Showing 41 changed files with 1,211 additions and 323 deletions.
9 changes: 8 additions & 1 deletion darwin/future/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
from requests.adapters import HTTPAdapter, Retry

from darwin.future.core.types.common import JSONType, QueryString
from darwin.future.exceptions import NotFound, Unauthorized, UnprocessibleEntity
from darwin.future.exceptions import (
BadRequest,
NotFound,
Unauthorized,
UnprocessibleEntity,
)


class TeamsConfig(BaseModel):
Expand Down Expand Up @@ -235,6 +240,8 @@ def raise_for_darwin_exception(response: requests.Response) -> None:
"""
if response.status_code == 200:
return
if response.status_code == 400:
raise BadRequest(response)
if response.status_code == 401:
raise Unauthorized(response)
if response.status_code == 404:
Expand Down
31 changes: 17 additions & 14 deletions darwin/future/core/items/archive_items.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from __future__ import annotations

from typing import Dict, List
from uuid import UUID

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import JSONType
from darwin.future.data_objects.typing import UnknownType


def archive_list_of_items(
api_client: ClientCore,
client: ClientCore,
team_slug: str,
dataset_id: int,
item_ids: List[UUID],
dataset_ids: int | List[int],
filters: Dict[str, UnknownType] = {},
) -> JSONType:
"""
Expand All @@ -19,26 +19,29 @@ def archive_list_of_items(
Parameters
----------
client: Client
The client to use for the request
The client to use for the request.
team_slug: str
The slug of the team containing the items
dataset_id: int
The ID of the dataset containing the items
item_ids: List[UUID]
The IDs of the items to be archived
The slug of the team containing the items.
dataset_ids: int | List[int]
The ID(s) of the dataset(s) containing the items.
filters: Dict[str, UnknownType]
Dataset filter parameters
Filter parameters.
Returns
-------
JSONType
The response data.
"""
assert (
filters
), "No parameters provided, please provide at least one non-dataset id filter"
payload = {
"filters": {
"dataset_ids": [dataset_id],
"item_ids": [str(item_id) for item_id in item_ids],
"dataset_ids": dataset_ids
if isinstance(dataset_ids, list)
else [dataset_ids],
**filters,
}
}

return api_client.post(f"/v2/teams/{team_slug}/items/archive", data=payload)
return client.post(f"/v2/teams/{team_slug}/items/archive", data=payload)
32 changes: 17 additions & 15 deletions darwin/future/core/items/delete_items.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from __future__ import annotations

from typing import Dict, List
from uuid import UUID

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import JSONType
from darwin.future.data_objects.typing import UnknownType


def delete_list_of_items(
api_client: ClientCore,
client: ClientCore,
team_slug: str,
dataset_id: int,
item_ids: List[UUID],
dataset_ids: int | List[int],
filters: Dict[str, UnknownType] = {},
) -> JSONType:
"""
Expand All @@ -19,26 +19,28 @@ def delete_list_of_items(
Parameters
----------
client: Client
The client to use for the request
The client to use for the request.
team_slug: str
The slug of the team containing the items
dataset_id: int
The ID of the dataset containing the items
item_ids: List[UUID]
The IDs of the items to be deleted
The slug of the team containing the items.
dataset_ids: int | List[int]
The ID(s) of the dataset(s) containing the items.
filters: Dict[str, UnknownType]
Dataset filter parameters
Filter parameters
Returns
-------
JSONType
The response data.
"""
assert (
filters
), "No parameters provided, please provide at least one non-dataset id filter"
payload = {
"filters": {
"dataset_ids": [dataset_id],
"item_ids": [str(item_id) for item_id in item_ids],
"dataset_ids": dataset_ids
if isinstance(dataset_ids, list)
else [dataset_ids],
**filters,
}
}

return api_client.delete(f"/v2/teams/{team_slug}/items", data=payload)
return client.delete(f"/v2/teams/{team_slug}/items", data=payload)
26 changes: 17 additions & 9 deletions darwin/future/core/items/get.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import List, Tuple, Union
from __future__ import annotations

from typing import List, Literal, Tuple, Union
from uuid import UUID

from pydantic import ValidationError, parse_obj_as

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import QueryString
from darwin.future.data_objects.item import Folder, Item
from darwin.future.data_objects.item import Folder, ItemCore


def get_item_ids(
Expand Down Expand Up @@ -86,7 +88,7 @@ def get_item(
team_slug: str,
item_id: Union[UUID, str],
params: QueryString = QueryString({}),
) -> Item:
) -> ItemCore:
"""
Returns an item
Expand All @@ -106,14 +108,15 @@ def get_item(
"""
response = api_client.get(f"/v2/teams/{team_slug}/items/{item_id}", params)
assert isinstance(response, dict)
return parse_obj_as(Item, response)
return parse_obj_as(ItemCore, response)


def list_items(
api_client: ClientCore,
team_slug: str,
params: QueryString,
) -> Tuple[List[Item], List[ValidationError]]:
dataset_ids: int | list[int] | Literal["all"],
params: QueryString = QueryString({}),
) -> Tuple[List[ItemCore], List[ValidationError]]:
"""
Returns a list of items for the dataset
Expand All @@ -133,15 +136,20 @@ def list_items(
List[ValidationError]
A list of ValidationError on failed objects
"""
assert "dataset_ids" in params.value, "dataset_ids must be provided"
dataset_ids = (
dataset_ids
if isinstance(dataset_ids, list) or dataset_ids == "all"
else [dataset_ids]
)
params = params + QueryString({"dataset_ids": dataset_ids})
response = api_client.get(f"/v2/teams/{team_slug}/items", params)
assert isinstance(response, dict)
items: List[Item] = []
items: List[ItemCore] = []
exceptions: List[ValidationError] = []
for item in response["items"]:
assert isinstance(item, dict)
try:
items.append(parse_obj_as(Item, item))
items.append(parse_obj_as(ItemCore, item))
except ValidationError as e:
exceptions.append(e)
return items, exceptions
Expand Down
55 changes: 32 additions & 23 deletions darwin/future/core/items/move_items.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
from typing import List
from __future__ import annotations

from typing import Dict, List
from uuid import UUID

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import JSONType
from darwin.future.data_objects.typing import UnknownType


def move_items_to_stage(
api_client: ClientCore,
client: ClientCore,
team_slug: str,
workflow_id: UUID,
dataset_id: int,
dataset_ids: int | List[int],
stage_id: UUID,
item_ids: List[UUID],
filters: Dict[str, UnknownType] = {},
) -> JSONType:
"""
Moves a list of items to a stage
Parameters
----------
client: Client
The client to use for the request
The client to use for the request.
team_slug: str
The slug of the team to move items for
dataset_id: str
The id or slug of the dataset to move items for
stage_id: str
The id or slug of the stage to move items to
item_ids: List[UUID]
A list of item ids to move to the stage
The slug of the team to move items for.
workflow_id: UUID
The id of the workflow to move items for.
dataset_ids: int | List[int]
The ID(s) of the dataset(s) containing the items.
stage_id: UUID
The id of the workflow to move items for.
filters: Dict[str, UnknownType]
Filter parameters.
Returns
-------
JSONType
The response data.
"""

return api_client.post(
f"/v2/teams/{team_slug}/items/stage",
{
"filters": {
"dataset_ids": [dataset_id],
"item_ids": [str(id) for id in item_ids],
},
"stage_id": str(stage_id),
"workflow_id": str(workflow_id),
assert (
filters
), "No parameters provided, please provide at least one non-dataset id filter"
payload = {
"filters": {
"dataset_ids": dataset_ids
if isinstance(dataset_ids, list)
else [dataset_ids],
**filters,
},
)
"stage_id": str(stage_id),
"workflow_id": str(workflow_id),
}

return client.post(f"/v2/teams/{team_slug}/items/stage", data=payload)
50 changes: 50 additions & 0 deletions darwin/future/core/items/move_items_to_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import annotations

from typing import Dict, List

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import JSONType
from darwin.future.data_objects.typing import UnknownType


def move_list_of_items_to_folder(
client: ClientCore,
team_slug: str,
dataset_ids: int | List[int],
path: str,
filters: Dict[str, UnknownType] = {},
) -> JSONType:
"""
Move specified items to a folder
Parameters
----------
client: Client
The client to use for the request.
team_slug: str
The slug of the team containing the items.
dataset_ids: int | List[int]
The ID(s) of the dataset(s) containing the items.
path: str
The path to the folder to move the items to.
filters: Dict[str, UnknownType]
Filter parameters.
Returns
-------
JSONType
The response data.
"""
assert (
filters
), "No parameters provided, please provide at least one non-dataset id filter"
payload = {
"filters": {
"dataset_ids": dataset_ids
if isinstance(dataset_ids, list)
else [dataset_ids],
**filters,
}
}

return client.post(f"/v2/teams/{team_slug}/items/path", data=payload)
Loading

0 comments on commit c01ce1e

Please sign in to comment.