-
Notifications
You must be signed in to change notification settings - Fork 0
/
memories.go
347 lines (269 loc) · 9.04 KB
/
memories.go
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
package modernmt
import (
"os"
"strconv"
)
func (re *memoryServices) List() ([]Memory, error) {
res, err := re.client.send("GET", "/memories", nil, nil, nil)
if err != nil {
return nil, err
}
var memories []Memory
for _, el := range res.([]interface{}) {
memories = append(memories, makeMemory(el.(map[string]interface{})))
}
return memories, nil
}
func (re *memoryServices) Get(id int64) (Memory, error) {
_id := strconv.FormatInt(id, 10)
return re.GetByKey(_id)
}
func (re *memoryServices) GetByKey(id string) (Memory, error) {
path := "/memories/" + id
res, err := re.client.send("GET", path, nil, nil, nil)
if err != nil {
return Memory{}, err
}
return makeMemory(res.(map[string]interface{})), nil
}
func (re *memoryServices) Create(name string, description string) (Memory, error) {
data := map[string]interface{}{
"name": name,
}
if description != "" {
data["description"] = description
}
res, err := re.client.send("POST", "/memories", data, nil, nil)
if err != nil {
return Memory{}, err
}
return makeMemory(res.(map[string]interface{})), nil
}
func (re *memoryServices) Connect(name string, description string, externalId string) (Memory, error) {
data := map[string]interface{}{
"name": name,
}
if description != "" {
data["description"] = description
}
if externalId != "" {
data["external_id"] = externalId
}
res, err := re.client.send("POST", "/memories", data, nil, nil)
if err != nil {
return Memory{}, err
}
return makeMemory(res.(map[string]interface{})), nil
}
func (re *memoryServices) Edit(id int64, name string, description string) (Memory, error) {
_id := strconv.FormatInt(id, 10)
return re.EditByKey(_id, name, description)
}
func (re *memoryServices) EditByKey(id string, name string, description string) (Memory, error) {
data := map[string]interface{}{}
if name != "" {
data["name"] = name
}
if description != "" {
data["description"] = description
}
path := "/memories/" + id
res, err := re.client.send("PUT", path, data, nil, nil)
if err != nil {
return Memory{}, err
}
return makeMemory(res.(map[string]interface{})), nil
}
func (re *memoryServices) Delete(id int64) (Memory, error) {
_id := strconv.FormatInt(id, 10)
return re.DeleteByKey(_id)
}
func (re *memoryServices) DeleteByKey(id string) (Memory, error) {
path := "/memories/" + id
res, err := re.client.send("DELETE", path, nil, nil, nil)
if err != nil {
return Memory{}, err
}
return makeMemory(res.(map[string]interface{})), nil
}
func (re *memoryServices) Add(id int64, source string, target string, sentence string, translation string,
tuid string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.AddWithSessionByKey(_id, source, target, sentence, translation, tuid, "")
}
func (re *memoryServices) AddByKey(id string, source string, target string, sentence string, translation string,
tuid string) (ImportJob, error) {
return re.AddWithSessionByKey(id, source, target, sentence, translation, tuid, "")
}
func (re *memoryServices) AddWithSession(id int64, source string, target string, sentence string, translation string,
tuid string, session string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.AddWithSessionByKey(_id, source, target, sentence, translation, tuid, session)
}
func (re *memoryServices) AddWithSessionByKey(id string, source string, target string,
sentence string, translation string, tuid string, session string) (ImportJob, error) {
data := map[string]interface{}{
"source": source,
"target": target,
"sentence": sentence,
"translation": translation,
}
if tuid != "" {
data["tuid"] = tuid
}
if session != "" {
data["session"] = session
}
path := "/memories/" + id + "/content"
res, err := re.client.send("POST", path, data, nil, nil)
if err != nil {
return ImportJob{}, err
}
return makeImportJob(res.(map[string]interface{})), nil
}
func (re *memoryServices) Replace(id int64, tuid string, source string, target string, sentence string,
translation string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.ReplaceWithSessionByKey(_id, tuid, source, target, sentence, translation, "")
}
func (re *memoryServices) ReplaceByKey(id string, tuid string, source string, target string, sentence string,
translation string) (ImportJob, error) {
return re.ReplaceWithSessionByKey(id, tuid, source, target, sentence, translation, "")
}
func (re *memoryServices) ReplaceWithSession(id int64, tuid string, source string, target string, sentence string,
translation string, session string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.ReplaceWithSessionByKey(_id, tuid, source, target, sentence, translation, session)
}
func (re *memoryServices) ReplaceWithSessionByKey(id string, tuid string, source string, target string, sentence string,
translation string, session string) (ImportJob, error) {
data := map[string]interface{}{
"tuid": tuid,
"source": source,
"target": target,
"sentence": sentence,
"translation": translation,
}
if session != "" {
data["session"] = session
}
path := "/memories/" + id + "/content"
res, err := re.client.send("PUT", path, data, nil, nil)
if err != nil {
return ImportJob{}, err
}
return makeImportJob(res.(map[string]interface{})), nil
}
func (re *memoryServices) ImportTmxPath(id int64, path string, compression string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.ImportTmxPathByKey(_id, path, compression)
}
func (re *memoryServices) ImportTmxPathByKey(id string, path string, compression string) (ImportJob, error) {
file, err := os.Open(path)
if err != nil {
return ImportJob{}, err
}
return re.ImportTmxByKey(id, file, compression)
}
func (re *memoryServices) ImportTmx(id int64, tmx *os.File, compression string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.ImportTmxByKey(_id, tmx, compression)
}
func (re *memoryServices) ImportTmxByKey(id string, tmx *os.File, compression string) (ImportJob, error) {
data := map[string]interface{}{}
if compression != "" {
data["compression"] = compression
}
files := map[string]*os.File{
"tmx": tmx,
}
path := "/memories/" + id + "/content"
res, err := re.client.send("POST", path, data, files, nil)
if err != nil {
return ImportJob{}, err
}
return makeImportJob(res.(map[string]interface{})), nil
}
func (re *memoryServices) AddToGlossary(id int64, terms []GlossaryTerm, _type string, tuid string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.AddToGlossaryByKey(_id, terms, _type, tuid)
}
func (re *memoryServices) AddToGlossaryByKey(id string, terms []GlossaryTerm, _type string,
tuid string) (ImportJob, error) {
data := map[string]interface{}{
"terms": terms,
"type": _type,
}
if tuid != "" {
data["tuid"] = tuid
}
path := "/memories/" + id + "/glossary"
res, err := re.client.send("POST", path, data, nil, nil)
if err != nil {
return ImportJob{}, err
}
return makeImportJob(res.(map[string]interface{})), nil
}
func (re *memoryServices) ReplaceInGlossary(id int64, terms []GlossaryTerm, _type string,
tuid string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.ReplaceInGlossaryByKey(_id, terms, _type, tuid)
}
func (re *memoryServices) ReplaceInGlossaryByKey(id string, terms []GlossaryTerm, _type string,
tuid string) (ImportJob, error) {
data := map[string]interface{}{
"terms": terms,
"type": _type,
}
if tuid != "" {
data["tuid"] = tuid
}
path := "/memories/" + id + "/glossary"
res, err := re.client.send("PUT", path, data, nil, nil)
if err != nil {
return ImportJob{}, err
}
return makeImportJob(res.(map[string]interface{})), nil
}
func (re *memoryServices) ImportGlossaryPath(id int64, path string, _type string,
compression string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.ImportGlossaryPathByKey(_id, path, _type, compression)
}
func (re *memoryServices) ImportGlossaryPathByKey(id string, path string, _type string,
compression string) (ImportJob, error) {
file, err := os.Open(path)
if err != nil {
return ImportJob{}, err
}
return re.ImportGlossaryByKey(id, file, _type, compression)
}
func (re *memoryServices) ImportGlossary(id int64, csv *os.File, _type string, compression string) (ImportJob, error) {
_id := strconv.FormatInt(id, 10)
return re.ImportGlossaryByKey(_id, csv, _type, compression)
}
func (re *memoryServices) ImportGlossaryByKey(id string, csv *os.File, _type string,
compression string) (ImportJob, error) {
data := map[string]interface{}{
"type": _type,
}
if compression != "" {
data["compression"] = compression
}
files := map[string]*os.File{
"csv": csv,
}
path := "/memories/" + id + "/glossary"
res, err := re.client.send("POST", path, data, files, nil)
if err != nil {
return ImportJob{}, err
}
return makeImportJob(res.(map[string]interface{})), nil
}
func (re *memoryServices) GetImportStatus(uuid string) (ImportJob, error) {
res, err := re.client.send("GET", "/import-jobs/"+uuid, nil, nil, nil)
if err != nil {
return ImportJob{}, err
}
return makeImportJob(res.(map[string]interface{})), nil
}