This repository was archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathAIConfiguration.java
More file actions
303 lines (265 loc) · 9.61 KB
/
AIConfiguration.java
File metadata and controls
303 lines (265 loc) · 9.61 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package ai.api;
/***********************************************************************************************************************
*
* API.AI Java SDK - client-side libraries for API.AI
* =================================================
*
* Copyright (C) 2017 by Speaktoit, Inc. (https://www.speaktoit.com) https://www.api.ai
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
***********************************************************************************************************************/
import java.io.UnsupportedEncodingException;
/***********************************************************************************************************************
*
* API.AI Java SDK - client-side libraries for API.AI
* =================================================
*
* Copyright (C) 2015 by Speaktoit, Inc. (https://www.speaktoit.com) https://www.api.ai
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
***********************************************************************************************************************/
import java.net.Proxy;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import ai.api.util.StringUtils;
/**
* AI service configuration
*/
public class AIConfiguration implements Cloneable {
private static final String SERVICE_PROD_URL = "https://api.api.ai/v1/";
protected static final String CURRENT_PROTOCOL_VERSION = "20150910";
protected static final String QUESTION_ENDPOINT = "query";
protected static final String USER_ENTITIES_ENDPOINT = "userEntities";
protected static final String ENTITIES_ENDPOINT = "entities";
protected static final String CONTEXTS_ENDPOINT = "contexts";
private final String apiKey;
private final SupportedLanguages language;
private String serviceUrl;
/**
* Protocol version used for api queries. Can be changed if old protocol version required.
*
* See https://docs.api.ai/v20/docs/versioning for details
*/
private String protocolVersion;
private boolean writeSoundLog = false;
private Proxy proxy;
/**
* Create configuration with given client access token and language.
*
* See https://docs.api.ai/v20/docs/authentication for details
*
* @param clientAccessToken An agent unique key. Cannot be <code>null</code>
* @param language An agent language
*/
public AIConfiguration(final String clientAccessToken, final SupportedLanguages language) {
if (clientAccessToken == null) {
throw new IllegalArgumentException("clientAccessToken");
}
this.apiKey = clientAccessToken;
this.language = language != null ? language : SupportedLanguages.DEFAULT;
protocolVersion = CURRENT_PROTOCOL_VERSION;
serviceUrl = SERVICE_PROD_URL;
}
/**
* Create configuration with given client access token.
*
* See https://docs.api.ai/v20/docs/authentication for details
*
* @param clientAccessToken An agent unique key. Cannot be <code>null</code>
*/
public AIConfiguration(final String clientAccessToken) {
this(clientAccessToken, null);
}
/**
* Get client access key
*/
public String getApiKey() {
return apiKey;
}
/**
* Get client agent language
*
* @return Never <code>null</code>
*/
public String getLanguage() {
return language.languageTag;
}
/**
* Get api.ai agent language
*
* @return Never <code>null</code>
*/
public String getApiAiLanguage() {
return language.apiaiLanguage;
}
/**
* This flag is for testing purposes ONLY. Don't change it.
*
* @param writeSoundLog value, indicating recorded sound will be saved in storage (if possible)
*/
public void setWriteSoundLog(final boolean writeSoundLog) {
this.writeSoundLog = writeSoundLog;
}
/**
* This flag is for testing purposes ONLY. Don't use it in your code.
*
* @return value, indicating recorded sound will be saved in storage (if possible)
*/
public boolean isWriteSoundLog() {
return writeSoundLog;
}
/**
* Check list of supported protocol versions on the api.ai website.
*
* @return protocol version in YYYYMMDD format
*/
public String getProtocolVersion() {
return protocolVersion;
}
/**
* Set protocol version for API queries. Must be in YYYYMMDD format. This option for special cases
* only, should not be used in usual cases.
*
* @param protocolVersion Protocol version in YYYYMMDD format or empty string for the oldest
* version. Check list of supported protocol versions on the api.ai website.
*/
public void setProtocolVersion(final String protocolVersion) {
this.protocolVersion = protocolVersion;
}
/**
* Set API service url. Used primarily for test requests.
*/
public void setServiceUrl(final String serviceUrl) {
this.serviceUrl = serviceUrl;
}
/**
* Get connection proxy information. If <code>null</code> then direct connection would be used.
*/
public Proxy getProxy() {
return proxy;
}
/**
* Set connection proxy information.
*
* @param proxy If <code>null</code> then direct connection would be used.
*/
public void setProxy(final Proxy proxy) {
this.proxy = proxy;
}
/**
* Clone the configuration
*/
public AIConfiguration clone() {
try {
AIConfiguration result = (AIConfiguration) super.clone();
return result;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
String getQuestionUrl(final String sessionId) {
return formatEndpoint(sessionId, protocolVersion, serviceUrl, QUESTION_ENDPOINT);
}
String getUserEntitiesEndpoint(final String sessionId) {
return formatEndpoint(sessionId, protocolVersion, serviceUrl, USER_ENTITIES_ENDPOINT);
}
String getEntitiesEndpoint(final String sessionId) {
return formatEndpoint(sessionId, protocolVersion, serviceUrl, ENTITIES_ENDPOINT);
}
private String formatEndpoint(String sessionId, String protocolVersion, String serviceUrl, String userEntitiesEndpoint) {
if (StringUtils.isEmpty(protocolVersion)) {
return String.format("%s%s?sessionId=%s", serviceUrl, userEntitiesEndpoint, sessionId);
} else {
return String.format("%s%s?v=%s&sessionId=%s", serviceUrl, userEntitiesEndpoint,
protocolVersion, sessionId);
}
}
String getContextsUrl(final String sessionId) {
return getContextsUrl(sessionId, "");
}
String getContextsUrl(final String sessionId, final String suffix) {
StringBuilder result = new StringBuilder();
result.append(serviceUrl).append(CONTEXTS_ENDPOINT);
if (!StringUtils.isEmpty(suffix)) {
try {
result.append("/").append(URLEncoder.encode(suffix, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// This is unexpected due to encoding value is defined as constant string
throw new RuntimeException(e);
}
}
result.append("?");
if (!StringUtils.isEmpty(protocolVersion)) {
result.append("v=").append(protocolVersion).append("&");
}
result.append("sessionId=").append(sessionId);
return result.toString();
}
private static Map<String, SupportedLanguages> STRING_TO_LANGUAGE = new HashMap<>();
/**
* Currently supported languages
*/
public static enum SupportedLanguages {
ChineseChina("zh-CN"),
ChineseHongKong("zh-HK"),
ChineseTaiwan("zh-TW"),
English("en"),
EnglishUS("en-US", "en"),
EnglishGB("en-GB", "en"),
Dutch("nl"),
French("fr"),
German("de"),
Italian("it"),
Japanese("ja"),
Korean("ko"),
Portuguese("pt"),
PortugueseBrazil("pt-BR"),
Russian("ru"),
Spanish("es"),
Ukrainian("uk");
/**
* Default language value
*/
public static SupportedLanguages DEFAULT = SupportedLanguages.English;
private final String languageTag;
private final String apiaiLanguage;
SupportedLanguages(final String languageTag) {
this(languageTag, languageTag);
}
SupportedLanguages(final String languageTag, final String apiaiLanguage) {
assert languageTag != null;
assert apiaiLanguage != null;
this.languageTag = languageTag;
this.apiaiLanguage = apiaiLanguage;
SupportedLanguages retValue = STRING_TO_LANGUAGE.put(languageTag, this);
assert retValue == null : "languageTag duplicates";
}
public static SupportedLanguages fromLanguageTag(final String languageTag) {
SupportedLanguages result = STRING_TO_LANGUAGE.get(languageTag);
return result != null ? result : DEFAULT;
}
}
}