From 9564431d5adc5627b8f5cbebf6f9dc3441005778 Mon Sep 17 00:00:00 2001 From: "@baptiste33" Date: Tue, 5 Nov 2024 10:28:26 +0100 Subject: [PATCH] feat: use the SDK to create tag to organization --- .../adapters/kili_api_gateway/tag/__init__.py | 9 ++++++++- .../adapters/kili_api_gateway/tag/operations.py | 10 ++++++++++ src/kili/presentation/client/tag.py | 16 ++++++++++++++++ src/kili/use_cases/tag/__init__.py | 4 ++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/kili/adapters/kili_api_gateway/tag/__init__.py b/src/kili/adapters/kili_api_gateway/tag/__init__.py index 3cf4f4c72..d285ad811 100644 --- a/src/kili/adapters/kili_api_gateway/tag/__init__.py +++ b/src/kili/adapters/kili_api_gateway/tag/__init__.py @@ -1,6 +1,6 @@ """Mixin extending Kili API Gateway class with Tags related operations.""" -from typing import Dict, List +from typing import Dict, List, Optional from kili.adapters.kili_api_gateway.base import BaseOperationMixin from kili.adapters.kili_api_gateway.helpers.queries import fragment_builder @@ -10,6 +10,7 @@ from .operations import ( GQL_CHECK_TAG, + GQL_CREATE_TAG, GQL_DELETE_TAG, GQL_UNCHECK_TAG, GQL_UPDATE_TAG, @@ -67,3 +68,9 @@ def delete_tag(self, tag_id: TagId) -> bool: variables = {"tagId": tag_id} result = self.graphql_client.execute(GQL_DELETE_TAG, variables) return result["data"] + + def create_tag(self, label: str, color: Optional[str] = None) -> Dict: + """Send a GraphQL request calling createTag resolver.""" + variables = {"label": label, "color": color} + result = self.graphql_client.execute(GQL_CREATE_TAG, variables) + return result["data"] diff --git a/src/kili/adapters/kili_api_gateway/tag/operations.py b/src/kili/adapters/kili_api_gateway/tag/operations.py index 5680be1f0..efc3fd036 100644 --- a/src/kili/adapters/kili_api_gateway/tag/operations.py +++ b/src/kili/adapters/kili_api_gateway/tag/operations.py @@ -33,6 +33,16 @@ } """ +GQL_CREATE_TAG = """ +mutation createTag($data: TagData!) { + createTag(data: $data) { + id + color + label + } +} +""" + def get_list_tags_by_org_query(fragment: str) -> str: """Return the GraphQL query to list tags by organization.""" diff --git a/src/kili/presentation/client/tag.py b/src/kili/presentation/client/tag.py index 84cf27ac3..82489aeb4 100644 --- a/src/kili/presentation/client/tag.py +++ b/src/kili/presentation/client/tag.py @@ -176,3 +176,19 @@ def delete_tag(self, tag_name: Optional[str] = None, tag_id: Optional[str] = Non raise ValueError("Either `tag_name` or `tag_id` must be provided.") tag_id = tag_use_cases.get_tag_ids_from_labels(labels=(tag_name,))[0] return tag_use_cases.delete_tag(tag_id=TagId(tag_id)) + + def create_tag(self, name: str, color: Optional[str]) -> Dict[Literal["id"], str]: + """Create a tag. + + This operation is organization-wide. + The tag will be proposed for projects of the organization. + + Args: + name: Name of the tag to create. + color: Color of the tag to create. If not providen a default color will be used. + + Returns: + The id of the created tag. + """ + tag_use_cases = TagUseCases(self.kili_api_gateway) + return tag_use_cases.create_tag(name, color) diff --git a/src/kili/use_cases/tag/__init__.py b/src/kili/use_cases/tag/__init__.py index 7630f7814..d87807bb4 100644 --- a/src/kili/use_cases/tag/__init__.py +++ b/src/kili/use_cases/tag/__init__.py @@ -127,3 +127,7 @@ def delete_tag(self, tag_id: TagId) -> bool: Whether the tag was successfully removed. """ return self._kili_api_gateway.delete_tag(tag_id=tag_id) + + def create_tag(self, name: str, color: Optional[str]) -> Dict: + """Create a tag.""" + return self._kili_api_gateway.create_tag(label=name, color=color)