Skip to content

Commit 572a982

Browse files
committed
feat: update app-related API to current version
The new API version changes the request/response schema, plus added a way to version apps: - App struct changed to reflect the update - Added GetAppTags, GetAppTag, CreateAppTag, UpdateAppTag, MoveAppTag, and DeleteAppTag for tag-related APIs
1 parent 2c8f1ad commit 572a982

File tree

2 files changed

+447
-44
lines changed

2 files changed

+447
-44
lines changed

apps.go

Lines changed: 200 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,22 @@ const (
2424
Ongoing AppTrainingStatus = "ongoing"
2525
)
2626

27-
// App - https://wit.ai/docs/http/20170307#get__apps_link
27+
// App - https://wit.ai/docs/http/20200513/#get__apps_link
2828
type App struct {
29+
ID string `json:"id,omitempty"`
2930
Name string `json:"name"`
3031
Lang string `json:"lang"`
3132
Private bool `json:"private"`
32-
// Description presents when we get an app
33-
Description string `json:"description,omitempty"`
34-
// Use Desc when create an app
35-
Desc string `json:"desc,omitempty"`
36-
// ID presents when we get an app
37-
ID string `json:"id,omitempty"`
38-
// AppID presents when we create an app
39-
AppID string `json:"app_id,omitempty"`
40-
CreatedAt string `json:"created_at,omitempty"`
41-
Timezone string `json:"timezone,omitempty"`
42-
// Training information
43-
LastTrainingDurationSecs int `json:"last_training_duration_secs,omitempty"`
33+
34+
// Timezone is only used when creating/updating an app; it's
35+
// not available when getting the details of an app.
36+
Timezone string `json:"timezone,omitempty"`
37+
38+
CreatedAt Time `json:"created_at,omitempty"`
39+
4440
WillTrainAt Time `json:"will_train_at,omitempty"`
4541
LastTrainedAt Time `json:"last_trained_at,omitempty"`
42+
LastTrainingDurationSecs int `json:"last_training_duration_secs,omitempty"`
4643
TrainingStatus AppTrainingStatus `json:"training_status,omitempty"`
4744
}
4845

@@ -63,13 +60,15 @@ func (witTime *Time) UnmarshalJSON(input []byte) error {
6360
return nil
6461
}
6562

66-
// CreatedApp - https://wit.ai/docs/http/20170307#post__apps_link
63+
// CreatedApp - https://wit.ai/docs/http/20200513/#post__apps_link
6764
type CreatedApp struct {
6865
AccessToken string `json:"access_token"`
6966
AppID string `json:"app_id"`
7067
}
7168

