-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartcat.py
356 lines (281 loc) · 12.4 KB
/
smartcat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding: utf-8 -*-
import json
import requests
from abc import ABCMeta
class SmartCAT(object):
"""SmartCAT API
Provides functionality for SmartCAT resource management:
- project
- document
Manage Project Resource::
>>> from smartcat.api import SmartCAT
>>> api = SmartCAT('username', 'password', SmartCAT.SERVER_EUROPE)
>>> project_resource = api.project
<smartcat.api.Project>
>>> project_model = {
"name": "Sample Project",
"sourceLanguage": "en",
"targetLanguages": ["ru"],
"assignToVendor": False
}
>>> res = project_resource.create(data=project_model)
<Response [200]>
Manage Document Resource::
>>> from smartcat.api import SmartCAT
>>> api = SmartCAT('username', 'password', SmartCAT.SERVER_EUROPE)
>>> document_resource = api.document
<smartcat.api.Document>
>>> res = document_resource.request_export(document_ids=['project1_doc1', 'project1_doc2', 'project2_doc1'])
<Response [200]>
>>> res = document_resource.request_export(document_ids='project1_doc1')
<Response [200]>
"""
SERVER_USA = "https://us.smartcat.ai"
SERVER_EUROPE = "https://smartcat.ai"
def __init__(self, username, password, server_url=SERVER_EUROPE):
"""
Constructor
:param username: SmartCAT API username.
:param password: SmartCAT API password.
:param server_url (optional): The API server: SmartCAT.SERVER_EUROPE or SmartCAT.SERVER_USA
"""
self.username = username
self.password = password
self.server_url = server_url
#: :class:`Project <Project>`.
self._project = None
self._document = None
pass
@property
def project(self):
"""Returns instance of class:`Project <smartcat.api.Project>`.
:return: :class:`Project <smartcat.api.Project>` object
:rtype: smartcat.api.Project
"""
if self._project is not None:
return self._project
self._project = self._create_api_resource("Project")
return self._project
@property
def document(self):
"""Returns instance of `Document <smartcat.api.Document>`
:return: :class:`Document <smartcat.api.Document>` object
:rtype: smartcat.api.Document
"""
if self._document is not None:
return self._document
self._document = self._create_api_resource("Document")
return self._document
def _create_api_resource(self, resource):
"""Creates and rerurns API resource
:return: :class:`BaseResource <BaseResource>` object
:rtype: smartcat.BaseResource
"""
return globals()[resource](self.username, self.password, self.server_url)
class BaseResource(object):
__metaclass__ = ABCMeta
def __init__(self, username, password, server):
self.session = requests.Session()
self.session.auth = (username, password)
self.session.headers.update({"Accept": "application/json"})
self.server = server
def send_get_request(self, path, **kwargs):
url = self.server + path
return self.session.get(url, **kwargs)
def send_options_request(self, path, **kwargs):
url = self.server + path
return self.session.options(url, **kwargs)
def send_head_request(self, path, **kwargs):
url = self.server + path
return self.session.put(url, **kwargs)
def send_post_request(self, path, data=None, json=None, **kwargs):
url = self.server + path
return self.session.post(url, data=data, json=json, **kwargs)
def send_put_request(self, path, data=None, **kwargs):
url = self.server + path
return self.session.put(url, data=data, **kwargs)
def send_patch_request(self, path, data=None, **kwargs):
url = self.server + path
return self.session.patch(url, data=data, **kwargs)
def send_delete_request(self, path, **kwargs):
url = self.server + path
return self.session.delete(url, **kwargs)
class Project(BaseResource):
def create(self, data, files=None):
# type: (dict) -> requests.Response
"""Create a new project
:param data: The project information.
:type data: dict
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``)
for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional
headers to add for the file
"""
if files is None:
files = {}
files["model"] = (None, json.dumps(data), "application/json")
return self.send_post_request("/api/integration/v1/project/create", files=files)
def update(self, id, data):
"""Update project by id
:param id: The project identifier.
:param data: The project information.
:type data: dict
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_put_request("/api/integration/v1/project/%s" % id, json=data)
def delete(self, id):
"""Delete project
:param id: The project identifier.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_delete_request("/api/integration/v1/project/%s" % id)
def cancel(self, id):
"""Cancel the project
:param id: The project identifier.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_post_request(
"/api/integration/v1/project/cancel", params={"projectId": id}
)
def restore(self, id):
"""Restore the project
:param id: The project identifier.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_post_request(
"/api/integration/v1/project/restore", params={"projectId": id}
)
def get(self, id):
"""Get project
:param id: The project identifier.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_get_request("/api/integration/v1/project/%s" % id)
def completed_work_statistics(self, id):
"""Receiving statistics for the completed parts of the project.
:param id: The project identifier.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_get_request(
"/api/integration/v1/project/%s/completedWorkStatistics" % id
)
def get_all(self):
"""Adds document to project.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_get_request("/api/integration/v1/project/list")
def attach_document(self, id, files):
"""Adds document to project.
:param id: The project identifier.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``)
for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional
headers to add for the file
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
params = {"projectId": id}
return self.send_post_request(
"/api/integration/v1/project/document", files=files, params=params
)
def add_target_lang(self, id, lang):
"""Add a new target language to the project
:param id: The project identifier.
:param lang: Target language code.
:return: :class:`Response <Response>` object
:rtype:
"""
return self.send_post_request(
"/api/integration/v1/project/language",
params={"projectId": id, "targetLanguage": lang},
)
class Document(BaseResource):
def update(self, document_id, files):
"""Updates document
:param document_id: The document identifier.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``)
for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional
headers to add for the file
:return: :class:`Response <Response>` object
:rtype: requests.Response
todo:: implement updateDocumentModel
"""
return self.send_put_request(
"/api/integration/v1/document/update",
files=files,
params={"documentId": document_id},
)
def rename(self, id, name):
"""Renames document
:param id: The document identifier.
:param name: New name.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_put_request(
"/api/integration/v1/document/rename",
params={"documentId": id, "name": name},
)
def get_translation_status(self, id):
"""Receive the status of adding document translation.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_get_request(
"/api/integration/v1/document/translate/status", params={"documentId": id}
)
def translate(self, id, files):
"""Translate the selected document using the uploaded translation file.
note::Available only for updatable file formats (in actual practice,
these currently include resource files with unique resource IDs)
This assigns a task to be processed; the translation
job may not be finished at the time the request is completed.
:param id: The document identifier.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``)
for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional
headers to add for the file
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return self.send_put_request(
"/api/integration/v1/document/translate",
files=files,
params={"documentId": id},
)
def request_export(self, document_ids, target_type="target"):
"""Sends task to export transations
:param document_ids: The document identifier string or list of the identifier.
:param target_type (optional): The translation document type: xliff or target.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
if isinstance(document_ids, str):
document_ids = [document_ids]
params = {"documentIds": "\n".join(document_ids), "type": target_type}
return self.send_post_request(
"/api/integration/v1/document/export", params=params
)
def download_export_result(self, task_id):
"""Download the results of export
:param task_id: The export task identifier
"""
return self.send_get_request(
"/api/integration/v1/document/export/%s" % task_id, stream=True
)