-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam_membership.go
311 lines (271 loc) · 7.9 KB
/
team_membership.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
package transifex_api_client
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
type ListTeamMembershipsParameters struct {
Organization string
Team string
Language string
User string
Role string
Cursor string
Include string
}
type GetSingleTeamMembershipParameters struct {
TeamMembership string
Include string
}
type TeamMembership struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes struct {
Role string `json:"role"`
} `json:"attributes"`
Relationships struct {
Team struct {
Data struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"data"`
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"team"`
Language struct {
Data struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"data"`
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"language"`
User struct {
Data struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"data"`
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"user"`
} `json:"relationships"`
Links struct {
Self string `json:"self"`
} `json:"links"`
}
// List team memberships.
// https://developers.transifex.com/reference/get_team-memberships
func (t *TransifexApiClient) ListTeamMemberships(params ListTeamMembershipsParameters) ([]TeamMembership, error) {
paramStr, err := t.createListTeamMembershipsParametersString(params)
if err != nil {
return nil, err
}
// Define the variable to decode the service response
var tms struct {
Data []TeamMembership `json:"data"`
Included []struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Username string `json:"username"`
} `json:"attributes"`
Links struct {
Self string `json:"self"`
} `json:"links"`
} `json:"included"`
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,
"/team_memberships",
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(&tms)
if err != nil {
t.l.Error(err)
return nil, err
}
return tms.Data, nil
}
// Get single team membership.
// https://developers.transifex.com/reference/get_team-memberships-team-membership-id
func (t *TransifexApiClient) GetSingleTeamMembership(params GetSingleTeamMembershipParameters) (TeamMembership, error) {
paramStr, err := t.createGetSingleTeamMembershipParametersString(params)
if err != nil {
return TeamMembership{}, err
}
// Define the variable to decode the service response
var tms struct {
Data TeamMembership `json:"data"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/team_memberships/",
params.TeamMembership,
paramStr,
}, ""),
bytes.NewBuffer(nil))
if err != nil {
t.l.Error(err)
return TeamMembership{}, 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 TeamMembership{}, err
}
// Decode the JSON response into the corresponding variable
err = json.NewDecoder(resp.Body).Decode(&tms)
if err != nil {
t.l.Error(err)
return TeamMembership{}, err
}
return tms.Data, nil
}
// The function prints the information about an team membership
func (t *TransifexApiClient) PrintTeamMembership(tm TeamMembership, formatter string) {
switch formatter {
case "text":
fmt.Printf("Team membership information:\n")
fmt.Printf(" Type: %v\n", tm.Type)
fmt.Printf(" ID: %v\n", tm.ID)
fmt.Printf(" Attributes:\n")
fmt.Printf(" Role: %v\n", tm.Attributes.Role)
fmt.Printf(" Relationships:\n")
fmt.Printf(" Team:\n")
fmt.Printf(" Data:\n")
fmt.Printf(" Type: %v\n", tm.Relationships.Team.Data.Type)
fmt.Printf(" ID: %v\n", tm.Relationships.Team.Data.ID)
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", tm.Relationships.Team.Links.Related)
fmt.Printf(" Language:\n")
fmt.Printf(" Data:\n")
fmt.Printf(" Type: %v\n", tm.Relationships.Language.Data.Type)
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", tm.Relationships.Language.Links.Related)
fmt.Printf(" User:\n")
fmt.Printf(" Data:\n")
fmt.Printf(" Type: %v\n", tm.Relationships.User.Data.Type)
fmt.Printf(" ID: %v\n", tm.Relationships.User.Data.ID)
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", tm.Relationships.User.Links.Related)
fmt.Printf(" Links:\n")
fmt.Printf(" Self: %v\n", tm.Links.Self)
case "json":
text2print, err := json.Marshal(tm)
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) createListTeamMembershipsParametersString(params ListTeamMembershipsParameters) (string, error) {
// Initialize the parameters string
paramStr := ""
// Add mandatory Organization option
if params.Organization == "" {
return "", fmt.Errorf("mandatory parameter 'Organization' is missed")
}
paramStr += "&filter[organization]=" + params.Organization
// Add optional Team value
if params.Team != "" {
paramStr += "&filter[team]=" + params.Team
}
// Add optional Language value
if params.Language != "" {
paramStr += "&filter[language]=" + params.Language
}
// Add optional User value
if params.User != "" {
paramStr += "&filter[user]=" + params.User
}
// Add optional Role value
switch strings.ToLower(params.Role) {
case "coordinator":
fallthrough
case "translator":
fallthrough
case "reviewer":
paramStr += "&filter[role]=" + strings.ToLower(params.Role)
case "":
default:
return "", fmt.Errorf("unknown 'Origin' value")
}
// 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 Include value
if params.Include != "" {
if params.Include != "user" {
return "", fmt.Errorf("unknown 'Include' value")
}
paramStr += "&include=user"
}
// 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) createGetSingleTeamMembershipParametersString(params GetSingleTeamMembershipParameters) (string, error) {
// Initialize the parameters string
paramStr := ""
// Add mandatory TeamMembership option
if params.TeamMembership == "" {
return "", fmt.Errorf("mandatory parameter 'TeamMembership' is missed")
}
// Add optional Include value
if params.Include != "" {
if params.Include != "user" {
return "", fmt.Errorf("unknown 'Include' value")
}
paramStr += "&include=user"
}
// Replace the & with ? symbol if the string is not empty
if len(paramStr) > 0 {
paramStr = "?" + strings.TrimPrefix(paramStr, "&")
}
return paramStr, nil
}