forked from ngalaiko/poeditor-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoeditor.go
228 lines (185 loc) · 4.36 KB
/
poeditor.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
package poeditor
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"errors"
"os"
"io"
"bytes"
"mime/multipart"
)
type Poeditor struct {
ApiToken string
ProjectId string
}
func New(apiToken string, projectId string) *Poeditor {
return &Poeditor{
ApiToken: apiToken,
ProjectId: projectId,
}
}
func (t *Poeditor) ListProjectLanguages() ([]Language, error) {
response, err := t.request(actionListLanguages, map[string]string{})
if err != nil {
return nil, err
}
result := []Language{}
for _, listElem := range response.List {
result = append(result, Language(listElem.Code))
}
return result, nil
}
func (t *Poeditor) ListProjectTerms(language Language) ([]*PoTerm, error) {
response, err := t.request(actionListTerms, map[string]string{
"language": language.String(),
})
if err != nil {
return nil, err
}
result := []*PoTerm{}
for _, listElem := range response.List {
result = append(result, &PoTerm{
Term: listElem.Term,
Context: listElem.Context,
})
}
return result, nil
}
func (t *Poeditor) AddTermToProject(terms []*PoTerm) error {
bytes, err := json.Marshal(terms)
if err != nil {
return err
}
_, err = t.request(actionAddTerms, map[string]string{
"data": string(bytes),
})
if err != nil {
return err
}
return nil
}
func (t *Poeditor) DeleteTermFromProject(terms []*PoTerm) error {
bytes, err := json.Marshal(terms)
if err != nil {
return err
}
_, err = t.request(actionDeleteTerms, map[string]string{
"data": string(bytes),
})
if err != nil {
return err
}
return nil
}
func (t *Poeditor) SyncTerms(terms []*PoTerm) error {
bytes, err := json.Marshal(terms)
if err != nil {
return err
}
_, err = t.request(actionSyncTerms, map[string]string{
"data": string(bytes),
})
if err != nil {
return err
}
return nil
}
func (t *Poeditor) Export(language Language, filetype FileType) (string, error) {
response, err := t.request(actionExportTerms, map[string]string{
"language": language.String(),
"type": filetype.String(),
})
if err != nil {
return "", err
}
return response.Item, nil
}
func (t *Poeditor) Upload(pathToFile string, language Language, updateType UpdateType) error {
file, err := os.Open(pathToFile)
if err != nil {
return err
}
defer file.Close()
_, err = t.requestWithFile(pathToFile, map[string]string{
"language": language.String(),
"updating": updateType.String(),
"api_token": t.ApiToken,
"action": actionUploadTerms.String(),
"id": t.ProjectId,
})
if err != nil {
return err
}
return nil
}
func (t *Poeditor) request(action Action, params map[string]string) (*result, error) {
data, err := t.parseParams(t.ProjectId, action, params)
if err != nil {
return nil, err
}
resp, err := http.PostForm(apiUrl, data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return t.parseResponse(resp)
}
func (t *Poeditor) requestWithFile(pathToFile string, params map[string]string) (*result, error){
file, err := os.Open(pathToFile)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
fw, err := writer.CreateFormFile("file", pathToFile)
if err != nil {
return nil, err
}
if _, err = io.Copy(fw, file); err != nil {
return nil, err
}
for key, value := range params {
writer.WriteField(key, value)
}
if err := writer.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", apiUrl, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
return nil, err
}
return t.parseResponse(response)
}
func (t *Poeditor) parseResponse(response *http.Response) (*result, error) {
bytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, ErrPoeErrorResponse
}
result := new(result)
if err := json.Unmarshal(bytes, result); err != nil {
return nil, err
}
if result.Response.Status != "success" {
return nil, errors.New(result.Response.Message)
}
return result, nil
}
func (t *Poeditor) parseParams(projectId string, action Action, params map[string]string) (url.Values, error) {
urlParams := url.Values{}
urlParams.Add("api_token", t.ApiToken)
urlParams.Add("action", action.String())
urlParams.Add("id", projectId)
for param, value := range params {
urlParams.Add(param, value)
}
return urlParams, nil
}