Skip to content
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
14 changes: 14 additions & 0 deletions crowdin_api/api_resources/translation_memory/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ class TranslationMemorySegmentRecordOperationPath(Enum):
REMOVE = "/records/{recordId}"


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


class TranslationMemorySegmentBatchOperationPath(Enum):
ADD = "/-"
ADD_RECORD = "/{segmentId}/records/-"
REPLACE = "/{segmentId}/records/{recordId}/text"
REMOVE = "/{segmentId}"
REMOVE_RECORD = "/{segmentId}/records/{recordId}"


class ListTmsOrderBy(Enum):
ID = "id"
NAME = "name"
Expand Down
26 changes: 26 additions & 0 deletions crowdin_api/api_resources/translation_memory/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from crowdin_api.api_resources.enums import ExportFormat
from crowdin_api.api_resources.translation_memory.types import (
TranslationMemoryPatchRequest,
TranslationMemorySegmentBatchOperationAdd,
TranslationMemorySegmentBatchOperationRemove,
TranslationMemorySegmentBatchOperationReplace,
TranslationMemorySegmentRecord,
TranslationMemorySegmentRecordOperationAdd,
TranslationMemorySegmentRecordOperationReplace,
Expand Down Expand Up @@ -207,6 +210,29 @@ def edit_tm_segment(
request_data=data,
)

def tm_segment_batch_operations(
self,
tmId: int,
data: Iterable[
Union[
TranslationMemorySegmentBatchOperationAdd,
TranslationMemorySegmentBatchOperationReplace,
TranslationMemorySegmentBatchOperationRemove,
]
],
):
"""
TM Segment Batch Operations.

Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.tms.segments.patchBatch
"""
return self.requester.request(
method="patch",
path=self.get_tm_segments_path(tmId=tmId),
request_data=data,
)

# Export
def get_tm_export_path(self, tmId: int, exportId: Optional[str] = None):
if exportId is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
ListTmSegmentsOrderBy,
ListTmsOrderBy,
TranslationMemoryPatchPath,
TranslationMemorySegmentBatchOperation,
TranslationMemorySegmentBatchOperationPath,
TranslationMemorySegmentRecordOperation,
TranslationMemorySegmentRecordOperationPath,
)
Expand Down Expand Up @@ -275,6 +277,54 @@ def test_edit_tm_segment(
],
)

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

data = [
{
"op": TranslationMemorySegmentBatchOperation.ADD,
"path": TranslationMemorySegmentBatchOperationPath.ADD,
"value": {
"records": [
{
"languageId": "uk",
"text": "Перекладений текст",
}
]
},
},
{
"op": TranslationMemorySegmentBatchOperation.ADD,
"path": TranslationMemorySegmentBatchOperationPath.ADD_RECORD,
"value": {
"languageId": "it",
"text": "Ciao, mondo!",
},
},
{
"op": TranslationMemorySegmentBatchOperation.REMOVE,
"path": TranslationMemorySegmentBatchOperationPath.REMOVE,
},
{
"op": TranslationMemorySegmentBatchOperation.REMOVE,
"path": TranslationMemorySegmentBatchOperationPath.REMOVE_RECORD,
},
{
"op": TranslationMemorySegmentBatchOperation.REPLACE,
"path": TranslationMemorySegmentBatchOperationPath.REPLACE,
"value": "Testo tradotto",
},
]

resource = self.get_resource(base_absolut_url)
assert resource.tm_segment_batch_operations(tmId=1, data=data) == "response"
m_request.assert_called_once_with(
method="patch",
path=resource.get_tm_segments_path(tmId=1),
request_data=data,
)

# Export
@pytest.mark.parametrize(
"in_params, path",
Expand Down
25 changes: 24 additions & 1 deletion crowdin_api/api_resources/translation_memory/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Any
from typing import Any, Iterable, Union

from crowdin_api.api_resources.enums import PatchOperation
from crowdin_api.api_resources.translation_memory.enums import (
TranslationMemoryPatchPath,
TranslationMemorySegmentBatchOperation,
TranslationMemorySegmentBatchOperationPath,
TranslationMemorySegmentRecordOperation,
TranslationMemorySegmentRecordOperationPath,
)
Expand All @@ -20,6 +22,10 @@ class TranslationMemorySegmentRecord(TypedDict):
text: str


class TranslationMemorySegment(TypedDict):
records: Iterable[TranslationMemorySegmentRecord]


class TranslationMemorySegmentRecordOperationAdd(TypedDict):
op: TranslationMemorySegmentRecordOperation
path: TranslationMemorySegmentRecordOperationPath
Expand All @@ -35,3 +41,20 @@ class TranslationMemorySegmentRecordOperationReplace(TypedDict):
class TranslationMemorySegmentRecordOperationRemove(TypedDict):
op: TranslationMemorySegmentRecordOperation
path: TranslationMemorySegmentRecordOperationPath


class TranslationMemorySegmentBatchOperationAdd(TypedDict):
op: TranslationMemorySegmentBatchOperation
path: TranslationMemorySegmentBatchOperationPath
value: Union[TranslationMemorySegment, TranslationMemorySegmentRecord]


class TranslationMemorySegmentBatchOperationReplace(TypedDict):
op: TranslationMemorySegmentBatchOperation
path: TranslationMemorySegmentBatchOperationPath
value: str


class TranslationMemorySegmentBatchOperationRemove(TypedDict):
op: TranslationMemorySegmentBatchOperation
path: TranslationMemorySegmentBatchOperationPath
Loading