Skip to content

Commit 6415375

Browse files
feat: use the SDK to create tag to organization
1 parent ed4d29b commit 6415375

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/kili/adapters/kili_api_gateway/tag/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Mixin extending Kili API Gateway class with Tags related operations."""
22

3-
from typing import Dict, List
3+
from typing import Dict, List, Optional
44

55
from kili.adapters.kili_api_gateway.base import BaseOperationMixin
66
from kili.adapters.kili_api_gateway.helpers.queries import fragment_builder
@@ -10,6 +10,7 @@
1010

1111
from .operations import (
1212
GQL_CHECK_TAG,
13+
GQL_CREATE_TAG,
1314
GQL_DELETE_TAG,
1415
GQL_UNCHECK_TAG,
1516
GQL_UPDATE_TAG,
@@ -67,3 +68,9 @@ def delete_tag(self, tag_id: TagId) -> bool:
6768
variables = {"tagId": tag_id}
6869
result = self.graphql_client.execute(GQL_DELETE_TAG, variables)
6970
return result["data"]
71+
72+
def create_tag(self, label: str, color: Optional[str] = None) -> Dict:
73+
"""Send a GraphQL request calling createTag resolver."""
74+
variables = {"data": {"label": label, "color": color}}
75+
result = self.graphql_client.execute(GQL_CREATE_TAG, variables)
76+
return result["createTag"]

src/kili/adapters/kili_api_gateway/tag/operations.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
}
3434
"""
3535

36+
GQL_CREATE_TAG = """
37+
mutation createTag($data: TagData!) {
38+
createTag(data: $data) {
39+
id
40+
color
41+
label
42+
}
43+
}
44+
"""
45+
3646

3747
def get_list_tags_by_org_query(fragment: str) -> str:
3848
"""Return the GraphQL query to list tags by organization."""

src/kili/presentation/client/tag.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,19 @@ def delete_tag(self, tag_name: Optional[str] = None, tag_id: Optional[str] = Non
176176
raise ValueError("Either `tag_name` or `tag_id` must be provided.")
177177
tag_id = tag_use_cases.get_tag_ids_from_labels(labels=(tag_name,))[0]
178178
return tag_use_cases.delete_tag(tag_id=TagId(tag_id))
179+
180+
def create_tag(self, name: str, color: Optional[str] = None) -> Dict[Literal["id"], str]:
181+
"""Create a tag.
182+
183+
This operation is organization-wide.
184+
The tag will be proposed for projects of the organization.
185+
186+
Args:
187+
name: Name of the tag to create.
188+
color: Color of the tag to create. If not providen a default color will be used.
189+
190+
Returns:
191+
The id of the created tag.
192+
"""
193+
tag_use_cases = TagUseCases(self.kili_api_gateway)
194+
return tag_use_cases.create_tag(name, color)

src/kili/use_cases/tag/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ def delete_tag(self, tag_id: TagId) -> bool:
127127
Whether the tag was successfully removed.
128128
"""
129129
return self._kili_api_gateway.delete_tag(tag_id=tag_id)
130+
131+
def create_tag(self, name: str, color: Optional[str]) -> Dict:
132+
"""Create a tag."""
133+
return self._kili_api_gateway.create_tag(label=name, color=color)

0 commit comments

Comments
 (0)