-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.go
92 lines (82 loc) · 2.69 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
package apiai
import (
"fmt"
"net/url"
)
const baseUrl = "https://api.api.ai/v1/"
const defaultVersion = "20150910"
const defaultQueryLang = "en"
const defaultSpeechLang = "en-US"
var queryLang = []string{"pt-BR", "zh-HK", "zh-CN", "zh-TW", "en", "nl", "fr", "de", "it", "ja", "ko", "pt", "ru", "es", "uk"}
var speechLang = []string{"en-US", "en-AU", "en-CA", "en-GB", "en-IN", "ru-RU", "de-DE", "es-ES", "pt-PT", "pt-BR", "zh-CN", "zh-TW", "zh-HK", "ja-JP", "fr-FR"}
type ClientConfig struct {
Token string //a9a9a9a9a9a9aa9a9a9a9a9a9a9a9a9a
Version string //YYYYMMDD
QueryLang string
SpeechLang string
ProxyURL string
}
type ApiClient struct {
config *ClientConfig
}
type Client interface {
Query(Query) (*QueryResponse, error)
Tts(text string) (string, error)
GetContext(name string) (*Context, error)
CreateContext(context Context) error
DeleteContext(name string) error
GetContexts() ([]Context, error)
DeleteContexts() error
GetEntities() ([]EntityDescription, error)
UpdateEntities(entities []Entity) error
GetEntity(idOrName string) (*Entity, error)
CreateEntity(entity Entity) (*CreationResponse, error)
UpdateEntity(idOrName string, entity Entity) error
DeleteEntity(idOrName string) error
AddEntries(idOrName string, entries []Entry) error
UpdateEntries(idOrName string, entries []Entry) error
DeleteEntries(idOrName string, entries []string) error
GetIntent(id string) (*Intent, error)
CreateIntent(intent Intent) (*CreationResponse, error)
UpdateIntent(id string, intent Intent) error
DeleteIntent(id string) error
GetIntents() ([]IntentDescription, error)
}
func NewClient(conf *ClientConfig) (*ApiClient, error) {
if conf.Token == "" {
return nil, fmt.Errorf("%v", "You have to provide a Token")
}
if conf.Version == "" {
conf.Version = defaultVersion
}
if conf.QueryLang == "" {
conf.QueryLang = defaultQueryLang
}
if conf.SpeechLang == "" {
conf.SpeechLang = defaultSpeechLang
}
if !languageAvailable(conf.QueryLang, queryLang) {
return nil, fmt.Errorf("%v", "You have to provide a valid query language, see https://docs.api.ai/docs/languages")
}
if !languageAvailable(conf.SpeechLang, speechLang) {
return nil, fmt.Errorf("%v", "You have to provide a valid speech language, see https://docs.api.ai/docs/tts#headers")
}
return &ApiClient{conf}, nil
}
func languageAvailable(inputLang string, languages []string) bool {
for _, lang := range languages {
if lang == inputLang {
return true
}
}
return false
}
func (c *ApiClient) buildUrl(endpoint string, params map[string]string) string {
u := baseUrl + endpoint + "?v=" + c.config.Version
if params != nil {
for i, v := range params {
u += "&" + i + "=" + url.QueryEscape(v)
}
}
return u
}