-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
352 lines (281 loc) · 7.34 KB
/
driver.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
package cloudcms
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime/debug"
"golang.org/x/oauth2"
)
type CloudcmsConfig struct {
Client_id string `json:"clientKey"`
Client_secret string `json:"clientSecret"`
Username string `json:"username"`
Password string `json:"password"`
BaseURL string `json:"baseURL"`
Debug bool `json:"debug"`
}
type CloudCmsSession struct {
oauthClient *http.Client
config *CloudcmsConfig
}
type JsonObject map[string]interface{}
type ResultMap struct {
rows []JsonObject
size int
total_rows int
offset int
}
func (obj *JsonObject) GetString(key string) string {
val, ok := (*obj)[key]
if !ok {
return ""
}
return fmt.Sprintf("%v", val)
}
func (obj *JsonObject) GetObject(key string) JsonObject {
val, ok := (*obj)[key]
if !ok {
return nil
}
m, ok := val.(map[string]interface{})
if !ok {
return nil
}
return JsonObject(m)
}
func (obj *JsonObject) GetArray(key string) []interface{} {
val, ok := (*obj)[key]
if !ok {
return nil
}
a, ok := val.([]interface{})
if !ok {
return nil
}
return a
}
func (obj *JsonObject) GetObjectArray(key string) []JsonObject {
arr := obj.GetArray(key)
if arr == nil {
return nil
}
res := make([]JsonObject, len(arr))
for i, v := range arr {
res[i] = v.(map[string]interface{})
}
return res
}
// This type implements the http.RoundTripper interface
type LoggingRoundTripper struct {
Proxied http.RoundTripper
}
func (lrt LoggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, e error) {
info := fmt.Sprintf("%v %v %v", req.Method, req.URL, req.Proto)
fmt.Println(info)
// Send the request, get the response (or the error)
res, e = lrt.Proxied.RoundTrip(req)
// Handle the result.
if e != nil {
fmt.Printf("Error: %v", e)
debug.PrintStack()
} else {
fmt.Printf("Received %v response\n", res.Status)
}
return res, e
}
func buildOAuthClient(cloudcmsConfig *CloudcmsConfig) (*http.Client, error) {
ctx := context.Background()
httpClient := http.Client{}
if cloudcmsConfig.Debug {
httpClient.Transport = LoggingRoundTripper{http.DefaultTransport}
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, &httpClient)
conf := &oauth2.Config{
ClientID: cloudcmsConfig.Client_id,
ClientSecret: cloudcmsConfig.Client_secret,
Endpoint: oauth2.Endpoint{
TokenURL: cloudcmsConfig.BaseURL + "/oauth/token",
AuthStyle: oauth2.AuthStyleInParams,
},
Scopes: []string{"api"},
}
token, err := conf.PasswordCredentialsToken(ctx, cloudcmsConfig.Username, cloudcmsConfig.Password)
if err != nil {
return nil, err
}
oauthClient := conf.Client(ctx, token)
return oauthClient, nil
}
func ToParams(objs ...JsonObject) url.Values {
params := url.Values{}
for _, obj := range objs {
if obj != nil {
for key, val := range obj {
bytes, _ := json.Marshal(val)
params.Add(key, string(bytes))
}
}
}
return params
}
func ToResultMap(obj JsonObject) *ResultMap {
// rows := []JsonObject{}
// rowsInterface := obj["rows"]
// rowsArr, ok := rowsInterface.([]interface{})
// if !ok {
// panic("Failed to retrieve rows")
// }
// for _, rowObj := range rowsArr {
// rows = append(rows, rowObj.(map[string]interface{}))
// }
rows := obj.GetObjectArray("rows")
return &ResultMap{
rows: rows,
size: int(obj["size"].(float64)),
total_rows: int(obj["total_rows"].(float64)),
offset: int(obj["offset"].(float64)),
}
}
func MapToReader(obj JsonObject) io.Reader {
if obj == nil {
return bytes.NewReader([]byte("{}"))
}
data, _ := json.Marshal(obj)
return bytes.NewReader(data)
}
func LoadConfig() *CloudcmsConfig {
var result *CloudcmsConfig
result = nil
wd, err := os.Getwd()
if err != nil {
return nil
}
if result == nil {
result = ReadConfig(filepath.Join(wd, "gitana.json"))
}
if result == nil {
result = ReadConfig(filepath.Join(wd, "gitana-test.json"))
}
if result == nil {
result = ReadConfig(filepath.Join(wd, "cloudcms.json"))
}
if result == nil {
result = ReadConfig(filepath.Join(wd, "cloudcms-test.json"))
}
return result
}
func ReadConfig(path string) *CloudcmsConfig {
data, err := os.ReadFile(path)
if err != nil {
return nil
}
var res CloudcmsConfig
err = json.Unmarshal(data, &res)
if err != nil {
return nil
}
return &res
}
func ConnectDefault() (*CloudCmsSession, error) {
config := LoadConfig()
if config == nil {
return nil, fmt.Errorf("could not locate gitana.json")
}
return Connect(config)
}
func Connect(cloudcmsConfig *CloudcmsConfig) (*CloudCmsSession, error) {
oauthClient, err := buildOAuthClient(cloudcmsConfig)
if err != nil {
return nil, err
}
client := &CloudCmsSession{
oauthClient: oauthClient,
config: cloudcmsConfig,
}
return client, nil
}
func (session *CloudCmsSession) Request(req *http.Request) (*http.Response, error) {
resp, err := session.oauthClient.Do(req)
if err != nil {
return nil, err
}
// Throw error for non-2xx repsonses
if resp.StatusCode < 200 || resp.StatusCode > 299 {
// Try to get response message
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
return nil, fmt.Errorf("%d: %s", resp.StatusCode, b)
}
return resp, nil
}
func (session *CloudCmsSession) RequestJson(method string, uri string, params url.Values, body io.Reader) (JsonObject, error) {
if params == nil {
params = url.Values{}
}
if !params.Has("full") {
params.Add("full", "true")
}
if !params.Has("metadata") {
params.Add("metadata", "true")
}
uri += "?" + params.Encode()
req, err := http.NewRequest(method, session.config.BaseURL+uri, body)
if err != nil {
return nil, err
}
resp, err := session.Request(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
target := make(JsonObject)
err = json.NewDecoder(resp.Body).Decode(&target)
if err != nil {
return nil, err
}
return target, nil
}
func (session *CloudCmsSession) Get(url string, params url.Values) (JsonObject, error) {
return session.RequestJson("GET", url, params, nil)
}
func (session *CloudCmsSession) Delete(url string, params url.Values) (JsonObject, error) {
return session.RequestJson("DELETE", url, params, nil)
}
func (session *CloudCmsSession) Post(url string, params url.Values, body io.Reader) (JsonObject, error) {
return session.RequestJson("POST", url, params, body)
}
func (session *CloudCmsSession) Put(url string, params url.Values, body io.Reader) (JsonObject, error) {
return session.RequestJson("PUT", url, params, body)
}
func (session *CloudCmsSession) Patch(url string, params url.Values, body io.Reader) (JsonObject, error) {
return session.RequestJson("PATCH", url, params, body)
}
func (session *CloudCmsSession) Download(url string, params url.Values) (io.ReadCloser, error) {
if params != nil {
url += "?" + params.Encode()
}
req, _ := http.NewRequest("GET", session.config.BaseURL+url, nil)
resp, err := session.Request(req)
if err != nil {
return nil, err
}
return resp.Body, nil
}
func (session *CloudCmsSession) MultipartPost(url string, params url.Values, formContentType string, body io.Reader) (io.ReadCloser, error) {
if params != nil {
url += "?" + params.Encode()
}
req, _ := http.NewRequest("POST", session.config.BaseURL+url, body)
req.Header.Set("Content-Type", formContentType)
resp, err := session.Request(req)
if err != nil {
return nil, err
}
return resp.Body, nil
}