Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for String Batch Operation #121

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]