forked from wit-ai/wit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
189 lines (152 loc) · 5.8 KB
/
entity.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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Entity - https://wit.ai/docs/http/20170307#post__entities_link
type Entity struct {
ID string `json:"id"`
Doc string `json:"doc"`
Name string `json:"name,omitempty"`
Lang string `json:"lang,omitempty"`
Builtin bool `json:"builtin,omitempty"`
Lookups []string `json:"lookups,omitempty"`
Values []EntityValue `json:"values,omitempty"`
}
// EntityValue - https://wit.ai/docs/http/20170307#get__entities__entity_id_link
type EntityValue struct {
Value string `json:"value"`
Expressions []string `json:"expressions"`
MetaData string `json:"metadata"`
}
// GetEntities - returns list of entities. https://wit.ai/docs/http/20170307#get__entities_link
func (c *Client) GetEntities() ([]string, error) {
resp, err := c.request(http.MethodGet, "/entities", "application/json", nil)
if err != nil {
return []string{}, err
}
defer resp.Close()
var entities []string
decoder := json.NewDecoder(resp)
err = decoder.Decode(&entities)
return entities, err
}
// CreateEntity - Creates a new entity with the given attributes. https://wit.ai/docs/http/20170307#post__entities_link
func (c *Client) CreateEntity(entity Entity) (*Entity, error) {
entityJSON, err := json.Marshal(entity)
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, "/entities", "application/json", bytes.NewBuffer(entityJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var entityResp *Entity
decoder := json.NewDecoder(resp)
err = decoder.Decode(&entityResp)
return entityResp, err
}
// GetEntity - returns entity by ID. https://wit.ai/docs/http/20170307#get__entities__entity_id_link
func (c *Client) GetEntity(id string) (*Entity, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/entities/%s", url.PathEscape(id)), "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var entity *Entity
decoder := json.NewDecoder(resp)
err = decoder.Decode(&entity)
return entity, err
}
// DeleteEntity - deletes entity by ID. https://wit.ai/docs/http/20170307#delete__entities__entity_id_link
func (c *Client) DeleteEntity(id string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s", url.PathEscape(id)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}
// DeleteEntityRole - deletes entity role. https://wit.ai/docs/http/20170307#delete__entities__entity_id_role_id_link
func (c *Client) DeleteEntityRole(entityID string, role string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s:%s", url.PathEscape(entityID), url.PathEscape(role)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}
// UpdateEntity - Updates an entity. https://wit.ai/docs/http/20170307#put__entities__entity_id_link
func (c *Client) UpdateEntity(id string, entity Entity) error {
entityJSON, err := json.Marshal(entity)
if err != nil {
return err
}
resp, err := c.request(http.MethodPut, fmt.Sprintf("/entities/%s", url.PathEscape(id)), "application/json", bytes.NewBuffer(entityJSON))
if err != nil {
return err
}
defer resp.Close()
decoder := json.NewDecoder(resp)
return decoder.Decode(&entity)
}
// AddEntityValue - Add a possible value into the list of values for the keyword entity. https://wit.ai/docs/http/20170307#post__entities__entity_id_values_link
func (c *Client) AddEntityValue(entityID string, value EntityValue) (*Entity, error) {
valueJSON, err := json.Marshal(value)
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, fmt.Sprintf("/entities/%s/values", url.PathEscape(entityID)), "application/json", bytes.NewBuffer(valueJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var entityResp *Entity
decoder := json.NewDecoder(resp)
if err = decoder.Decode(&entityResp); err != nil {
return nil, err
}
return entityResp, nil
}
// DeleteEntityValue - Delete a canonical value from the entity. https://wit.ai/docs/http/20170307#delete__entities__entity_id_values_link
func (c *Client) DeleteEntityValue(entityID string, value string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s/values/%s", url.PathEscape(entityID), url.PathEscape(value)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}
// AddEntityValueExpression - Create a new expression of the canonical value of the keyword entity. https://wit.ai/docs/http/20170307#post__entities__entity_id_values__value_id_expressions_link
func (c *Client) AddEntityValueExpression(entityID string, value string, expression string) (*Entity, error) {
type expr struct {
Expression string `json:"expression"`
}
exprJSON, err := json.Marshal(expr{
Expression: expression,
})
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, fmt.Sprintf("/entities/%s/values/%s/expressions", url.PathEscape(entityID), url.PathEscape(value)), "application/json", bytes.NewBuffer(exprJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var entityResp *Entity
decoder := json.NewDecoder(resp)
if err = decoder.Decode(&entityResp); err != nil {
return nil, err
}
return entityResp, nil
}
// DeleteEntityValueExpression - Delete an expression of the canonical value of the entity. https://wit.ai/docs/http/20170307#delete__entities__entity_id_values__value_id_expressions_link
func (c *Client) DeleteEntityValueExpression(entityID string, value string, expression string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/entities/%s/values/%s/expressions/%s", url.PathEscape(entityID), url.PathEscape(value), url.PathEscape(expression)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}