-
Notifications
You must be signed in to change notification settings - Fork 31
/
intent.go
85 lines (70 loc) · 2 KB
/
intent.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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Intent - represents a wit-ai intent.
//
// https://wit.ai/docs/http/#get__intents_link
type Intent struct {
ID string `json:"id"`
Name string `json:"name"`
Entities []Entity `json:"entities,omitempty"`
}
// GetIntents - returns the list of intents.
//
// https://wit.ai/docs/http/#get__intents_link
func (c *Client) GetIntents() ([]Intent, error) {
resp, err := c.request(http.MethodGet, "/intents", "application/json", nil)
if err != nil {
return []Intent{}, err
}
defer resp.Close()
var intents []Intent
err = json.NewDecoder(resp).Decode(&intents)
return intents, err
}
// CreateIntent - creates a new intent with the given name.
//
// https://wit.ai/docs/http/#post__intents_link
func (c *Client) CreateIntent(name string) (*Intent, error) {
intentJSON, err := json.Marshal(Intent{Name: name})
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, "/intents", "application/json", bytes.NewBuffer(intentJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var intentResp *Intent
err = json.NewDecoder(resp).Decode(&intentResp)
return intentResp, err
}
// GetIntent - returns intent by name.
//
// https://wit.ai/docs/http/#get__intents__intent_link
func (c *Client) GetIntent(name string) (*Intent, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/intents/%s", url.PathEscape(name)), "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var intent *Intent
err = json.NewDecoder(resp).Decode(&intent)
return intent, err
}
// DeleteIntent - permanently deletes an intent by name.
//
// https://wit.ai/docs/http/#delete__intents__intent_link
func (c *Client) DeleteIntent(name string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/intents/%s", url.PathEscape(name)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}