forked from jsgoecke/go-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.go
187 lines (169 loc) · 4.93 KB
/
entities.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
// Copyright (c) 2014 Jason Goecke
// entities.go
package wit
import (
"encoding/json"
"net/url"
"strings"
)
// Represents an Entity for the Wit API (https://wit.ai/docs/api#toc_15)
type Entity struct {
Builtin bool `json:"builtin"`
Doc string `json:"doc"`
Id string `json:"id"`
Name string `json:"name"`
Values []EntityValue `json:"values"`
}
// Represents a Value within an Entity
type EntityValue struct {
Value string `json:"value"`
Expressions []string `json:"expressions"`
}
type Expression struct {
Expression string `json:"expression"`
}
// Represents a slice of entites when returend as an array (https://wit.ai/docs/api#toc_15)
type Entities []string
// Creates a new entity (https://wit.ai/docs/api#toc_19)
//
// result, err := client.CreateEntity(entity)
func (client *WitClient) CreateEntity(entity *Entity) (*Entity, error) {
data, _ := json.Marshal(entity)
result, err := post(client.ApiBase+"/entities", data)
if err != nil {
return nil, err
}
entity = &Entity{}
err = json.Unmarshal(result, entity)
return entity, nil
}
// Creates a new entity value (https://wit.ai/docs/api#toc_25)
//
// result, err := client.CreateEntityValue("favorite_city, entityValue)
func (client *WitClient) CreateEntityValue(id string, entityValue *EntityValue) (*Entity, error) {
data, _ := json.Marshal(entityValue)
result, err := post(client.ApiBase+"/entities/"+id+"/values", data)
if err != nil {
return nil, err
}
entity := &Entity{}
err = json.Unmarshal(result, entity)
if err != nil {
return nil, err
}
return entity, nil
}
// Creates a new entity value expression (https://wit.ai/docs/api#toc_25)
//
// result, err := client.CreateEntityValueExp("favorite_city", "Barcelona", "Paella")
func (client *WitClient) CreateEntityValueExp(id string, value string, exp string) (*Entity, error) {
jsonData, _ := json.Marshal(&Expression{exp})
result, err := post(client.ApiBase+"/entities/"+id+"/values/"+value+"/expressions", jsonData)
if err != nil {
return nil, err
}
entity := &Entity{}
err = json.Unmarshal(result, entity)
if err != nil {
return nil, err
}
return entity, nil
}
// Deletes an entity (https://wit.ai/docs/api#toc_30)
//
// result, err := client.DeleteEntity("favorite_city")
func (client *WitClient) DeleteEntity(id string) error {
id = url.QueryEscape(id)
_, err := delete(client.ApiBase+"/entities", id)
if err != nil {
return err
}
return nil
}
// Deletes an entity's value (https://wit.ai/docs/api#toc_25)
//
// result, err := client.DeleteEntityValue("favorite_city", "Paris")
func (client *WitClient) DeleteEntityValue(id string, value string) ([]byte, error) {
id = url.QueryEscape(id)
result, err := delete(client.ApiBase+"/entities", id+"/values/"+value)
if err != nil {
return nil, err
}
return result, nil
}
// Deletes an entity's value's expression (https://wit.ai/docs/api#toc_35)
//
// result, err := client.DeleteEntityValueExp("favorite_city", "Paris", "")
func (client *WitClient) DeleteEntityValueExp(id string, value string, exp string) ([]byte, error) {
id = url.QueryEscape(id)
exp = strings.Replace(url.QueryEscape(exp), "+", "%20", -1)
result, err := delete(client.ApiBase+"/entities", id+"/values/"+value+"/expressions/"+exp)
if err != nil {
return nil, err
}
return result, nil
}
// Lists the configured entities (https://wit.ai/docs/api#toc_15)
//
// result, err := client.Entities()
func (client *WitClient) Entities() (*Entities, error) {
result, err := get(client.ApiBase + "/entities")
if err != nil {
return nil, err
}
entities, _ := parseEntities(result)
return entities, nil
}
// Lists a single configured entity (https://wit.ai/docs/api#toc_17)
//
// result, err := client.Entity("wit$temperature")
func (client *WitClient) Entity(id string) (*Entity, error) {
id = url.QueryEscape(id)
result, err := get(client.ApiBase + "/entities/" + id)
if err != nil {
return nil, err
}
entity, err := parseEntity(result)
if err != nil {
return nil, err
}
return entity, nil
}
// Updates and entity (https://wit.ai/docs/api#toc_22)
//
// result, err := client.UpdateEntity(entity)
func (client *WitClient) UpdateEntity(entity *Entity) ([]byte, error) {
data, err := json.Marshal(entity)
result, err := put(client.ApiBase+"/entities/"+entity.Id, data)
if err != nil {
return nil, err
}
return result, nil
}
// Parses the Entities JSON
func parseEntities(data []byte) (*Entities, error) {
entities := &Entities{}
err := json.Unmarshal(data, entities)
if err != nil {
return nil, err
}
return entities, nil
}
// Parses the Entity JSON
func parseEntity(data []byte) (*Entity, error) {
entity := &Entity{}
err := json.Unmarshal(data, entity)
if err != nil {
return nil, err
}
return entity, nil
}
// Parses the Entities Value JSON
func parseEntityValue(data []byte) (*EntityValue, error) {
entityValue := &EntityValue{}
err := json.Unmarshal(data, entityValue)
if err != nil {
return nil, err
}
return entityValue, nil
}