-
Notifications
You must be signed in to change notification settings - Fork 31
/
apps.go
331 lines (272 loc) · 8.53 KB
/
apps.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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
// AppTrainingStatus - Represents the status of an app
type AppTrainingStatus string
const (
// Done status
Done AppTrainingStatus = "done"
// Scheduled status
Scheduled AppTrainingStatus = "scheduled"
// Ongoing status
Ongoing AppTrainingStatus = "ongoing"
)
// App - https://wit.ai/docs/http/#get__apps_link
type App struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Lang string `json:"lang"`
Private bool `json:"private"`
// Timezone is only used when creating/updating an app; it's
// not available when getting the details of an app.
Timezone string `json:"timezone,omitempty"`
CreatedAt Time `json:"created_at,omitempty"`
WillTrainAt Time `json:"will_train_at,omitempty"`
LastTrainedAt Time `json:"last_trained_at,omitempty"`
LastTrainingDurationSecs int `json:"last_training_duration_secs,omitempty"`
TrainingStatus AppTrainingStatus `json:"training_status,omitempty"`
}
// Time - Custom type to encapsulated a time.Time
type Time struct {
time.Time
}
// UnmarshalJSON - Our unmarshal function for our custom type
func (witTime *Time) UnmarshalJSON(input []byte) error {
strInput := string(input)
strInput = strings.Trim(strInput, `"`)
newTime, err := time.Parse(WitTimeFormat, strInput)
if err != nil {
return err
}
witTime.Time = newTime
return nil
}
// CreatedApp - https://wit.ai/docs/http/#post__apps_link
type CreatedApp struct {
AccessToken string `json:"access_token"`
AppID string `json:"app_id"`
}
// GetApps - Returns an array of all apps that you own.
//
// https://wit.ai/docs/http/#get__apps_link
func (c *Client) GetApps(limit int, offset int) ([]App, error) {
if limit <= 0 {
limit = 0
}
if offset <= 0 {
offset = 0
}
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps?limit=%d&offset=%d", limit, offset), "application/json", nil)
if err != nil {
return []App{}, err
}
defer resp.Close()
var apps []App
decoder := json.NewDecoder(resp)
err = decoder.Decode(&apps)
return apps, err
}
// GetApp - Returns an object representation of the specified app.
//
// https://wit.ai/docs/http/#get__apps__app_link
func (c *Client) GetApp(id string) (*App, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var app *App
decoder := json.NewDecoder(resp)
if err = decoder.Decode(&app); err != nil {
return nil, err
}
return app, nil
}
// CreateApp - creates new app.
//
// https://wit.ai/docs/http/#post__apps_link
func (c *Client) CreateApp(app App) (*CreatedApp, error) {
appJSON, err := json.Marshal(app)
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, "/apps", "application/json", bytes.NewBuffer(appJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var createdApp *CreatedApp
decoder := json.NewDecoder(resp)
err = decoder.Decode(&createdApp)
return createdApp, err
}
// UpdateApp - Updates an app.
//
// https://wit.ai/docs/http/#put__apps__app_link
func (c *Client) UpdateApp(id string, app App) error {
appJSON, err := json.Marshal(app)
if err != nil {
return err
}
resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", bytes.NewBuffer(appJSON))
if err == nil {
resp.Close()
}
return err
}
// DeleteApp - deletes app by ID.
//
// https://wit.ai/docs/http/#delete__apps__app_link
func (c *Client) DeleteApp(id string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}
// AppTag - https://wit.ai/docs/http/#get__apps__app_tags__tag_link
type AppTag struct {
Name string `json:"name,omitempty"`
Desc string `json:"desc,omitempty"`
CreatedAt Time `json:"created_at,omitempty"`
UpdatedAt Time `json:"updated_at,omitempty"`
}
// GetAppTags - Returns an array of all tag groups for an app.
// Within a group, all tags point to the same app state (as a result of moving tags).
//
// https://wit.ai/docs/http/#get__apps__app_tags_link
func (c *Client) GetAppTags(appID string) ([][]AppTag, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags", url.PathEscape(appID)), "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var tags [][]AppTag
decoder := json.NewDecoder(resp)
err = decoder.Decode(&tags)
return tags, err
}
// GetAppTag - returns the detail of the specified tag.
//
// https://wit.ai/docs/http/#get__apps__app_tags__tag_link
func (c *Client) GetAppTag(appID, tagID string) (*AppTag, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var tag *AppTag
decoder := json.NewDecoder(resp)
err = decoder.Decode(&tag)
return tag, err
}
// CreateAppTag - Take a snapshot of the current app state, save it as a tag (version)
// of the app. The name of the tag created will be returned in the response.
//
// https://wit.ai/docs/http/#post__apps__app_tags_link
func (c *Client) CreateAppTag(appID string, tag string) (*AppTag, error) {
type appTag struct {
Tag string `json:"tag"`
}
tagJSON, err := json.Marshal(appTag{Tag: tag})
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, fmt.Sprintf("/apps/%s/tags", url.PathEscape(tag)), "application/json", bytes.NewBuffer(tagJSON))
if err != nil {
return nil, err
}
defer resp.Close()
// theresponse format is different than the one in get API.
var tmp appTag
decoder := json.NewDecoder(resp)
if err := decoder.Decode(&tmp); err != nil {
return nil, err
}
return &AppTag{Name: tmp.Tag}, nil
}
// UpdateAppTagRequest - https://wit.ai/docs/http/#put__apps__app_tags__tag_link
type UpdateAppTagRequest struct {
Tag string `json:"tag,omitempty"`
Desc string `json:"desc,omitempty"`
MoveTo string `json:"move_to,omitempty"`
}
// UpdateAppTagResponse - https://wit.ai/docs/http/#put__apps__app_tags__tag_link
type UpdateAppTagResponse struct {
Tag string `json:"tag,omitempty"`
Desc string `json:"desc,omitempty"`
MovedTo string `json:"moved_to,omitempty"`
}
// UpdateAppTag - Update the tag's name or description
//
// https://wit.ai/docs/http/#put__apps__app_tags__tag_link
func (c *Client) UpdateAppTag(appID, tagID string, updated AppTag) (*AppTag, error) {
type tag struct {
Tag string `json:"tag,omitempty"`
Desc string `json:"desc,omitempty"`
}
updateJSON, err := json.Marshal(tag{Tag: updated.Name, Desc: updated.Desc})
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", bytes.NewBuffer(updateJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var tagResp tag
decoder := json.NewDecoder(resp)
err = decoder.Decode(&tagResp)
return &AppTag{Name: tagResp.Tag, Desc: tagResp.Desc}, err
}
type MovedAppTag struct {
Tag string `json:"tag"`
Desc string `json:"desc"`
MovedTo string `json:"moved_to"`
}
// MoveAppTag - move the tag to point to another tag.
//
// https://wit.ai/docs/http/#put__apps__app_tags__tag_link
func (c *Client) MoveAppTag(appID, tagID string, to string, updated *AppTag) (*MovedAppTag, error) {
type tag struct {
Tag string `json:"tag,omitempty"`
Desc string `json:"desc,omitempty"`
MoveTo string `json:"move_to,omitempty"`
}
updateReq := tag{MoveTo: to}
if updated != nil {
updateReq.Tag = updated.Name
updateReq.Desc = updated.Desc
}
updateJSON, err := json.Marshal(updateReq)
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", bytes.NewBuffer(updateJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var tagResp *MovedAppTag
decoder := json.NewDecoder(resp)
err = decoder.Decode(&tagResp)
return tagResp, err
}
// DeleteAppTag - Permanently delete the tag.
//
// https://wit.ai/docs/http/#delete__apps__app_tags__tag_link
func (c *Client) DeleteAppTag(appID, tagID string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}