-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_string.go
422 lines (367 loc) · 12.2 KB
/
resource_string.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
package transifex_api_client
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
)
type ResourceString struct {
Attributes struct {
AppearanceOrder int `json:"appearance_order"`
CharacterLimit int `json:"character_limit"`
Context string `json:"context"`
DatetimeCreated string `json:"datetime_created"`
DeveloperComment string `json:"developer_comment"`
Instructions string `json:"instructions"`
Key string `json:"key"`
MetadataDatetimeModified string `json:"metadata_datetime_modified"`
Occurrences string `json:"occurrences"`
Pluralized bool `json:"pluralized"`
StringHash string `json:"string_hash"`
Strings struct {
One string `json:"one"`
Other string `json:"other"`
} `json:"strings"`
StringsDatetimeModified string `json:"strings_datetime_modified"`
Tags []string `json:"tags"`
} `json:"attributes"`
ID string `json:"id"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Relationships struct {
Committer struct {
Data struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"committer"`
Language struct {
Data struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"language"`
Resource struct {
Data struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"resource"`
} `json:"relationships"`
Type string `json:"type"`
}
type GetResourceStringsCollectionParameters struct {
Resource string
Cursor string
CreatedAfter time.Time
CreatedBefore time.Time
Key string
Tags []string
Limit string
}
type GetRevisionsOfResourceStringsParameters struct {
Resource string
Key string
Tags []string
Cursor string
Limit string
}
type ResourceStringRevision interface{}
// Get resource strings collection.
// https://developers.transifex.com/reference/get_resource-strings
func (t *TransifexApiClient) GetResourceStringsCollection(params GetResourceStringsCollectionParameters) ([]ResourceString, error) {
paramStr, err := t.createGetResourceStringsCollectionParametersString(params)
if err != nil {
return nil, err
}
// Define the variable to decode the service response
var rsc struct {
Data []ResourceString `json:"data"`
Links struct {
Self string `json:"self"`
Next string `json:"next"`
Previous string `json:"previous"`
} `json:"links"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/resource_strings",
paramStr,
}, ""),
bytes.NewBuffer(nil))
if err != nil {
t.l.Error(err)
return nil, err
}
// Set authorization and Accept HTTP request headers
req.Header.Set("Authorization", "Bearer "+t.token)
req.Header.Add("Accept", "application/vnd.api+json")
// Perform the request
resp, err := t.client.Do(req)
if err != nil {
t.l.Error(err)
return nil, err
}
// Decode the JSON response into the corresponding variable
err = json.NewDecoder(resp.Body).Decode(&rsc)
if err != nil {
t.l.Error(err)
return nil, err
}
return rsc.Data, nil
}
// Get the details of a specific resource string.
// https://developers.transifex.com/reference/get_resource-strings-resource-string-id
func (t *TransifexApiClient) GetResourceStringDetails(resource_string_id string) (ResourceString, error) {
// Define the variable to decode the service response
var rsd struct {
Data ResourceString `json:"data"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/resource_strings/",
resource_string_id,
}, ""),
bytes.NewBuffer(nil))
if err != nil {
t.l.Error(err)
return ResourceString{}, err
}
// Set authorization and Accept HTTP request headers
req.Header.Set("Authorization", "Bearer "+t.token)
req.Header.Add("Accept", "application/vnd.api+json")
// Perform the request
resp, err := t.client.Do(req)
if err != nil {
t.l.Error(err)
return ResourceString{}, err
}
// Decode the JSON response into the corresponding variable
err = json.NewDecoder(resp.Body).Decode(&rsd)
if err != nil {
t.l.Error(err)
return ResourceString{}, err
}
return rsd.Data, nil
}
// Get revisions of resource strings.
// https://developers.transifex.com/reference/get_resource-strings-revisions
func (t *TransifexApiClient) GetRevisionsOfResourceStrings(params GetRevisionsOfResourceStringsParameters) ([]ResourceStringRevision, error) {
paramStr, err := t.createGetRevisionsOfResourceStringsParametersString(params)
if err != nil {
return nil, err
}
// Define the variable to decode the service response
var rors struct {
Data []ResourceStringRevision `json:"data"`
Links struct {
Self string `json:"self"`
Next string `json:"next"`
Previous string `json:"previous"`
} `json:"links"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/resource_strings_revisions",
paramStr,
}, ""),
bytes.NewBuffer(nil))
if err != nil {
t.l.Error(err)
return nil, err
}
// Set authorization and Accept HTTP request headers
req.Header.Set("Authorization", "Bearer "+t.token)
req.Header.Add("Accept", "application/vnd.api+json")
// Perform the request
resp, err := t.client.Do(req)
if err != nil {
t.l.Error(err)
return nil, err
}
// Decode the JSON response into the corresponding variable
err = json.NewDecoder(resp.Body).Decode(&rors)
if err != nil {
t.l.Error(err)
return nil, err
}
return rors.Data, nil
}
// The function prints the information about a resource string
func (t *TransifexApiClient) PrintResourseString(s ResourceString, formatter string) {
switch formatter {
case "text":
fmt.Printf(" Type: %v\n", s.Type)
fmt.Printf(" ID: %v\n", s.ID)
fmt.Printf(" Attributes:\n")
fmt.Printf(" AppearanceOrder: %v\n", s.Attributes.AppearanceOrder)
fmt.Printf(" Key: %v\n", s.Attributes.Key)
fmt.Printf(" Context: %v\n", s.Attributes.Context)
fmt.Printf(" Strings:\n")
fmt.Printf(" Other: %v\n", s.Attributes.Strings.Other)
if len(s.Attributes.Tags) > 0 {
fmt.Printf(" Tags:\n")
for _, v := range s.Attributes.Tags {
fmt.Printf(" - %v\n", v)
}
}
fmt.Printf(" Occurrences: %v\n", s.Attributes.Occurrences)
fmt.Printf(" DeveloperComment: %v\n", s.Attributes.DeveloperComment)
fmt.Printf(" Instructions: %v\n", s.Attributes.Instructions)
fmt.Printf(" CharacterLimit: %v\n", s.Attributes.CharacterLimit)
fmt.Printf(" Pluralized: %v\n", s.Attributes.Pluralized)
fmt.Printf(" StringHash: %v\n", s.Attributes.StringHash)
fmt.Printf(" DatetimeCreated: %v\n", s.Attributes.DatetimeCreated)
fmt.Printf(" MetadataDatetimeModified: %v\n", s.Attributes.MetadataDatetimeModified)
fmt.Printf(" StringsDatetimeModified: %v\n", s.Attributes.StringsDatetimeModified)
fmt.Printf(" Relationships:\n")
fmt.Printf(" Resource:\n")
fmt.Printf(" Data:\n")
fmt.Printf(" Type: %v\n", s.Relationships.Resource.Data.Type)
fmt.Printf(" ID: %v\n", s.Relationships.Resource.Data.ID)
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", s.Relationships.Resource.Links.Related)
fmt.Printf(" Language:\n")
fmt.Printf(" Data:\n")
fmt.Printf(" Type: %v\n", s.Relationships.Language.Data.Type)
fmt.Printf(" ID: %v\n", s.Relationships.Language.Data.ID)
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", s.Relationships.Language.Links.Related)
fmt.Printf(" Committer:\n")
fmt.Printf(" Data:\n")
fmt.Printf(" Type: %v\n", s.Relationships.Committer.Data.Type)
fmt.Printf(" ID: %v\n", s.Relationships.Committer.Data.ID)
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", s.Relationships.Committer.Links.Related)
fmt.Printf(" Links:\n")
fmt.Printf(" Self: %v\n", s.Links.Self)
case "json":
text2print, err := json.Marshal(s)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(text2print))
default:
}
}
// The function checks the input set of parameters and converts it into a valid URL parameters string
func (t *TransifexApiClient) createGetResourceStringsCollectionParametersString(params GetResourceStringsCollectionParameters) (string, error) {
// Initialize the parameters string
paramStr := ""
// Add mandatory Resource option
if params.Resource == "" {
return "", fmt.Errorf("mandatory parameter 'Resource' is missed")
}
paramStr += "&filter[resource]=" + params.Resource
// Add optional Cursor value (from the previous response!)
// The cursor used for pagination.
// The value of the cursor must be retrieved from pagination links included in previous responses;
// you should not attempt to write them on your own.
if params.Cursor != "" {
paramStr += "&page[cursor]=" + params.Cursor
}
// Add optional datetime_created->gte value
if (params.CreatedAfter != time.Time{}) {
paramStr += "&filter[datetime_created][gte]=" + params.CreatedAfter.Format("2006-01-02T15:04:05Z")
}
// Add optional datetime_created->lt value
if (params.CreatedBefore != time.Time{}) {
paramStr += "&filter[datetime_created][lt]=" + params.CreatedBefore.Format("2006-01-02T15:04:05Z")
}
// Exact match for the key of the resource string.
//! This filter is case sensitive.
if params.Key != "" {
paramStr += "&filter[key]=" + params.Key
}
// Add Tags option
if len(params.Tags) != 0 {
paramStr += "&filter[tags][all]=" + strings.Join(params.Tags, ",")
}
// The page size limit. If not set, the default value is 150.
// If set, the minimum value it can take is 150 and the maximum 1000.
if params.Limit != "" {
num, err := strconv.Atoi(params.Limit)
if err != nil {
return "", fmt.Errorf("unable to convert 'Limit' value to int")
}
if num < 150 || num > 1000 {
return "", fmt.Errorf("value of 'Limit' parameter should be in the range [150..1000]")
}
paramStr += "&limit=" + params.Limit
} else {
paramStr += "&limit=150"
}
// Replace the & with ? symbol if the string is not empty
if len(paramStr) > 0 {
paramStr = "?" + strings.TrimPrefix(paramStr, "&")
}
return paramStr, nil
}
// The function checks the input set of parameters and converts it into a valid URL parameters string
func (t *TransifexApiClient) createGetRevisionsOfResourceStringsParametersString(params GetRevisionsOfResourceStringsParameters) (string, error) {
// Initialize the parameters string
paramStr := ""
// Add mandatory Resource option
if params.Resource == "" {
return "", fmt.Errorf("mandatory parameter 'Resource' is missed")
}
paramStr += "&filter[resource_string][resource]=" + params.Resource
// Add optional Cursor value (from the previous response!)
// The cursor used for pagination.
// The value of the cursor must be retrieved from pagination links included in previous responses;
// you should not attempt to write them on your own.
if params.Cursor != "" {
paramStr += "&page[cursor]=" + params.Cursor
}
// Exact match for the key of the resource string.
//! This filter is case sensitive.
if params.Key != "" {
paramStr += "&filter[resource_string][key]=" + params.Key
}
// Add Tags option
if len(params.Tags) != 0 {
paramStr += "&filter[resource_string][tags][all]=" + strings.Join(params.Tags, ",")
}
// The page size limit. If not set, the default value is 150.
// If set, the minimum value it can take is 150 and the maximum 1000.
if params.Limit != "" {
num, err := strconv.Atoi(params.Limit)
if err != nil {
return "", fmt.Errorf("unable to convert 'Limit' value to int")
}
if num < 150 || num > 1000 {
return "", fmt.Errorf("value of 'Limit' parameter should be in the range [150..1000]")
}
paramStr += "&limit=" + params.Limit
} else {
paramStr += "&limit=150"
}
// Replace the & with ? symbol if the string is not empty
if len(paramStr) > 0 {
paramStr = "?" + strings.TrimPrefix(paramStr, "&")
}
return paramStr, nil
}