forked from Smartling/api-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartlingApiExample.py
More file actions
169 lines (134 loc) · 6.11 KB
/
SmartlingApiExample.py
File metadata and controls
169 lines (134 loc) · 6.11 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
''' Copyright 2012 Smartling, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this work except in compliance with the License.
* You may obtain a copy of the License in the LICENSE file, or at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
'''
import os
import sys
lib_path = os.path.abspath('../')
sys.path.append(lib_path) # allow to import ../smartlingApiSdk/SmartlingFileApi
from smartlingApiSdk.SmartlingFileApi import SmartlingFileApiFactory, ProxySettings
from smartlingApiSdk.SmartlingDirective import SmartlingDirective
from smartlingApiSdk.UploadData import UploadData
class SmartlingApiExample:
MY_API_KEY = "YOUR_API_KEY" #should be changed with read values
MY_PROJECT_ID = "YOUR_PROJECT_ID" #should be changed with real values
def __init__(self, useSandbox, uploadData, locale, new_name):
self.getCredentials()
useProxy = False
if useProxy :
proxySettings = ProxySettings("login", "password", "proxy_host", "proxy_port")
else:
proxySettings = None
if useSandbox:
self.fapi = SmartlingFileApiFactory().getSmartlingTranslationApi(False, self.MY_API_KEY, self.MY_PROJECT_ID, proxySettings)
else:
self.fapi = SmartlingFileApiFactory().getSmartlingTranslationApiProd(self.MY_API_KEY, self.MY_PROJECT_ID, proxySettings)
self.uploadData = uploadData
self.locale = locale
self.new_name = new_name
def getCredentials(self):
""" get api key and project id from environment variables
to set environment variables use command : export SL_API_KEY=******* ; export SL_PROJECT_ID=****** """
self.MY_API_KEY = os.environ.get('SL_API_KEY', self.MY_API_KEY)
self.MY_PROJECT_ID = os.environ.get('SL_PROJECT_ID', self.MY_PROJECT_ID)
def printMarker(self, caption):
print "--" + caption + "-" * 40
def test_import(self, name_to_import):
""" this method tests `import` command """
self.printMarker("file upload")
#upload file first to be able upload it's translations later
resp, code = self.fapi.upload(self.uploadData)
print resp, code
self.printMarker("files list")
#list all files to ensure upload worked
resp, code = self.fapi.list()
print resp, code
self.printMarker("importing uploaded")
old_name = self.uploadData.name
#set correct uri/name for file to be imported
self.uploadData.uri = self.uploadData.name
self.uploadData.name = name_to_import
#import translations from file
resp, code = self.fapi.import_call(self.uploadData, self.locale, translationState="READY_FOR_PUBLISH")
print resp, code
self.uploadData.name = old_name
#perform `last_modified` command
self.printMarker("last modified")
resp, code = self.fapi.last_modified(self.uploadData.name)
print "resp.messages=", resp.messages
print "resp.code=", resp.code
print "resp.data.items="
for v in resp.data.items: print v
self.printMarker("delete from server goes here")
#delete test file imported in the beginning of test
resp, code = self.fapi.delete(self.uploadData.name)
print resp, code
def test(self):
""" simple illustration for set of API commands: upload, list, status, get, rename, delete """
self.printMarker("file upload")
resp, code = self.fapi.upload(self.uploadData)
print resp, code
self.printMarker("files list")
resp, code = self.fapi.list()
print resp, code
self.printMarker("file status")
resp, code = self.fapi.status(self.uploadData.name, self.locale)
print resp, code
self.printMarker("file from server goes here")
resp, code = self.fapi.get(self.uploadData.name, self.locale)
print resp, code
self.printMarker("renaming file")
resp, code = self.fapi.rename(self.uploadData.name, self.new_name)
print resp, code
self.printMarker("delete from server goes here")
resp, code = self.fapi.delete(self.new_name)
print resp, code
self.printMarker("doing list again to see if it's deleted")
resp, code = self.fapi.list()
print resp, code
FILE_NAME = "java.properties"
FILE_NAME_UTF16 = "javaUTF16.properties"
FILE_TYPE = "javaProperties"
FILE_PATH = "../resources/"
FILE_NAME_RENAMED = "java.properties.renamed"
CALLBACK_URL = "http://yourdomain.com/callback"
FILE_NAME_IMPORT = "test_import.xml"
FILE_NAME_TO_IMPORT = "test_import_es.xml"
FILE_TYPE_IMPORT ="android"
def ascii_test():
#test simple file
uploadDataASCII = UploadData(FILE_PATH, FILE_NAME, FILE_TYPE)
uploadDataASCII.addDirective(SmartlingDirective("placeholder_format_custom", "\[.+?\]"))
useSandbox = False
example = SmartlingApiExample(useSandbox, uploadDataASCII, "it-IT", FILE_NAME_RENAMED)
example.test()
def utf16_test():
#add charset and approveContent parameters
uploadDataUtf16 = UploadData(FILE_PATH, FILE_NAME_UTF16, FILE_TYPE)
uploadDataUtf16.setApproveContent("true")
uploadDataUtf16.setCallbackUrl(CALLBACK_URL)
useSandbox = False
example = SmartlingApiExample(useSandbox, uploadDataUtf16, "it-IT", FILE_NAME_RENAMED)
example.test()
def import_test():
#example for import and last_modified commands
uploadDataImport = UploadData(FILE_PATH, FILE_NAME_IMPORT, FILE_TYPE_IMPORT)
uploadDataImport.addDirective(SmartlingDirective("placeholder_format_custom", "\[.+?\]"))
useSandbox = False
example = SmartlingApiExample(useSandbox, uploadDataImport, "it-IT", FILE_NAME_RENAMED)
example.test_import(FILE_NAME_TO_IMPORT)
ascii_test()
utf16_test()
import_test()