forked from documize/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.go
375 lines (289 loc) · 8.2 KB
/
gemini.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
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package gemini
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
"github.com/documize/community/domain/store"
)
// Provider represents Gemini
type Provider struct {
Runtime *env.Runtime
Store *store.Store
}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "23b133f9-4020-4616-9291-a98fb939735f"
section.Title = "Gemini"
section.Description = "Work items and tickets"
section.ContentType = "gemini"
section.PageType = "tab"
return section
}
// Render converts Gemini data into HTML suitable for browser rendering.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
var items []geminiItem
var payload = geminiRender{}
var c = geminiConfig{}
json.Unmarshal([]byte(data), &items)
json.Unmarshal([]byte(config), &c)
c.ItemCount = len(items)
payload.Items = items
payload.Config = c
payload.Authenticated = c.UserID > 0
t := template.New("items")
t, _ = t.Parse(renderTemplate)
buffer := new(bytes.Buffer)
t.Execute(buffer, payload)
return buffer.String()
}
// Command handles authentication, workspace listing and items retrieval.
func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
provider.WriteMessage(w, "gemini", "missing method name")
return
}
switch method {
case "secrets":
secs(ctx, p.Store, w, r)
case "auth":
auth(ctx, p.Store, w, r)
case "workspace":
workspace(ctx, p.Store, w, r)
case "items":
items(ctx, p.Store, w, r)
}
}
// Refresh just sends back data as-is.
func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData string) {
var c = geminiConfig{}
err := json.Unmarshal([]byte(config), &c)
if err != nil {
p.Runtime.Log.Error("Unable to read Gemini config", err)
return
}
c.Clean(ctx, p.Store)
if len(c.URL) == 0 {
p.Runtime.Log.Info("Gemini.Refresh received empty URL")
return
}
if len(c.Username) == 0 {
p.Runtime.Log.Info("Gemini.Refresh received empty username")
return
}
if len(c.APIKey) == 0 {
p.Runtime.Log.Info("Gemini.Refresh received empty API key")
return
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/items/card/%d", c.URL, c.WorkspaceID), nil)
// req.Header.Set("Content-Type", "application/json")
creds := []byte(fmt.Sprintf("%s:%s", c.Username, c.APIKey))
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
if res.StatusCode != http.StatusOK {
return
}
defer res.Body.Close()
var items []geminiItem
dec := json.NewDecoder(res.Body)
err = dec.Decode(&items)
if err != nil {
p.Runtime.Log.Error("unable to Decode gemini items", err)
return
}
j, err := json.Marshal(items)
if err != nil {
p.Runtime.Log.Error("unable to marshal gemini items", err)
return
}
newData = string(j)
return
}
func auth(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
var config = geminiConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean(nil, store) // don't look at the database for the parameters
if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
creds := []byte(fmt.Sprintf("%s:%s", config.Username, config.APIKey))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/users/username/%s", config.URL, config.Username), nil)
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
config.SaveSecrets(ctx, store)
defer res.Body.Close()
var g = geminiUser{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&g)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
provider.WriteJSON(w, g)
}
func workspace(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
var config = geminiConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean(ctx, store)
if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
if config.UserID == 0 {
provider.WriteMessage(w, "gemini", "Missing UserId value")
return
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/navigationcards/users/%d", config.URL, config.UserID), nil)
creds := []byte(fmt.Sprintf("%s:%s", config.Username, config.APIKey))
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
defer res.Body.Close()
var workspace interface{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&workspace)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
provider.WriteJSON(w, workspace)
}
func items(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
var config = geminiConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean(ctx, store)
if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
creds := []byte(fmt.Sprintf("%s:%s", config.Username, config.APIKey))
filter, err := json.Marshal(config.Filter)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
var jsonFilter = []byte(string(filter))
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/items/filtered", config.URL), bytes.NewBuffer(jsonFilter))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
defer res.Body.Close()
var items interface{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&items)
if err != nil {
fmt.Println(err)
provider.WriteError(w, "gemini", err)
return
}
provider.WriteJSON(w, items)
}
func secs(ctx *provider.Context, store *store.Store, w http.ResponseWriter, r *http.Request) {
sec, _ := getSecrets(ctx, store)
provider.WriteJSON(w, sec)
}