-
Notifications
You must be signed in to change notification settings - Fork 31
/
trait.go
141 lines (117 loc) · 3.44 KB
/
trait.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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Trait - represents a wit-ai trait.
//
// https://wit.ai/docs/http/#post__traits_link
type Trait struct {
ID string `json:"id"`
Name string `json:"name"`
Values []TraitValue `json:"values"`
}
// TraitValue - represents the value of a Trait.
//
// https://wit.ai/docs/http/#get__traits__trait_link
type TraitValue struct {
ID string `json:"id"`
Value string `json:"value"`
}
// GetTraits - returns a list of traits.
//
// https://wit.ai/docs/http/#get__traits_link
func (c *Client) GetTraits() ([]Trait, error) {
resp, err := c.request(http.MethodGet, "/traits", "application/json", nil)
if err != nil {
return []Trait{}, err
}
defer resp.Close()
var traits []Trait
decoder := json.NewDecoder(resp)
err = decoder.Decode(&traits)
return traits, err
}
// CreateTrait - creates a new trait with the given values.
//
// https://wit.ai/docs/http/#post__traits_link
func (c *Client) CreateTrait(name string, values []string) (*Trait, error) {
type trait struct {
Name string `json:"name"`
Values []string `json:"values"`
}
traitJSON, err := json.Marshal(trait{Name: name, Values: values})
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, "/traits", "application/json", bytes.NewBuffer(traitJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var traitResp *Trait
decoder := json.NewDecoder(resp)
err = decoder.Decode(&traitResp)
return traitResp, err
}
// GetTrait - returns all available information about a trait.
//
// https://wit.ai/docs/http/#get__traits__trait_link
func (c *Client) GetTrait(name string) (*Trait, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/traits/%s", url.PathEscape(name)), "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var traitResp *Trait
decoder := json.NewDecoder(resp)
err = decoder.Decode(&traitResp)
return traitResp, err
}
// DeleteTrait - permanently deletes a trait.
//
// https://wit.ai/docs/http/#delete__traits__trait_link
func (c *Client) DeleteTrait(name string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/traits/%s", url.PathEscape(name)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}
// AddTraitValue - create a new value for a trait.
//
// https://wit.ai/docs/http/#post__traits__trait_values_link
func (c *Client) AddTraitValue(traitName string, value string) (*Trait, error) {
type traitValue struct {
Value string `json:"value"`
}
valueJSON, err := json.Marshal(traitValue{Value: value})
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, fmt.Sprintf("/traits/%s/values", url.PathEscape(traitName)), "application/json", bytes.NewBuffer(valueJSON))
if err != nil {
return nil, err
}
defer resp.Close()
var traitResp *Trait
decoder := json.NewDecoder(resp)
if err = decoder.Decode(&traitResp); err != nil {
return nil, err
}
return traitResp, nil
}
// DeleteTraitValue - permanently deletes the trait value.
//
// https://wit.ai/docs/http/#delete__traits__trait_values__value_link
func (c *Client) DeleteTraitValue(traitName string, value string) error {
resp, err := c.request(http.MethodDelete, fmt.Sprintf("/traits/%s/values/%s", url.PathEscape(traitName), url.PathEscape(value)), "application/json", nil)
if err == nil {
resp.Close()
}
return err
}