|
| 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 System.Collections.Generic; |
| 21 | +using FullSerializer; |
| 22 | +using MiniJSON; |
| 23 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 24 | +using IBM.Watson.DeveloperCloud.Connection; |
| 25 | +using IBM.Watson.DeveloperCloud.Logging; |
| 26 | + |
| 27 | +namespace IBM.Watson.DeveloperCloud.Services.ConversationExperimental.v1 |
| 28 | +{ |
| 29 | + /// <summary> |
| 30 | + /// This class wraps the Watson Conversation service. |
| 31 | + /// <a href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/conversation.html">Conversation Service</a> |
| 32 | + /// </summary> |
| 33 | + public class ConversationExperimental : IWatsonService |
| 34 | + { |
| 35 | + #region Public Types |
| 36 | + /// <summary> |
| 37 | + /// The callback for GetWorkspaces(). |
| 38 | + /// </summary> |
| 39 | + public delegate void OnGetWorkspaces(Workspaces workspaces); |
| 40 | + /// <summary> |
| 41 | + /// The callback for Message(). |
| 42 | + /// </summary> |
| 43 | + /// <param name="success"></param> |
| 44 | + public delegate void OnMessageCallback(bool success); |
| 45 | + /// <summary> |
| 46 | + /// The callback delegate for the Converse() function. |
| 47 | + /// </summary> |
| 48 | + /// <param name="resp">The response object to a call to Converse().</param> |
| 49 | + public delegate void OnMessage(MessageResponse resp); |
| 50 | + |
| 51 | + #endregion |
| 52 | + |
| 53 | + #region Public Properties |
| 54 | + #endregion |
| 55 | + |
| 56 | + #region Private Data |
| 57 | + private const string SERVICE_ID = "ConversationExperimentalV1"; |
| 58 | + private static fsSerializer sm_Serializer = new fsSerializer(); |
| 59 | + #endregion |
| 60 | + |
| 61 | + #region Message |
| 62 | + private const string SERVICE_MESSAGE = "/v1/workspaces"; |
| 63 | + /// <summary> |
| 64 | + /// Message the specified workspaceId, input and callback. |
| 65 | + /// </summary> |
| 66 | + /// <param name="workspaceId">Workspace identifier.</param> |
| 67 | + /// <param name="input">Input.</param> |
| 68 | + /// <param name="callback">Callback.</param> |
| 69 | + public bool Message(string workspaceId, string input, OnMessage callback) |
| 70 | + { |
| 71 | + if(string.IsNullOrEmpty(workspaceId)) |
| 72 | + throw new ArgumentNullException("workspaceId"); |
| 73 | + if(string.IsNullOrEmpty(input)) |
| 74 | + throw new ArgumentNullException("input"); |
| 75 | + if(callback == null) |
| 76 | + throw new ArgumentNullException("callback"); |
| 77 | + |
| 78 | + RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE); |
| 79 | + if(connector == null) |
| 80 | + return false; |
| 81 | + |
| 82 | + string reqJson = "{{\"input\": {{\"text\": \"{0}\"}}}}"; |
| 83 | + string reqString = string.Format(reqJson, input); |
| 84 | + |
| 85 | + MessageReq req = new MessageReq(); |
| 86 | + req.Callback = callback; |
| 87 | + req.Headers["Content-Type"] = "application/json"; |
| 88 | + req.Headers["Accept"] = "application/json"; |
| 89 | + req.Parameters["version"] = Version.VERSION; |
| 90 | + req.Function = "/" + workspaceId + "/message"; |
| 91 | + req.Send = Encoding.UTF8.GetBytes(reqString); |
| 92 | + req.OnResponse = MessageResp; |
| 93 | + |
| 94 | + return connector.Send(req); |
| 95 | + } |
| 96 | + |
| 97 | + private class MessageReq : RESTConnector.Request |
| 98 | + { |
| 99 | + public OnMessage Callback { get; set; } |
| 100 | + } |
| 101 | + |
| 102 | + private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp) |
| 103 | + { |
| 104 | + MessageResponse response = new MessageResponse(); |
| 105 | + if (resp.Success) |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + fsData data = null; |
| 110 | + fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data); |
| 111 | + if (!r.Succeeded) |
| 112 | + throw new WatsonException(r.FormattedMessages); |
| 113 | + |
| 114 | + object obj = response; |
| 115 | + r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj); |
| 116 | + if (!r.Succeeded) |
| 117 | + throw new WatsonException(r.FormattedMessages); |
| 118 | + } |
| 119 | + catch (Exception e) |
| 120 | + { |
| 121 | + Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString()); |
| 122 | + resp.Success = false; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (((MessageReq)req).Callback != null) |
| 127 | + ((MessageReq)req).Callback(resp.Success ? response : null); |
| 128 | + } |
| 129 | + #endregion |
| 130 | + |
| 131 | + #region IWatsonService implementation |
| 132 | + |
| 133 | + public string GetServiceID() |
| 134 | + { |
| 135 | + return SERVICE_ID; |
| 136 | + } |
| 137 | + |
| 138 | + public void GetServiceStatus(ServiceStatus callback) |
| 139 | + { |
| 140 | + if (Config.Instance.FindCredentials(SERVICE_ID) != null) |
| 141 | + new CheckServiceStatus(this, callback); |
| 142 | + else |
| 143 | + { |
| 144 | + if (callback != null && callback.Target != null) |
| 145 | + { |
| 146 | + callback(SERVICE_ID, false); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private class CheckServiceStatus |
| 152 | + { |
| 153 | + private ConversationExperimental m_Service = null; |
| 154 | + private ServiceStatus m_Callback = null; |
| 155 | + private int m_ConversationCount = 0; |
| 156 | + |
| 157 | + public CheckServiceStatus(ConversationExperimental service, ServiceStatus callback) |
| 158 | + { |
| 159 | + m_Service = service; |
| 160 | + m_Callback = callback; |
| 161 | + |
| 162 | + string customServiceID = Config.Instance.GetVariableValue(SERVICE_ID + "_ID"); |
| 163 | + |
| 164 | + //If custom classifierID is defined then we are using it to check the service health |
| 165 | + if (!string.IsNullOrEmpty(customServiceID)) |
| 166 | + { |
| 167 | + |
| 168 | + if (!m_Service.Message(customServiceID, "Ping", OnMessage)) |
| 169 | + OnFailure("Failed to invoke Converse()."); |
| 170 | + else |
| 171 | + m_ConversationCount += 1; |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + OnFailure("Please define a workspace variable in config.json (" + SERVICE_ID + "_ID)"); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void OnMessage(MessageResponse resp) |
| 180 | + { |
| 181 | + if (m_ConversationCount > 0) |
| 182 | + { |
| 183 | + m_ConversationCount -= 1; |
| 184 | + if (resp != null) |
| 185 | + { |
| 186 | + if (m_ConversationCount == 0 && m_Callback != null && m_Callback.Target != null) |
| 187 | + m_Callback(SERVICE_ID, true); |
| 188 | + } |
| 189 | + else |
| 190 | + OnFailure("ConverseResponse is null."); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private void OnFailure(string msg) |
| 195 | + { |
| 196 | + Log.Error("Dialog", msg); |
| 197 | + m_Callback(SERVICE_ID, false); |
| 198 | + m_ConversationCount = 0; |
| 199 | + } |
| 200 | + }; |
| 201 | + #endregion |
| 202 | + } |
| 203 | +} |
0 commit comments