-
Notifications
You must be signed in to change notification settings - Fork 8
/
onesky.go
543 lines (460 loc) · 13.7 KB
/
onesky.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Package onesky is go utils for working with OneSky translation service
// Copyright (c) 2015 Sebastian Czoch <[email protected]>. All rights reserved.
// Use of this source code is governed by a GNU v2 license found in the LICENSE file.
package onesky
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"time"
)
// APIAddress is https address to OneSky API
const APIAddress = "https://platform.api.onesky.io"
// APIVersion is OneSky API version which will be used
const APIVersion = "1"
// Client is basics struct for this package contains Secret, APIKey and ProjectID which is needed to authorize in OneSky service
type Client struct {
Secret string
APIKey string
ProjectID int
}
type apiEndpoint struct {
path string
method string
}
var apiEndpoints = map[string]apiEndpoint{
"getFile": apiEndpoint{"projects/%d/translations", "GET"},
"postFile": apiEndpoint{"projects/%d/files", "POST"},
"deleteFile": apiEndpoint{"projects/%d/files", "DELETE"},
"listFiles": apiEndpoint{"projects/%d/files", "GET"},
"importTasks": apiEndpoint{"projects/%d/import-tasks", "GET"},
"importTask": apiEndpoint{"projects/%d/import-tasks/%d", "GET"},
"getTranslationsStatus": apiEndpoint{"projects/%d/translations/status", "GET"},
"getLanguages": apiEndpoint{"projects/%d/languages", "GET"},
}
// FileData is a struct which contains informations about file uploaded to OneSky service
type FileData struct {
Name string `json:"name"`
FileName string `json:"file_name"`
StringCount int `json:"string_count"`
LastImport LastImport `json:"last_import"`
UpoladedAt string `json:"uploaded_at"`
UpoladedAtTimestamp int `json:"uploaded_at_timestamp"`
}
// LastImport is a struct which contains informations about last upload
type LastImport struct {
ID int `json:"id"`
Status string `json:"status"`
}
type listFilesResponse struct {
Data []FileData `json:"data"`
}
type getLanguagesResponse struct {
Data []Language `json:"data"`
}
// TaskData is a struct which contains informations about import task
type TaskData struct {
ID int64
OriginalID interface{} `json:"id"`
File TaskFile `json:"file"`
StringCount int `json:"string_count"`
WordCount int `json:"word_count"`
Status string `json:"status"`
CreateddAt string `json:"created_at"`
CreateddAtTimestamp int `json:"created_at_timestamp"`
}
// Language is a struct which contains informations about locale
type Language struct {
Code string `json:"code"`
EnglishName string `json:"english_name"`
LocalName string `json:"local_name"`
CustomLocale string `json:"custom_locale"`
Locale string `json:"locale"`
Region string `json:"region"`
TranslationProgress string `json:"translation_progress"`
}
// TaskFile is a struct which contains informations about file of import task
type TaskFile struct {
Name string `json:"name"`
Format string `json:"format"`
Locale Language `json:"locale"`
}
// ImportTasksResponse is a struct which contains informations about the response from list import tasks API
type ImportTasksResponse struct {
Data []TaskData `json:"data"`
}
// ImportTaskResponse is a struct which contains informations about the response from show an import task API
type ImportTaskResponse struct {
Data TaskData `json:"data"`
}
// UploadData is a struct which contains informations about uploaded file
type UploadData struct {
Name string `json:"name"`
Format string `json:"format"`
Language Language `json:"language"`
Import TaskData `json:"import"`
}
// UploadResponse is a struct which contains informations about the response from upload file API
type UploadResponse struct {
Data UploadData `json:"data"`
}
// TranslationsStatus is a struct which contains information about a project's translation status
type TranslationsStatus struct {
FileName string `json:"file_name"`
Locale Language `json:"locale"`
Progress string `json:"progress"`
StringCount int64 `json:"string_count"`
WordCount int64 `json:"word_count"`
}
type getTranslationsStatusResponse struct {
Data TranslationsStatus `json:"data"`
}
// convertToInt64 : Convert interface{} to int64
func convertToInt64(in interface{}) (int64, error) {
switch in.(type) {
case string:
return strconv.ParseInt(in.(string), 10, 64)
case int:
return int64(in.(int)), nil
case int16:
return int64(in.(int16)), nil
case int32:
return int64(in.(int32)), nil
case int64:
return in.(int64), nil
case uint:
return int64(in.(uint)), nil
case uint16:
return int64(in.(uint16)), nil
case uint32:
return int64(in.(uint32)), nil
case uint64:
return int64(in.(uint64)), nil
case float32:
return int64(in.(float32)), nil
case float64:
return int64(in.(float64)), nil
default:
return 0, fmt.Errorf("%s: %v", "Unable to convert value", in)
}
}
// ImportTask : Show an import task. Parameters: import_id
func (c *Client) ImportTask(importID int64) (TaskData, error) {
endpoint, err := getEndpoint("importTask")
if err != nil {
return TaskData{}, err
}
values := url.Values{}
urlStr, err := endpoint.full(c, values, importID)
if err != nil {
return TaskData{}, err
}
res, err := makeRequest(endpoint.method, urlStr, nil, "")
if err != nil {
return TaskData{}, err
}
if res.StatusCode != http.StatusOK {
return TaskData{}, fmt.Errorf("bad status: %s", res.Status)
}
body, err := getResponseBodyAsString(res)
if err != nil {
return TaskData{}, err
}
aux := ImportTaskResponse{}
err = json.Unmarshal([]byte(body), &aux)
if err != nil {
return TaskData{}, err
}
if i, err := convertToInt64(aux.Data.OriginalID); err == nil {
// fmt.Printf("%T, %v", i, i)
aux.Data.ID = i
}
return aux.Data, nil
}
// ImportTasks : List import tasks. Parameters: page: 1, per_page: 50, status: [all|completed|in-progress|failed]
// tasks, err := onesky.ImportTasks(map[string]interface{}{"per_page": 50, "status": "in-progress"})
func (c *Client) ImportTasks(params map[string]interface{}) ([]TaskData, error) {
endpoint, err := getEndpoint("importTasks")
if err != nil {
return nil, err
}
values := url.Values{}
values.Set("page", "1")
values.Set("per_page", "50")
values.Set("status", "all")
for k, v := range params {
values.Set(k, fmt.Sprintf("%v", v))
}
urlStr, err := endpoint.full(c, values)
if err != nil {
return nil, err
}
res, err := makeRequest(endpoint.method, urlStr, nil, "")
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status: %s", res.Status)
}
body, err := getResponseBodyAsString(res)
if err != nil {
return nil, err
}
aux := ImportTasksResponse{}
err = json.Unmarshal([]byte(body), &aux)
if err != nil {
return nil, err
}
for i := range aux.Data {
task := &aux.Data[i]
if id, err := convertToInt64(task.OriginalID); err == nil {
// fmt.Printf("\n%T, %v, %T\n", id, id, task.OriginalID)
task.ID = id
}
}
return aux.Data, nil
}
// ListFiles is method on Client struct which download form OneSky service informations about uploaded files
func (c *Client) ListFiles(page, perPage int) ([]FileData, error) {
endpoint, err := getEndpoint("listFiles")
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("page", strconv.Itoa(page))
v.Set("per_page", strconv.Itoa(perPage))
urlStr, err := endpoint.full(c, v)
if err != nil {
return nil, err
}
res, err := makeRequest(endpoint.method, urlStr, nil, "")
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status: %s", res.Status)
}
body, err := getResponseBodyAsString(res)
if err != nil {
return nil, err
}
aux := listFilesResponse{}
err = json.Unmarshal([]byte(body), &aux)
if err != nil {
return nil, err
}
return aux.Data, nil
}
// DownloadFile is method on Client struct which download form OneSky service choosen file as string
func (c *Client) DownloadFile(fileName, locale string) (string, error) {
endpoint, err := getEndpoint("getFile")
if err != nil {
return "", err
}
v := url.Values{}
v.Set("locale", locale)
v.Set("source_file_name", fileName)
urlStr, err := endpoint.full(c, v)
if err != nil {
return "", err
}
res, err := makeRequest(endpoint.method, urlStr, nil, "")
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("bad status: %s", res.Status)
}
body, err := getResponseBodyAsString(res)
if err != nil {
return "", err
}
return body, nil
}
// UploadFile is method on Client struct which upload file to OneSky service
func (c *Client) UploadFile(file, fileFormat, locale string, keepStrings bool) (UploadData, error) {
endpoint, err := getEndpoint("postFile")
if err != nil {
return UploadData{}, err
}
v := url.Values{}
v.Set("locale", locale)
v.Set("file_format", fileFormat)
v.Set("is_keeping_all_strings", strconv.FormatBool(keepStrings))
urlStr, err := endpoint.full(c, v)
if err != nil {
return UploadData{}, err
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
f, err := os.Open(file)
if err != nil {
return UploadData{}, err
}
defer w.Close()
fw, err := w.CreateFormFile("file", file)
if err != nil {
return UploadData{}, err
}
if _, err = io.Copy(fw, f); err != nil {
return UploadData{}, err
}
w.Close()
res, err := makeRequest(endpoint.method, urlStr, &b, w.FormDataContentType())
if err != nil {
return UploadData{}, err
}
if res.StatusCode != http.StatusCreated {
return UploadData{}, fmt.Errorf("bad status: %s", res.Status)
}
body, err := getResponseBodyAsString(res)
if err != nil {
return UploadData{}, err
}
aux := UploadResponse{}
err = json.Unmarshal([]byte(body), &aux)
if err != nil {
return UploadData{}, err
}
if i, err := convertToInt64(aux.Data.Import.OriginalID); err == nil {
aux.Data.Import.ID = i
}
return aux.Data, nil
}
// DeleteFile is method on Client struct which remove file from OneSky service
func (c *Client) DeleteFile(fileName string) error {
endpoint, err := getEndpoint("deleteFile")
if err != nil {
return err
}
v := url.Values{}
v.Set("file_name", fileName)
urlStr, err := endpoint.full(c, v)
if err != nil {
return err
}
res, err := makeRequest(endpoint.method, urlStr, nil, "")
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", res.Status)
}
return nil
}
// GetTranslationsStatus returns information about a project's translation status
func (c *Client) GetTranslationsStatus(fileName, locale string) (TranslationsStatus, error) {
endpoint, err := getEndpoint("getTranslationsStatus")
if err != nil {
return TranslationsStatus{}, err
}
v := url.Values{}
v.Set("file_name", fileName)
v.Set("locale", locale)
urlStr, err := endpoint.full(c, v)
if err != nil {
return TranslationsStatus{}, err
}
res, err := makeRequest(endpoint.method, urlStr, nil, "")
if err != nil {
return TranslationsStatus{}, err
}
if res.StatusCode != http.StatusOK {
return TranslationsStatus{}, fmt.Errorf("bad status: %s", res.Status)
}
body, err := getResponseBodyAsString(res)
if err != nil {
return TranslationsStatus{}, err
}
aux := getTranslationsStatusResponse{}
err = json.Unmarshal([]byte(body), &aux)
if err != nil {
return TranslationsStatus{}, err
}
return aux.Data, nil
}
// GetLanguages is method on Client struct which download from OneSky service information about available languages in project
func (c *Client) GetLanguages() ([]Language, error) {
endpoint, err := getEndpoint("getLanguages")
if err != nil {
return nil, err
}
v := url.Values{}
urlStr, err := endpoint.full(c, v)
if err != nil {
return nil, err
}
res, err := makeRequest(endpoint.method, urlStr, nil, "")
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status: %s", res.Status)
}
body, err := getResponseBodyAsString(res)
if err != nil {
return nil, err
}
aux := getLanguagesResponse{}
err = json.Unmarshal([]byte(body), &aux)
if err != nil {
return nil, err
}
return aux.Data, nil
}
func makeRequest(method, urlStr string, body io.Reader, contentType string) (*http.Response, error) {
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
return nil, err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func getResponseBodyAsString(response *http.Response) (string, error) {
res, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(res), nil
}
func (c *Client) getAuthHashAndTime() (string, string) {
hasher := md5.New()
time := strconv.Itoa(int(time.Now().Unix()))
hasher.Write([]byte(time + c.Secret))
return hex.EncodeToString(hasher.Sum(nil)), time
}
func (e *apiEndpoint) full(c *Client, additionalArgs url.Values, extends ...interface{}) (string, error) {
extends = append([]interface{}{c.ProjectID}, extends...)
urlWithProjectID := fmt.Sprintf(e.path, extends...)
address, err := url.Parse(APIAddress + "/" + APIVersion + "/" + urlWithProjectID)
if err != nil {
return "", err
}
hash, timestamp := c.getAuthHashAndTime()
additionalArgs.Set("api_key", c.APIKey)
additionalArgs.Set("timestamp", timestamp)
additionalArgs.Set("dev_hash", hash)
return address.String() + "?" + additionalArgs.Encode(), nil
}
func getEndpoint(name string) (apiEndpoint, error) {
endpoint, ok := apiEndpoints[name]
if !ok {
return apiEndpoint{}, fmt.Errorf("endpoint %s not found", name)
}
return endpoint, nil
}