-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
285 lines (215 loc) · 6.15 KB
/
Copy pathapi.go
File metadata and controls
285 lines (215 loc) · 6.15 KB
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
package main
import (
"bytes"
"encoding/json"
"github.com/olekukonko/tablewriter"
"io/ioutil"
"log"
"net/http"
"os"
pathlib "path"
"path/filepath"
"strconv"
"strings"
)
const apiUrl = "https://api.bottalk.de/"
type ResponseBasic struct {
Result string
Error string
Message string
}
func (h ResponseBasic) GetError() string {
return h.Error
}
type ResponseInfo struct {
ResponseBasic
User string
}
type ResponseCreateSkill struct {
ResponseBasic
Token string
}
type ResponseSkillList struct {
ResponseBasic
Skills []struct {
Name string
Token string
Language string
}
}
type ResponseSkillFiles struct {
ResponseBasic
Skill struct {
Name string
Token string
Language string
}
Files []struct {
Name string
Content string
}
}
func apiPost(action string, data []byte, v interface{}) string {
url := apiUrl + action
// Create a Bearer string by appending string access token
var bearer = "Bearer " + token.Token
// Create a new request using http
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
// add authorization header to the req
req.Header.Add("Authorization", bearer)
req.Header.Set("User-Agent", "bottalk-cli v"+strconv.Itoa(Version))
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error on response.\n[ERRO] -", err)
}
body, _ := ioutil.ReadAll(resp.Body)
jd := json.NewDecoder(bytes.NewReader([]byte(body)))
err = jd.Decode(&v)
if err != nil {
log.Fatal("Failed to decode response: " + err.Error())
return ""
}
basic := ResponseBasic{}
jd = json.NewDecoder(bytes.NewReader([]byte(body)))
err = jd.Decode(&basic)
if err != nil {
log.Fatal("Failed to decode response: " + err.Error())
return ""
}
if strings.Contains(basic.Message, "token expired") {
log.Println("token expired, trying to refresh")
getTokenRefresh()
return apiPost(action, data, v)
}
if len(basic.Error) > 0 {
log.Fatal("Failed to make a call: " + basic.GetError() + " => " + basic.Message)
return ""
}
return string([]byte(body))
}
func apiGet(action string, v interface{}) string {
url := apiUrl + action
// Create a Bearer string by appending string access token
var bearer = "Bearer " + token.Token
// Create a new request using http
req, err := http.NewRequest("GET", url, nil)
// add authorization header to the req
req.Header.Add("Authorization", bearer)
req.Header.Set("User-Agent", "bottalk-cli v"+strconv.Itoa(Version))
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error on response.\n[ERRO] -", err)
}
body, _ := ioutil.ReadAll(resp.Body)
jd := json.NewDecoder(bytes.NewReader([]byte(body)))
err = jd.Decode(&v)
if err != nil {
log.Fatal("Failed to decode response: " + err.Error())
return ""
}
basic := ResponseBasic{}
jd = json.NewDecoder(bytes.NewReader([]byte(body)))
err = jd.Decode(&basic)
if err != nil {
log.Fatal("Failed to decode response: " + err.Error())
return ""
}
if strings.Contains(basic.Message, "token expired") {
log.Println("token expired, trying to refresh")
getTokenRefresh()
return apiGet(action, v)
}
if len(basic.Error) > 0 {
log.Fatal("Failed to make a call: " + basic.GetError() + " => " + basic.Message)
return ""
}
return string([]byte(body))
}
func getInfo() {
info := ResponseInfo{}
apiGet("info", &info)
log.Println("You are logged in with account: " + info.User)
}
func getSkillList() {
info := ResponseSkillList{}
apiGet("skills", &info)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Language", "Name"})
for _, v := range info.Skills {
table.Append([]string{v.Token, v.Language, v.Name})
}
table.Render() // Send output
}
func getSkillFiles(skillToken string) {
info := ResponseSkillFiles{}
apiGet("skill/"+skillToken, &info)
path := info.Skill.Name
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0700)
}
for _, j := range info.Files {
ioutil.WriteFile(path+"/"+j.Name, []byte(j.Content), 0700)
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(map[string]string{"token": info.Skill.Token, "name": info.Skill.Name, "language": info.Skill.Language})
ioutil.WriteFile(path+"/"+".skill.manifest", b.Bytes(), 0700)
log.Println("Successfully wrote skill " + info.Skill.Token + " (" + info.Skill.Name + ")")
}
func createNewSkill(skillName string, skillLanguage string) {
if skillLanguage == "" {
skillLanguage = "en-US"
}
values := map[string]string{"name": skillName, "language": skillLanguage}
jsonValue, _ := json.Marshal(values)
postResponse := ResponseCreateSkill{}
apiPost("skills/new", jsonValue, &postResponse)
if postResponse.Result == "ok" {
path := skillName
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0700)
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(map[string]string{"token": postResponse.Token, "name": skillName, "language": skillLanguage})
ioutil.WriteFile(path+"/"+".skill.manifest", b.Bytes(), 0700)
log.Println("Successfully created skill '" + postResponse.Token + "' (" + skillName + ")")
log.Println("Token: " + postResponse.Token)
}
}
func pushSkillFiles() {
manifest, err := os.Open(".skill.manifest")
if err != nil {
log.Fatalln("Cannot read skill manifest: " + err.Error())
}
mf := struct {
Name string
Token string
Language string
}{}
err = json.NewDecoder(manifest).Decode(&mf)
if err != nil {
log.Fatalln("Cannot decode manifest: " + err.Error())
}
log.Println("Gathering files in folder: ", path)
matches, _ := filepath.Glob(path + "*.yml")
if len(matches) == 0 {
log.Println("Scenario files not found -- nothing to push")
os.Exit(0)
}
sendFiles := []filedata{}
for _, file := range matches {
log.Println("* " + file)
dat, _ := ioutil.ReadFile(file)
sendFiles = append(sendFiles, filedata{Name: pathlib.Base(file), Content: string(dat)})
}
values := map[string][]filedata{"files": sendFiles}
jsonValue, _ := json.Marshal(values)
postResponse := ResponseBasic{}
apiPost("skill/"+mf.Token, jsonValue, &postResponse)
if postResponse.Result == "ok" {
log.Println("Successfully pushed your skill files")
}
}