Skip to content

Commit 6126f38

Browse files
authored
feat: add TM Segment Batch Operations support (#274)
1 parent 07787de commit 6126f38

4 files changed

Lines changed: 114 additions & 1 deletion

File tree

‎crowdin_api/api_resources/translation_memory/enums.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ class TranslationMemorySegmentRecordOperationPath(Enum):
1717
REMOVE = "/records/{recordId}"
1818

1919

20+
class TranslationMemorySegmentBatchOperation(Enum):
21+
ADD = "add"
22+
REPLACE = "replace"
23+
REMOVE = "remove"
24+
25+
26+
class TranslationMemorySegmentBatchOperationPath(Enum):
27+
ADD = "/-"
28+
ADD_RECORD = "/{segmentId}/records/-"
29+
REPLACE = "/{segmentId}/records/{recordId}/text"
30+
REMOVE = "/{segmentId}"
31+
REMOVE_RECORD = "/{segmentId}/records/{recordId}"
32+
33+
2034
class ListTmsOrderBy(Enum):
2135
ID = "id"
2236
NAME = "name"

‎crowdin_api/api_resources/translation_memory/resource.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from crowdin_api.api_resources.enums import ExportFormat
55
from crowdin_api.api_resources.translation_memory.types import (
66
TranslationMemoryPatchRequest,
7+
TranslationMemorySegmentBatchOperationAdd,
8+
TranslationMemorySegmentBatchOperationRemove,
9+
TranslationMemorySegmentBatchOperationReplace,
710
TranslationMemorySegmentRecord,
811
TranslationMemorySegmentRecordOperationAdd,
912
TranslationMemorySegmentRecordOperationReplace,
@@ -207,6 +210,29 @@ def edit_tm_segment(
207210
request_data=data,
208211
)
209212

213+
def tm_segment_batch_operations(
214+
self,
215+
tmId: int,
216+
data: Iterable[
217+
Union[
218+
TranslationMemorySegmentBatchOperationAdd,
219+
TranslationMemorySegmentBatchOperationReplace,
220+
TranslationMemorySegmentBatchOperationRemove,
221+
]
222+
],
223+
):
224+
"""
225+
TM Segment Batch Operations.
226+
227+
Link to documentation:
228+
https://developer.crowdin.com/api/v2/#operation/api.tms.segments.patchBatch
229+
"""
230+
return self.requester.request(
231+
method="patch",
232+
path=self.get_tm_segments_path(tmId=tmId),
233+
request_data=data,
234+
)
235+
210236
# Export
211237
def get_tm_export_path(self, tmId: int, exportId: Optional[str] = None):
212238
if exportId is not None:

‎crowdin_api/api_resources/translation_memory/tests/test_translation_memory_resources.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
ListTmSegmentsOrderBy,
77
ListTmsOrderBy,
88
TranslationMemoryPatchPath,
9+
TranslationMemorySegmentBatchOperation,
10+
TranslationMemorySegmentBatchOperationPath,
911
TranslationMemorySegmentRecordOperation,
1012
TranslationMemorySegmentRecordOperationPath,
1113
)
@@ -275,6 +277,54 @@ def test_edit_tm_segment(
275277
],
276278
)
277279

280+
@mock.patch("crowdin_api.requester.APIRequester.request")
281+
def test_tm_segment_batch_operations(self, m_request, base_absolut_url):
282+
m_request.return_value = "response"
283+
284+
data = [
285+
{
286+
"op": TranslationMemorySegmentBatchOperation.ADD,
287+
"path": TranslationMemorySegmentBatchOperationPath.ADD,
288+
"value": {
289+
"records": [
290+
{
291+
"languageId": "uk",
292+
"text": "Перекладений текст",
293+
}
294+
]
295+
},
296+
},
297+
{
298+
"op": TranslationMemorySegmentBatchOperation.ADD,
299+
"path": TranslationMemorySegmentBatchOperationPath.ADD_RECORD,
300+
"value": {
301+
"languageId": "it",
302+
"text": "Ciao, mondo!",
303+
},
304+
},
305+
{
306+
"op": TranslationMemorySegmentBatchOperation.REMOVE,
307+
"path": TranslationMemorySegmentBatchOperationPath.REMOVE,
308+
},
309+
{
310+
"op": TranslationMemorySegmentBatchOperation.REMOVE,
311+
"path": TranslationMemorySegmentBatchOperationPath.REMOVE_RECORD,
312+
},
313+
{
314+
"op": TranslationMemorySegmentBatchOperation.REPLACE,
315+
"path": TranslationMemorySegmentBatchOperationPath.REPLACE,
316+
"value": "Testo tradotto",
317+
},
318+
]
319+
320+
resource = self.get_resource(base_absolut_url)
321+
assert resource.tm_segment_batch_operations(tmId=1, data=data) == "response"
322+
m_request.assert_called_once_with(
323+
method="patch",
324+
path=resource.get_tm_segments_path(tmId=1),
325+
request_data=data,
326+
)
327+
278328
# Export
279329
@pytest.mark.parametrize(
280330
"in_params, path",

‎crowdin_api/api_resources/translation_memory/types.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import Any
1+
from typing import Any, Iterable, Union
22

33
from crowdin_api.api_resources.enums import PatchOperation
44
from crowdin_api.api_resources.translation_memory.enums import (
55
TranslationMemoryPatchPath,
6+
TranslationMemorySegmentBatchOperation,
7+
TranslationMemorySegmentBatchOperationPath,
68
TranslationMemorySegmentRecordOperation,
79
TranslationMemorySegmentRecordOperationPath,
810
)
@@ -20,6 +22,10 @@ class TranslationMemorySegmentRecord(TypedDict):
2022
text: str
2123

2224

25+
class TranslationMemorySegment(TypedDict):
26+
records: Iterable[TranslationMemorySegmentRecord]
27+
28+
2329
class TranslationMemorySegmentRecordOperationAdd(TypedDict):
2430
op: TranslationMemorySegmentRecordOperation
2531
path: TranslationMemorySegmentRecordOperationPath
@@ -35,3 +41,20 @@ class TranslationMemorySegmentRecordOperationReplace(TypedDict):
3541
class TranslationMemorySegmentRecordOperationRemove(TypedDict):
3642
op: TranslationMemorySegmentRecordOperation
3743
path: TranslationMemorySegmentRecordOperationPath
44+
45+
46+
class TranslationMemorySegmentBatchOperationAdd(TypedDict):
47+
op: TranslationMemorySegmentBatchOperation
48+
path: TranslationMemorySegmentBatchOperationPath
49+
value: Union[TranslationMemorySegment, TranslationMemorySegmentRecord]
50+
51+
52+
class TranslationMemorySegmentBatchOperationReplace(TypedDict):
53+
op: TranslationMemorySegmentBatchOperation
54+
path: TranslationMemorySegmentBatchOperationPath
55+
value: str
56+
57+
58+
class TranslationMemorySegmentBatchOperationRemove(TypedDict):
59+
op: TranslationMemorySegmentBatchOperation
60+
path: TranslationMemorySegmentBatchOperationPath

0 commit comments

Comments
 (0)