-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizations.go
200 lines (172 loc) · 4.91 KB
/
organizations.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
package transifex_api_client
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
)
type Organization struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Name string `json:"name"`
Slug string `json:"slug"`
LogoURL string `json:"logo_url"`
Private bool `json:"private"`
} `json:"attributes"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Relationships struct {
Projects struct {
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"projects"`
Teams struct {
Links struct {
Related string `json:"related"`
} `json:"links"`
} `json:"teams"`
} `json:"relationships"`
}
type ListOrganizationsParameters struct {
Cursor string
Slug string
}
// Get a list of all the Organizations the user belongs to.
// https://developers.transifex.com/reference/get_organizations
func (t *TransifexApiClient) ListOrganizations(params ListOrganizationsParameters) ([]Organization, error) {
paramStr, err := t.createListOrganizationsParametersString(params)
if err != nil {
return nil, err
}
// Define the variable to decode the service response
var lor struct {
Data []Organization `json:"data"`
Links struct {
Next string `json:"next"`
Previous string `json:"previous"`
Self string `json:"self"`
} `json:"links"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/organizations",
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(&lor)
if err != nil {
t.l.Error(err)
return nil, err
}
return lor.Data, nil
}
// Get the details of an Organization.
// https://developers.transifex.com/reference/get_organizations-organization-id
func (t *TransifexApiClient) GetOrganizationDetails(id string) (Organization, error) {
// Define the variable to decode the service response
var od struct {
Data Organization `json:"data"`
}
// Create an API request
req, err := http.NewRequest(
"GET",
strings.Join([]string{
t.apiURL,
"/organizations/",
id,
}, ""),
bytes.NewBuffer(nil),
)
if err != nil {
t.l.Error(err)
return Organization{}, 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 Organization{}, err
}
// Decode the JSON response into the corresponding variable
err = json.NewDecoder(resp.Body).Decode(&od)
if err != nil {
t.l.Error(err)
return Organization{}, err
}
return od.Data, nil
}
// The function prints the information about an organization
func (t *TransifexApiClient) PrintOrganization(o Organization, formatter string) {
switch formatter {
case "text":
fmt.Printf(" ID: %v\n", o.ID)
fmt.Printf(" Type: %v\n", o.Type)
fmt.Printf(" Attributes:\n")
fmt.Printf(" Name: %v\n", o.Attributes.Name)
fmt.Printf(" Slug: %v\n", o.Attributes.Slug)
fmt.Printf(" LogoURL: %v\n", o.Attributes.LogoURL)
fmt.Printf(" Private: %v\n", o.Attributes.Private)
fmt.Printf(" Links:\n")
fmt.Printf(" Self: %v\n", o.Links.Self)
fmt.Printf(" Relationships:\n")
fmt.Printf(" Projects:\n")
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", o.Relationships.Projects.Links.Related)
fmt.Printf(" Teams:\n")
fmt.Printf(" Links:\n")
fmt.Printf(" Related: %v\n", o.Relationships.Teams.Links.Related)
case "json":
text2print, err := json.Marshal(o)
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) createListOrganizationsParametersString(params ListOrganizationsParameters) (string, error) {
// Initialize the parameters string
paramStr := ""
// 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 slug of the organization to get details
if params.Slug != "" {
paramStr += "&filter[slug]=" + params.Slug
}
// Replace the & with ? symbol if the string is not empty
if len(paramStr) > 0 {
paramStr = "?" + strings.TrimPrefix(paramStr, "&")
}
return paramStr, nil
}