-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackends_kantree.go
157 lines (124 loc) · 4.01 KB
/
backends_kantree.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
package forms
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
)
// KantreeBackend is a backend using Kantree
type KantreeBackend struct {
BaseUrl string
APIKey string
ProjectId string
TitleField string
}
// Type returns the backend type (Kantree)
func (b KantreeBackend) Type() string {
return "Kantree"
}
func (b KantreeBackend) Configuration() map[string]interface{} {
return map[string]interface{}{
"type": b.Type(),
"configuration": map[string]string{
"api_key": b.APIKey,
"project_id": b.ProjectId,
"title_field": b.TitleField,
},
}
}
// NewKantreeBackend initializes a new empty KantreeBackend
func NewKantreeBackend(configuration map[string]interface{}) KantreeBackend {
return KantreeBackend{
BaseUrl: "https://kantree.io/api/1.0/",
APIKey: configuration["api_key"].(string),
ProjectId: configuration["project_id"].(string),
TitleField: configuration["title_field"].(string),
}
}
// AddForm creates a new form in the backend
func (b KantreeBackend) AddForm(f Form) error {
return errors.New("not implemented")
}
// DeleteForm removes the form from the backend using formId
func (b KantreeBackend) DeleteForm(formId string) error {
return errors.New("not implemented")
}
// GetForms retrieves all forms from this backend
func (b KantreeBackend) GetForms() ([]Form, error) {
return nil, errors.New("not implemented")
}
// GetForm retrieves the form from the backend given a formId
func (b KantreeBackend) GetForm(id string) (Form, error) {
return NewUnstructuredForm(""), errors.New("not implemented")
}
// Submitresponse registers a response to the form given it's formid
func (b KantreeBackend) SubmitResponse(form Form, response Response) error {
req, err := http.NewRequest("GET", fmt.Sprintf("%sprojects/%s", b.BaseUrl, b.ProjectId), nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(b.APIKey+":")))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return err
}
if reflect.ValueOf(data["top_level_card_id"]).Kind() != reflect.Float64 {
return errors.New("top_level_card_id type is " + reflect.ValueOf(data["top_level_card_id"]).Kind().String() + " instead of float64")
}
toplevelcardid := data["top_level_card_id"].(float64)
// responseBytes, _ := json.MarshalIndent(response, "", " ")
// kantreecard := map[string]interface{}{
// "title": response.(map[string]interface{})[b.TitleField],
// "attributes": map[string]interface{}{
// "Description": string(responseBytes),
// },
// }
kantreecard := map[string]interface{}{
"title": response.(map[string]interface{})[b.TitleField],
"attributes": response,
}
kcdata, err := json.Marshal(kantreecard)
if err != nil {
return err
}
req2, err := http.NewRequest("POST", fmt.Sprintf("%scards/%.0f/children", b.BaseUrl, toplevelcardid), bytes.NewBuffer(kcdata))
if err != nil {
return err
}
req2.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(b.APIKey+":")))
req2.Header.Set("Content-Type", "application/json")
resp2, err := client.Do(req2)
if err != nil {
return err
}
defer resp2.Body.Close()
var newdata interface{}
err = json.NewDecoder(resp2.Body).Decode(&newdata)
if err != nil {
return err
}
return nil
}
// GetFormResponses retrieves the responses for a given form (with formId)
func (b KantreeBackend) GetFormResponses(formId string) ([]Response, error) {
return nil, errors.New("not implemented")
}
//AddFormBackend adds a new backend : unavailable for Kantree backends
func (b KantreeBackend) AddFormBackend(string, Backend) error {
return errors.New("not implemented")
}
//GetFormBackends retrieves form backends : unavailable for Kantree backends
func (b KantreeBackend) GetFormBackends(string) ([]Backend, error) {
return nil, errors.New("not implemented")
}