|
| 1 | +/** |
| 2 | +* Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +* |
| 16 | +*/ |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Text; |
| 20 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 21 | +using IBM.Watson.DeveloperCloud.Connection; |
| 22 | +using IBM.Watson.DeveloperCloud.Logging; |
| 23 | +using MiniJSON; |
| 24 | +using System.Collections.Generic; |
| 25 | + |
| 26 | +namespace IBM.Watson.DeveloperCloud.Services.Assistant.v1 |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// This class wraps the Watson Assistant service. |
| 30 | + /// <a href="http://www.ibm.com/watson/developercloud/assistant.html">Assistant Service</a> |
| 31 | + /// </summary> |
| 32 | + public class Assistant : IWatsonService |
| 33 | + { |
| 34 | + #region Public Types |
| 35 | + #endregion |
| 36 | + |
| 37 | + #region Public Properties |
| 38 | + /// <summary> |
| 39 | + /// Gets and sets the endpoint URL for the service. |
| 40 | + /// </summary> |
| 41 | + public string Url |
| 42 | + { |
| 43 | + get { return _url; } |
| 44 | + set { _url = value; } |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets and sets the versionDate of the service. |
| 49 | + /// </summary> |
| 50 | + public string VersionDate |
| 51 | + { |
| 52 | + get |
| 53 | + { |
| 54 | + if (string.IsNullOrEmpty(_versionDate)) |
| 55 | + throw new ArgumentNullException("VersionDate cannot be null. Use VersionDate `2017-05-26`"); |
| 56 | + |
| 57 | + return _versionDate; |
| 58 | + } |
| 59 | + set { _versionDate = value; } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets and sets the credentials of the service. Replace the default endpoint if endpoint is defined. |
| 64 | + /// </summary> |
| 65 | + public Credentials Credentials |
| 66 | + { |
| 67 | + get { return _credentials; } |
| 68 | + set |
| 69 | + { |
| 70 | + _credentials = value; |
| 71 | + if (!string.IsNullOrEmpty(_credentials.Url)) |
| 72 | + { |
| 73 | + _url = _credentials.Url; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + #endregion |
| 78 | + |
| 79 | + #region Private Data |
| 80 | + private const string ServiceId = "AssistantV1"; |
| 81 | + private const string Workspaces = "/v1/workspaces"; |
| 82 | + private Credentials _credentials = null; |
| 83 | + private string _url = "https://gateway.watsonplatform.net/assistant/api"; |
| 84 | + private string _versionDate; |
| 85 | + #endregion |
| 86 | + |
| 87 | + #region Constructor |
| 88 | + /// <summary> |
| 89 | + /// Assistant constructor |
| 90 | + /// </summary> |
| 91 | + /// <param name="credentials">The service credentials</param> |
| 92 | + public Assistant(Credentials credentials) |
| 93 | + { |
| 94 | + if (credentials.HasCredentials() || credentials.HasAuthorizationToken()) |
| 95 | + { |
| 96 | + Credentials = credentials; |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + throw new WatsonException("Please provide a username and password or authorization token to use the Assistant service. For more information, see https://github.com/watson-developer-cloud/unity-sdk/#configuring-your-service-credentials"); |
| 101 | + } |
| 102 | + } |
| 103 | + #endregion |
| 104 | + |
| 105 | + #region Callback delegates |
| 106 | + /// <summary> |
| 107 | + /// Success callback delegate. |
| 108 | + /// </summary> |
| 109 | + /// <typeparam name="T">Type of the returned object.</typeparam> |
| 110 | + /// <param name="response">The returned object.</param> |
| 111 | + /// <param name="customData">user defined custom data including raw json.</param> |
| 112 | + public delegate void SuccessCallback<T>(T response, Dictionary<string, object> customData); |
| 113 | + /// <summary> |
| 114 | + /// Fail callback delegate. |
| 115 | + /// </summary> |
| 116 | + /// <param name="error">The error object.</param> |
| 117 | + /// <param name="customData">User defined custom data</param> |
| 118 | + public delegate void FailCallback(RESTConnector.Error error, Dictionary<string, object> customData); |
| 119 | + #endregion |
| 120 | + |
| 121 | + #region Message |
| 122 | + /// <summary> |
| 123 | + /// Message the specified workspaceId, input and callback. |
| 124 | + /// </summary> |
| 125 | + /// <param name="successCallback">The success callback.</param> |
| 126 | + /// <param name="failCallback">The fail callback.</param> |
| 127 | + /// <param name="workspaceID">Workspace identifier.</param> |
| 128 | + /// <param name="input">Input.</param> |
| 129 | + /// <param name="customData">Custom data.</param> |
| 130 | + public bool Message(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceID, string input, Dictionary<string, object> customData = null) |
| 131 | + { |
| 132 | + //if (string.IsNullOrEmpty(workspaceID)) |
| 133 | + // throw new ArgumentNullException("workspaceId"); |
| 134 | + if (successCallback == null) |
| 135 | + throw new ArgumentNullException("successCallback"); |
| 136 | + if (failCallback == null) |
| 137 | + throw new ArgumentNullException("failCallback"); |
| 138 | + |
| 139 | + RESTConnector connector = RESTConnector.GetConnector(Credentials, Workspaces); |
| 140 | + if (connector == null) |
| 141 | + return false; |
| 142 | + |
| 143 | + string reqJson = "{{\"input\": {{\"text\": \"{0}\"}}}}"; |
| 144 | + string reqString = string.Format(reqJson, input); |
| 145 | + |
| 146 | + MessageReq req = new MessageReq(); |
| 147 | + req.SuccessCallback = successCallback; |
| 148 | + req.FailCallback = failCallback; |
| 149 | + req.Headers["Content-Type"] = "application/json"; |
| 150 | + req.Headers["Accept"] = "application/json"; |
| 151 | + req.Parameters["version"] = VersionDate; |
| 152 | + req.Function = "/" + workspaceID + "/message"; |
| 153 | + req.Send = Encoding.UTF8.GetBytes(reqString); |
| 154 | + req.OnResponse = MessageResp; |
| 155 | + req.CustomData = customData == null ? new Dictionary<string, object>() : customData; |
| 156 | + |
| 157 | + return connector.Send(req); |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Message the specified workspaceId, input and callback. |
| 162 | + /// </summary> |
| 163 | + /// <param name="successCallback">The success callback.</param> |
| 164 | + /// <param name="failCallback">The fail callback.</param> |
| 165 | + /// <param name="workspaceID">Workspace identifier.</param> |
| 166 | + /// <param name="messageRequest">Message request object.</param> |
| 167 | + /// <param name="customData">Custom data.</param> |
| 168 | + /// <returns></returns> |
| 169 | + public bool Message(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceID, MessageRequest messageRequest, Dictionary<string, object> customData = null) |
| 170 | + { |
| 171 | + if (string.IsNullOrEmpty(workspaceID)) |
| 172 | + throw new ArgumentNullException("workspaceId"); |
| 173 | + if (successCallback == null) |
| 174 | + throw new ArgumentNullException("successCallback"); |
| 175 | + if (failCallback == null) |
| 176 | + throw new ArgumentNullException("failCallback"); |
| 177 | + |
| 178 | + RESTConnector connector = RESTConnector.GetConnector(Credentials, Workspaces); |
| 179 | + if (connector == null) |
| 180 | + return false; |
| 181 | + |
| 182 | + IDictionary<string, string> requestDict = new Dictionary<string, string>(); |
| 183 | + if (messageRequest.context != null) |
| 184 | + requestDict.Add("context", Json.Serialize(messageRequest.context)); |
| 185 | + if (messageRequest.input != null) |
| 186 | + requestDict.Add("input", Json.Serialize(messageRequest.input)); |
| 187 | + requestDict.Add("alternate_intents", Json.Serialize(messageRequest.alternate_intents)); |
| 188 | + if (messageRequest.entities != null) |
| 189 | + requestDict.Add("entities", Json.Serialize(messageRequest.entities)); |
| 190 | + if (messageRequest.intents != null) |
| 191 | + requestDict.Add("intents", Json.Serialize(messageRequest.intents)); |
| 192 | + if (messageRequest.output != null) |
| 193 | + requestDict.Add("output", Json.Serialize(messageRequest.output)); |
| 194 | + |
| 195 | + int iterator = 0; |
| 196 | + StringBuilder stringBuilder = new StringBuilder("{"); |
| 197 | + foreach(KeyValuePair<string, string> property in requestDict) |
| 198 | + { |
| 199 | + string delimeter = iterator < requestDict.Count - 1 ? "," : ""; |
| 200 | + stringBuilder.Append(string.Format("\"{0}\": {1}{2}", property.Key, property.Value, delimeter)); |
| 201 | + iterator++; |
| 202 | + } |
| 203 | + stringBuilder.Append("}"); |
| 204 | + |
| 205 | + string stringToSend = stringBuilder.ToString(); |
| 206 | + |
| 207 | + MessageReq req = new MessageReq(); |
| 208 | + req.SuccessCallback = successCallback; |
| 209 | + req.FailCallback = failCallback; |
| 210 | + req.Headers["Content-Type"] = "application/json"; |
| 211 | + req.Headers["Accept"] = "application/json"; |
| 212 | + req.Parameters["version"] = VersionDate; |
| 213 | + req.Function = "/" + workspaceID + "/message"; |
| 214 | + req.Send = Encoding.UTF8.GetBytes(stringToSend); |
| 215 | + req.OnResponse = MessageResp; |
| 216 | + req.CustomData = customData == null ? new Dictionary<string, object>() : customData; |
| 217 | + |
| 218 | + return connector.Send(req); |
| 219 | + } |
| 220 | + |
| 221 | + |
| 222 | + private class MessageReq : RESTConnector.Request |
| 223 | + { |
| 224 | + /// <summary> |
| 225 | + /// The success callback. |
| 226 | + /// </summary> |
| 227 | + public SuccessCallback<object> SuccessCallback { get; set; } |
| 228 | + /// <summary> |
| 229 | + /// The fail callback. |
| 230 | + /// </summary> |
| 231 | + public FailCallback FailCallback { get; set; } |
| 232 | + /// <summary> |
| 233 | + /// Custom data. |
| 234 | + /// </summary> |
| 235 | + public Dictionary<string, object> CustomData { get; set; } |
| 236 | + } |
| 237 | + |
| 238 | + private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp) |
| 239 | + { |
| 240 | + object result = null; |
| 241 | + string data = ""; |
| 242 | + Dictionary<string, object> customData = ((MessageReq)req).CustomData; |
| 243 | + |
| 244 | + if (resp.Success) |
| 245 | + { |
| 246 | + try |
| 247 | + { |
| 248 | + // For deserializing into a generic object |
| 249 | + data = Encoding.UTF8.GetString(resp.Data); |
| 250 | + result = Json.Deserialize(data); |
| 251 | + customData.Add("json", data); |
| 252 | + } |
| 253 | + catch (Exception e) |
| 254 | + { |
| 255 | + Log.Error("Assistant.MessageResp()", "MessageResp Exception: {0}", e.ToString()); |
| 256 | + data = e.Message; |
| 257 | + resp.Success = false; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + if (resp.Success) |
| 262 | + { |
| 263 | + if (((MessageReq)req).SuccessCallback != null) |
| 264 | + ((MessageReq)req).SuccessCallback(result, customData); |
| 265 | + } |
| 266 | + else |
| 267 | + { |
| 268 | + if (((MessageReq)req).FailCallback != null) |
| 269 | + ((MessageReq)req).FailCallback(resp.Error, customData); |
| 270 | + } |
| 271 | + } |
| 272 | + #endregion |
| 273 | + |
| 274 | + #region Workspaces |
| 275 | + #endregion |
| 276 | + |
| 277 | + #region Intents |
| 278 | + #endregion |
| 279 | + |
| 280 | + #region Examples |
| 281 | + #endregion |
| 282 | + |
| 283 | + #region Entities |
| 284 | + #endregion |
| 285 | + |
| 286 | + #region Values |
| 287 | + #endregion |
| 288 | + |
| 289 | + #region Synonyms |
| 290 | + #endregion |
| 291 | + |
| 292 | + #region Dialog Nodes |
| 293 | + #endregion |
| 294 | + |
| 295 | + #region Logs |
| 296 | + #endregion |
| 297 | + |
| 298 | + #region Counter Examples |
| 299 | + #endregion |
| 300 | + |
| 301 | + #region IWatsonService implementation |
| 302 | + /// <exclude /> |
| 303 | + public string GetServiceID() |
| 304 | + { |
| 305 | + return ServiceId; |
| 306 | + } |
| 307 | + #endregion |
| 308 | + } |
| 309 | +} |
0 commit comments