-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
179 lines (147 loc) · 3.61 KB
/
client.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
package gowit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
// "log"
"net/http"
"net/url"
"strings"
)
const (
APIEndpoint = "https://api.wit.ai"
APIVersion = "20170121"
TraitLookup = "trait"
KeywordsLookup = "keywords"
)
var apiToken string
// Client represents a client for the Wit.AI API
type Client struct {}
type param struct {
Method string
Path string
ContentType string
Data []byte
}
func NewClient(token string) *Client {
apiToken = token
return &Client{}
}
func (c *Client) Detect(text string) (Meaning, error) {
var p param
p.Method = "GET"
p.Path = "/message?q=" + url.QueryEscape(text)
var m Meaning
res, err := request(&p)
if err != nil {
return m, err
}
if err := json.Unmarshal(res, &m); err != nil {
fmt.Println("Failed to unmarshal response: ", string(res))
return m, err
}
return m, nil
}
// ListEntities returns a list of available entities for the app
// TODO: Should we return []Entity or just []string?
func (c *Client) ListEntities() ([]Entity, error) {
var p param
p.Method = "GET"
p.Path = "/entities"
res, err := request(&p)
if err != nil {
return nil, err
}
var entityNames []string
if err := json.Unmarshal(res, &entityNames); err != nil {
return nil, fmt.Errorf("Failed to unmarshal: %s. Data: %s", err.Error(), string(res))
}
var entities []Entity
for _, n := range entityNames {
var e Entity
e.Name = n
entities = append(entities, e)
}
return entities, nil
}
// GetEntity returns all the expressions validated for an entity.
// Wit.AI currently limits to the first 1000 values (with the first 50 expressions)
func (c *Client) GetEntity(id string) (e Entity, err error) {
var p param
p.Method = "GET"
p.Path = "/entities/" + id
res, err := request(&p)
if err != nil {
return e, err
}
return parseEntity(res)
}
//Add a new expression for 'intent'
func(c *Client) AddExpression(intent, expression string) error {
var p param
p.Method = "POST"
p.Path = "/entities/intent/values/" + intent + "/expressions"
p.Data = []byte(fmt.Sprintf(`{"expression": "%s"}`, expression))
_, err := request(&p)
return err
}
// UpdateEntity updates an entity with the given attributes.
func (c *Client) UpdateEntity(e *Entity) error {
data, err := json.Marshal(e)
if err != nil {
return fmt.Errorf("Failed to unmarshal entity: %s", err.Error())
}
var p param
p.Method = "PUT"
p.Path = "/entities/" + e.Name
p.Data = data
_, err = request(&p)
return err
}
// func (c *Client) DeleteAllValue
func deleteValue(e *Entity, v *Value) error {
var p param
p.Method = "DELETE"
p.Path = "/entities/" + e.Name + "/values/" + v.Name
_, err := request(&p)
return err
}
func parseEntity(data []byte) (Entity, error) {
var e Entity
if err := json.Unmarshal(data, &e); err != nil {
return e, fmt.Errorf("Failed to unmarshal: %s. Data: %s", err.Error(), string(data))
}
return e, nil
}
func request(p *param) ([]byte, error) {
if strings.Contains(p.Path, `?`) {
p.Path += "&v=" + APIVersion
} else {
p.Path += "?v=" + APIVersion
}
req, err := http.NewRequest(p.Method, APIEndpoint + p.Path, bytes.NewReader(p.Data))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer " + apiToken)
req.Header.Set("Accept", "application/json")
if p.ContentType != "" {
req.Header.Set("Content-Type", p.ContentType)
}
c := &http.Client{}
res, err := c.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, errors.New(http.StatusText(res.StatusCode) + ": " + string(body))
}
return body, nil
}