Skip to content

Commit

Permalink
feat: Support for String Batch Operation (#121)
Browse files Browse the repository at this point in the history
Signed-off-by: OmAxiani0 <75031769+OmAximani0@users.noreply.github.com>
  • Loading branch information
OmAximani0 committed Oct 4, 2023
1 parent 635b41d commit 9d3f75d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
15 changes: 15 additions & 0 deletions crowdin_api/api_resources/source_strings/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ class SourceStringsPatchPath(Enum):
IS_HIDDEN = "/isHidden"
MAXLENGTH = "/maxLength"
LABEL_IDS = "/labelIds"


class StringBatchOperations(Enum):
REPLACE = "replace"
REMOVE = "remove"
ADD = "add"


class StringBatchOperationsPath(Enum):
IDENTIFIER = "/{stringId}/identifier"
TEXT = "/{stringId}/text"
CONTEXT = "/{stringId}/context"
IS_HIDDEN = "/{stringId}/isHidden"
MAX_LENGTH = "/{stringId}/maxLength"
LABEL_IDS = "/{stringId}/labelIds"
22 changes: 21 additions & 1 deletion crowdin_api/api_resources/source_strings/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from crowdin_api.api_resources.abstract.resources import BaseResource
from crowdin_api.api_resources.enums import DenormalizePlaceholders
from crowdin_api.api_resources.source_strings.enums import ScopeFilter
from crowdin_api.api_resources.source_strings.types import SourceStringsPatchRequest
from crowdin_api.api_resources.source_strings.types import (
SourceStringsPatchRequest,
StringBatchOperationPatchRequest,
)


class SourceStringsResource(BaseResource):
Expand Down Expand Up @@ -136,3 +139,20 @@ def edit_string(self, projectId: int, stringId: int, data: Iterable[SourceString
path=self.get_source_strings_path(projectId=projectId, stringId=stringId),
request_data=data,
)

def string_batch_operation(
self,
projectId: int,
data: Iterable[StringBatchOperationPatchRequest],
):
"""
String Batch Operations.
Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.projects.strings.batchPatch
"""
return self.requester.request(
method="patch",
path=self.get_source_strings_path(projectId=projectId),
request_data=data,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import pytest
from crowdin_api.api_resources.enums import DenormalizePlaceholders, PatchOperation
from crowdin_api.api_resources.source_strings.enums import ScopeFilter, SourceStringsPatchPath
from crowdin_api.api_resources.source_strings.enums import (
ScopeFilter,
SourceStringsPatchPath,
StringBatchOperationsPath,
StringBatchOperations,
)
from crowdin_api.api_resources.source_strings.resource import SourceStringsResource
from crowdin_api.requester import APIRequester

Expand Down Expand Up @@ -170,3 +175,27 @@ def test_edit_string(self, m_request, base_absolut_url):
request_data=data,
path=resource.get_source_strings_path(projectId=1, stringId=2),
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_string_batch_operation(self, m_request, base_absolut_url):
m_request.return_value = "response"

data = [
{
"op": StringBatchOperations.REPLACE,
"path": StringBatchOperationsPath.IS_HIDDEN,
"value": True,
},
{
"op": StringBatchOperations.REMOVE,
"path": StringBatchOperationsPath.CONTEXT,
"value": "some value",
},
]
resource = self.get_resource(base_absolut_url)
assert resource.string_batch_operation(1, data=data) == "response"
m_request.assert_called_once_with(
method="patch",
path=resource.get_source_strings_path(1),
request_data=data,
)
14 changes: 12 additions & 2 deletions crowdin_api/api_resources/source_strings/types.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from typing import Any
from typing import Any, Union

from crowdin_api.api_resources.enums import PatchOperation
from crowdin_api.api_resources.source_strings.enums import SourceStringsPatchPath
from crowdin_api.api_resources.source_strings.enums import (
SourceStringsPatchPath,
StringBatchOperations,
StringBatchOperationsPath,
)
from crowdin_api.typing import TypedDict


class SourceStringsPatchRequest(TypedDict):
value: Any
op: PatchOperation
path: SourceStringsPatchPath


class StringBatchOperationPatchRequest(TypedDict):
op: StringBatchOperations
path: StringBatchOperationsPath
value: Union[str, dict, int, bool]

0 comments on commit 9d3f75d

Please sign in to comment.