|
26 | 26 |
|
27 | 27 | namespace IBM.Watson.DeveloperCloud.Services.ConversationExperimental.v1
|
28 | 28 | {
|
29 |
| - /// <summary> |
30 |
| - /// This class wraps the Watson Experimental Conversation service. |
31 |
| - /// <a href="http://www.ibm.com/watson/developercloud/conversation.html">Experimental 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 |
| - /// <exclude /> |
133 |
| - public string GetServiceID() |
134 |
| - { |
135 |
| - return SERVICE_ID; |
136 |
| - } |
137 |
| - |
138 |
| - /// <exclude /> |
139 |
| - public void GetServiceStatus(ServiceStatus callback) |
140 |
| - { |
141 |
| - if (Config.Instance.FindCredentials(SERVICE_ID) != null) |
142 |
| - new CheckServiceStatus(this, callback); |
143 |
| - else |
144 |
| - { |
145 |
| - if (callback != null && callback.Target != null) |
146 |
| - { |
147 |
| - callback(SERVICE_ID, false); |
148 |
| - } |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - private class CheckServiceStatus |
| 29 | + /// <summary> |
| 30 | + /// This class wraps the Watson Experimental Conversation service. |
| 31 | + /// <a href="http://www.ibm.com/watson/developercloud/conversation.html">Experimental 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 |
153 | 108 | {
|
154 |
| - private ConversationExperimental m_Service = null; |
155 |
| - private ServiceStatus m_Callback = null; |
156 |
| - private int m_ConversationCount = 0; |
157 |
| - |
158 |
| - public CheckServiceStatus(ConversationExperimental service, ServiceStatus callback) |
159 |
| - { |
160 |
| - m_Service = service; |
161 |
| - m_Callback = callback; |
162 |
| - |
163 |
| - string customServiceID = Config.Instance.GetVariableValue(SERVICE_ID + "_ID"); |
164 |
| - |
165 |
| - //If custom classifierID is defined then we are using it to check the service health |
166 |
| - if (!string.IsNullOrEmpty(customServiceID)) |
167 |
| - { |
168 |
| - |
169 |
| - if (!m_Service.Message(customServiceID, "Ping", OnMessage)) |
170 |
| - OnFailure("Failed to invoke Converse()."); |
171 |
| - else |
172 |
| - m_ConversationCount += 1; |
173 |
| - } |
174 |
| - else |
175 |
| - { |
176 |
| - OnFailure("Please define a workspace variable in config.json (" + SERVICE_ID + "_ID)"); |
177 |
| - } |
178 |
| - } |
179 |
| - |
180 |
| - private void OnMessage(MessageResponse resp) |
181 |
| - { |
182 |
| - if (m_ConversationCount > 0) |
183 |
| - { |
184 |
| - m_ConversationCount -= 1; |
185 |
| - if (resp != null) |
186 |
| - { |
187 |
| - if (m_ConversationCount == 0 && m_Callback != null && m_Callback.Target != null) |
188 |
| - m_Callback(SERVICE_ID, true); |
189 |
| - } |
190 |
| - else |
191 |
| - OnFailure("ConverseResponse is null."); |
192 |
| - } |
193 |
| - } |
194 |
| - |
195 |
| - private void OnFailure(string msg) |
196 |
| - { |
197 |
| - Log.Error("Dialog", msg); |
198 |
| - m_Callback(SERVICE_ID, false); |
199 |
| - m_ConversationCount = 0; |
200 |
| - } |
201 |
| - }; |
202 |
| - #endregion |
203 |
| - } |
| 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 | + /// <exclude /> |
| 133 | + public string GetServiceID() |
| 134 | + { |
| 135 | + return SERVICE_ID; |
| 136 | + } |
| 137 | + |
| 138 | + /// <exclude /> |
| 139 | + public void GetServiceStatus(ServiceStatus callback) |
| 140 | + { |
| 141 | + if (Config.Instance.FindCredentials(SERVICE_ID) != null) |
| 142 | + new CheckServiceStatus(this, callback); |
| 143 | + else |
| 144 | + { |
| 145 | + if (callback != null && callback.Target != null) |
| 146 | + { |
| 147 | + callback(SERVICE_ID, false); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private class CheckServiceStatus |
| 153 | + { |
| 154 | + private ConversationExperimental m_Service = null; |
| 155 | + private ServiceStatus m_Callback = null; |
| 156 | + private int m_ConversationCount = 0; |
| 157 | + |
| 158 | + public CheckServiceStatus(ConversationExperimental service, ServiceStatus callback) |
| 159 | + { |
| 160 | + m_Service = service; |
| 161 | + m_Callback = callback; |
| 162 | + |
| 163 | + string customServiceID = Config.Instance.GetVariableValue(SERVICE_ID + "_ID"); |
| 164 | + |
| 165 | + //If custom classifierID is defined then we are using it to check the service health |
| 166 | + if (!string.IsNullOrEmpty(customServiceID)) |
| 167 | + { |
| 168 | + |
| 169 | + if (!m_Service.Message(customServiceID, "Ping", OnMessage)) |
| 170 | + OnFailure("Failed to invoke Converse()."); |
| 171 | + else |
| 172 | + m_ConversationCount += 1; |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + OnFailure("Please define a workspace variable in config.json (" + SERVICE_ID + "_ID)"); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void OnMessage(MessageResponse resp) |
| 181 | + { |
| 182 | + if (m_ConversationCount > 0) |
| 183 | + { |
| 184 | + m_ConversationCount -= 1; |
| 185 | + if (resp != null) |
| 186 | + { |
| 187 | + if (m_ConversationCount == 0 && m_Callback != null && m_Callback.Target != null) |
| 188 | + m_Callback(SERVICE_ID, true); |
| 189 | + } |
| 190 | + else |
| 191 | + OnFailure("ConverseResponse is null."); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private void OnFailure(string msg) |
| 196 | + { |
| 197 | + Log.Error("Dialog", msg); |
| 198 | + m_Callback(SERVICE_ID, false); |
| 199 | + m_ConversationCount = 0; |
| 200 | + } |
| 201 | + }; |
| 202 | + #endregion |
| 203 | + } |
204 | 204 | }
|
0 commit comments