-
Notifications
You must be signed in to change notification settings - Fork 0
/
documents.go
163 lines (126 loc) · 3.21 KB
/
documents.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
package lingotek
import (
"encoding/json"
"io"
"net/url"
)
func (l *Lingotek) ListDocuments(doneChan <-chan bool) (<-chan Document, <-chan error) {
resultChan := make(chan Document)
errChan := make(chan error, 1)
go func() {
defer close(resultChan)
defer close(errChan)
response := l.createDummyResponse("document", nil)
var documents []Document
var totalRead = int32(0)
for {
resp, err := l.getNextPage(response)
if err != nil {
if err != EndOfList {
errChan <- err
}
return
}
response = resp
if response.Properties.Size == 0 {
return
}
err = json.Unmarshal(response.Entities, &documents)
if err != nil {
errChan <- err
return
}
for i := 0; i < len(documents); i++ {
totalRead += 1
select {
case <-doneChan:
return
default:
resultChan <- documents[i]
}
}
if totalRead == response.Properties.Total {
return
}
}
}()
return resultChan, errChan
}
func (l *Lingotek) UploadString(title, content, localeCode string, project Project) (*Status, error) {
var status Status
v := url.Values{}
v.Set("title", title)
v.Set("content", content)
v.Set("locale_code", localeCode)
v.Set("project_id", project.Property.Id)
err := l.postEntity("document", &v, &status)
return &status, err
}
func (l *Lingotek) AddTranslation(document *Document, localeCode string) (*Translation, error) {
var translation Translation
if document.Property.Id == "" {
return nil, IdRequired
}
v := url.Values{}
v.Set("locale_code", localeCode)
err := l.postEntity("document/"+document.Property.Id+"/translation", &v, &translation)
return &translation, err
}
func (l *Lingotek) GetTranslatedDocument(document *Document, localeCode string, writer io.Writer) (int64, error) {
if document.Property.Id == "" {
return 0, IdRequired
}
v := url.Values{}
v.Set("locale_code", localeCode)
return l.downloadContent("document/"+document.Property.Id+"/content", &v, writer)
}
func (l *Lingotek) ListTranslations(document *Document, doneChan <-chan bool) (<-chan Translation, <-chan error) {
resultChan := make(chan Translation)
errChan := make(chan error, 1)
go func() {
defer close(resultChan)
defer close(errChan)
response := l.createDummyResponse("document/"+document.Property.Id+"/translation", nil)
var translations []Translation
for {
resp, err := l.getNextPage(response)
if err != nil {
if err != EndOfList {
errChan <- err
}
return
}
response = resp
if response.Properties.Size == 0 {
return
}
err = json.Unmarshal(response.Entities, &translations)
if err != nil {
errChan <- err
return
}
for i := 0; i < len(translations); i++ {
select {
case <-doneChan:
return
default:
resultChan <- translations[i]
}
}
}
}()
return resultChan, errChan
}
func (l *Lingotek) CheckStatus(doc Document) (*Document, error) {
if doc.Property.Id == "" {
return nil, IdRequired
}
var document Document
err := l.getEntity("document/"+doc.Property.Id, nil, &document)
return &document, err
}
func (l *Lingotek) GetDocument(id string) (*Document, error) {
var document Document
err := l.getEntity("document/"+id, nil, &document)
return &document, err
}