72-
// GetApps - Returns an array of all apps that you own. https://wit.ai/docs/http/20170307#get__apps_link
69+
// GetApps - Returns an array of all apps that you own.
70+
//
71+
// https://wit.ai/docs/http/20200513/#get__apps_link
7372
func (c *Client) GetApps(limit int, offset int) ([]App, error) {
7473
if limit <= 0 {
7574
limit = 0
@@ -91,7 +90,9 @@ func (c *Client) GetApps(limit int, offset int) ([]App, error) {
9190
return apps, err
9291
}
9392

94-
// GetApp - returns map by ID. https://wit.ai/docs/http/20170307#get__apps__app_id_link
93+
// GetApp - Returns an object representation of the specified app.
94+
//
95+
// https://wit.ai/docs/http/20200513/#get__apps__app_link
9596
func (c *Client) GetApp(id string) (*App, error) {
9697
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
9798
if err != nil {
@@ -109,7 +110,49 @@ func (c *Client) GetApp(id string) (*App, error) {
109110
return app, nil
110111
}
111112

112-
// DeleteApp - deletes app by ID. https://wit.ai/docs/http/20170307#delete__apps__app_id_link
113+
// CreateApp - creates new app.
114+
//
115+
// https://wit.ai/docs/http/20200513/#post__apps_link
116+
func (c *Client) CreateApp(app App) (*CreatedApp, error) {
117+
appJSON, err := json.Marshal(app)
118+
if err != nil {
119+
return nil, err
120+
}
121+
122+
resp, err := c.request(http.MethodPost, "/apps", "application/json", bytes.NewBuffer(appJSON))
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
defer resp.Close()
128+
129+
var createdApp *CreatedApp
130+
decoder := json.NewDecoder(resp)
131+
err = decoder.Decode(&createdApp)
132+
133+
return createdApp, err
134+
}
135+
136+
// UpdateApp - Updates an app.
137+
//
138+
// https://wit.ai/docs/http/20200513/#put__apps__app_link
139+
func (c *Client) UpdateApp(id string, app App) error {
140+
appJSON, err := json.Marshal(app)
141+
if err != nil {
142+
return err
143+
}
144+
145+
resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", bytes.NewBuffer(appJSON))
146+
if err == nil {
147+
resp.Close()
148+
}
149+
150+
return err
151+
}
152+
153+
// DeleteApp - deletes app by ID.
154+
//
155+
// https://wit.ai/docs/http/20200513/#delete__apps__app_link
113156
func (c *Client) DeleteApp(id string) error {
114157
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", nil)
115158
if err == nil {
@@ -119,44 +162,170 @@ func (c *Client) DeleteApp(id string) error {
119162
return err
120163
}
121164

122-
// CreateApp - creates new app. https://wit.ai/docs/http/20170307#post__apps_link
123-
func (c *Client) CreateApp(app App) (*CreatedApp, error) {
124-
appJSON, err := json.Marshal(app)
165+
// AppTag - https://wit.ai/docs/http/20200513/#get__apps__app_tags__tag_link
166+
type AppTag struct {
167+
Name string `json:"name,omitempty"`
168+
Desc string `json:"desc,omitempty"`
169+
170+
CreatedAt Time `json:"created_at,omitempty"`
171+
UpdatedAt Time `json:"updated_at,omitempty"`
172+
}
173+
174+
// GetAppTags - Returns an array of all tag groups for an app.
175+
// Within a group, all tags point to the same app state (as a result of moving tags).
176+
//
177+
// https://wit.ai/docs/http/20200513/#get__apps__app_tags_link
178+
func (c *Client) GetAppTags(appID string) ([][]AppTag, error) {
179+
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags", url.PathEscape(appID)), "application/json", nil)
125180
if err != nil {
126181
return nil, err
127182
}
128183

129-
resp, err := c.request(http.MethodPost, "/apps", "application/json", bytes.NewBuffer(appJSON))
184+
defer resp.Close()
185+
186+
var tags [][]AppTag
187+
decoder := json.NewDecoder(resp)
188+
err = decoder.Decode(&tags)
189+
return tags, err
190+
}
191+
192+
// GetAppTag - returns the detail of the specified tag.
193+
//
194+
// https://wit.ai/docs/http/20200513/#get__apps__app_tags__tag_link
195+
func (c *Client) GetAppTag(appID, tagID string) (*AppTag, error) {
196+
resp, err := c.request(http.MethodGet, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
130197
if err != nil {
131198
return nil, err
132199
}
133200

134201
defer resp.Close()
135202

136-
var createdApp *CreatedApp
203+
var tag *AppTag
137204
decoder := json.NewDecoder(resp)
138-
err = decoder.Decode(&createdApp)
205+
err = decoder.Decode(&tag)
206+
return tag, err
207+
}
139208

140-
return createdApp, err
209+
// CreateAppTag - Take a snapshot of the current app state, save it as a tag (version)
210+
// of the app. The name of the tag created will be returned in the response.
211+
//
212+
// https://wit.ai/docs/http/20200513/#post__apps__app_tags_link
213+
func (c *Client) CreateAppTag(appID string, tag string) (*AppTag, error) {
214+
type appTag struct {
215+
Tag string `json:"tag"`
216+
}
217+
218+
tagJSON, err := json.Marshal(appTag{Tag: tag})
219+
if err != nil {
220+
return nil, err
221+
}
222+
223+
resp, err := c.request(http.MethodPost, fmt.Sprintf("/apps/%s/tags", url.PathEscape(tag)), "application/json", bytes.NewBuffer(tagJSON))
224+
if err != nil {
225+
return nil, err
226+
}
227+
228+
defer resp.Close()
229+
230+
// theresponse format is different than the one in get API.
231+
var tmp appTag
232+
decoder := json.NewDecoder(resp)
233+
if err := decoder.Decode(&tmp); err != nil {
234+
return nil, err
235+
}
236+
237+
return &AppTag{Name: tmp.Tag}, nil
141238
}
142239

143-
// UpdateApp - Updates an app. https://wit.ai/docs/http/20170307#put__apps__app_id_link
144-
func (c *Client) UpdateApp(id string, app App) (*App, error) {
145-
appJSON, err := json.Marshal(app)
240+
// UpdateAppTagRequest - https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
241+
type UpdateAppTagRequest struct {
242+
Tag string `json:"tag,omitempty"`
243+
Desc string `json:"desc,omitempty"`
244+
MoveTo string `json:"move_to,omitempty"`
245+
}
246+
247+
// UpdateAppTagResponse - https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
248+
type UpdateAppTagResponse struct {
249+
Tag string `json:"tag,omitempty"`
250+
Desc string `json:"desc,omitempty"`
251+
MovedTo string `json:"moved_to,omitempty"`
252+
}
253+
254+
// UpdateAppTag - Update the tag's name or description
255+
//
256+
// https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
257+
func (c *Client) UpdateAppTag(appID, tagID string, updated AppTag) (*AppTag, error) {
258+
type tag struct {
259+
Tag string `json:"tag,omitempty"`
260+
Desc string `json:"desc,omitempty"`
261+
}
262+
263+
updateJSON, err := json.Marshal(tag{Tag: updated.Name, Desc: updated.Desc})
146264
if err != nil {
147265
return nil, err
148266
}
149267

150-
resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s", url.PathEscape(id)), "application/json", bytes.NewBuffer(appJSON))
268+
resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", bytes.NewBuffer(updateJSON))
269+
if err != nil {
270+
return nil, err
271+
}
272+
273+
defer resp.Close()
274+
275+
var tagResp tag
276+
decoder := json.NewDecoder(resp)
277+
err = decoder.Decode(&tagResp)
278+
return &AppTag{Name: tagResp.Tag, Desc: tagResp.Desc}, err
279+
}
280+
281+
type MovedAppTag struct {
282+
Tag string `json:"tag"`
283+
Desc string `json:"desc"`
284+
MovedTo string `json:"moved_to"`
285+
}
286+
287+
// MoveAppTag - move the tag to point to another tag.
288+
//
289+
// https://wit.ai/docs/http/20200513/#put__apps__app_tags__tag_link
290+
func (c *Client) MoveAppTag(appID, tagID string, to string, updated *AppTag) (*MovedAppTag, error) {
291+
type tag struct {
292+
Tag string `json:"tag,omitempty"`
293+
Desc string `json:"desc,omitempty"`
294+
MoveTo string `json:"move_to,omitempty"`
295+
}
296+
297+
updateReq := tag{MoveTo: to}
298+
if updated != nil {
299+
updateReq.Tag = updated.Name
300+
updateReq.Desc = updated.Desc
301+
}
302+
303+
updateJSON, err := json.Marshal(updateReq)
304+
if err != nil {
305+
return nil, err
306+
}
307+
308+
resp, err := c.request(http.MethodPut, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", bytes.NewBuffer(updateJSON))
151309
if err != nil {
152310
return nil, err
153311
}
154312

155313
defer resp.Close()
156314

157-
var updatedApp *App
315+
var tagResp *MovedAppTag
158316
decoder := json.NewDecoder(resp)
159-
err = decoder.Decode(&updatedApp)
317+
err = decoder.Decode(&tagResp)
318+
return tagResp, err
319+
}
320+
321+
// DeleteAppTag - Permanently delete the tag.
322+
//
323+
// https://wit.ai/docs/http/20200513/#delete__apps__app_tags__tag_link
324+
func (c *Client) DeleteAppTag(appID, tagID string) error {
325+
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/apps/%s/tags/%s", url.PathEscape(appID), url.PathEscape(tagID)), "application/json", nil)
326+
if err == nil {
327+
resp.Close()
328+
}
160329

161-
return updatedApp, err
330+
return err
162331
}

0 commit comments

Comments
 (0)