|
| 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 IBM.Watson.DeveloperCloud.Connection; |
| 19 | +using IBM.Watson.DeveloperCloud.Logging; |
| 20 | +using IBM.Watson.DeveloperCloud.Services.Conversation.v1; |
| 21 | +using IBM.Watson.DeveloperCloud.Services.Discovery.v1; |
| 22 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 23 | +using System.Collections.Generic; |
| 24 | +using UnityEngine; |
| 25 | + |
| 26 | +namespace IBM.Watson.DeveloperCloud.Examples |
| 27 | +{ |
| 28 | + public class ExampleCallback : MonoBehaviour |
| 29 | + { |
| 30 | + private string _conversationUsername = ""; |
| 31 | + private string _conversationPassword = ""; |
| 32 | + private string _conversationUrl = "https://gateway.watsonplatform.net/conversation/api"; |
| 33 | + private string _workspaceId = ""; |
| 34 | + |
| 35 | + private string _discoveryUsername = ""; |
| 36 | + private string _discoveryPassword = ""; |
| 37 | + private string _discoveryUrl = "https://gateway.watsonplatform.net/discovery/api"; |
| 38 | + |
| 39 | + void Start() |
| 40 | + { |
| 41 | + LogSystem.InstallDefaultReactors(); |
| 42 | + |
| 43 | + // Create conversation instance |
| 44 | + Credentials conversationCredentials = new Credentials(_conversationUsername, _conversationPassword, _conversationUrl); |
| 45 | + Conversation conversation = new Conversation(conversationCredentials); |
| 46 | + conversation.VersionDate = "2017-05-26"; |
| 47 | + |
| 48 | + // Create discovery instance |
| 49 | + Credentials discoveryCredentials = new Credentials(_discoveryUsername, _discoveryPassword, _discoveryUrl); |
| 50 | + Discovery discovery = new Discovery(discoveryCredentials); |
| 51 | + discovery.VersionDate = "2016-12-01"; |
| 52 | + |
| 53 | + // Call with generic callbacks |
| 54 | + conversation.Message(OnSuccess, OnMessageFail, _workspaceId, ""); |
| 55 | + discovery.GetEnvironments(OnSuccess, OnFail); |
| 56 | + |
| 57 | + // Call with sepcific callbacks |
| 58 | + conversation.Message(OnMessage, OnGetEnvironmentsFail, _workspaceId, ""); |
| 59 | + discovery.GetEnvironments(OnGetEnvironments, OnFail); |
| 60 | + } |
| 61 | + |
| 62 | + // Generic success callback |
| 63 | + private void OnSuccess<T>(T resp, Dictionary<string, object> customData) |
| 64 | + { |
| 65 | + Log.Debug("ExampleCallback.OnSuccess()", "Response received: {0}", customData["json"].ToString()); |
| 66 | + } |
| 67 | + |
| 68 | + // Generic fail callback |
| 69 | + private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData) |
| 70 | + { |
| 71 | + Log.Error("ExampleCallback.OnFail()", "Error received: {0}", error.ToString()); |
| 72 | + } |
| 73 | + |
| 74 | + // OnMessage callback |
| 75 | + private void OnMessage(object resp, Dictionary<string, object> customData) |
| 76 | + { |
| 77 | + Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", customData["json"].ToString()); |
| 78 | + } |
| 79 | + |
| 80 | + // OnGetEnvironments callback |
| 81 | + private void OnGetEnvironments(GetEnvironmentsResponse resp, Dictionary<string, object> customData) |
| 82 | + { |
| 83 | + Log.Debug("ExampleCallback.OnGetEnvironments()", "Response received: {0}", customData["json"].ToString()); |
| 84 | + } |
| 85 | + |
| 86 | + // OnMessageFail callback |
| 87 | + private void OnMessageFail(RESTConnector.Error error, Dictionary<string, object> customData) |
| 88 | + { |
| 89 | + Log.Error("ExampleCallback.OnMessageFail()", "Error received: {0}", error.ToString()); |
| 90 | + } |
| 91 | + |
| 92 | + // OnGetEnvironmentsFail callback |
| 93 | + private void OnGetEnvironmentsFail(RESTConnector.Error error, Dictionary<string, object> customData) |
| 94 | + { |
| 95 | + Log.Error("ExampleCallback.OnGetEnvironmentsFail()", "Error received: {0}", error.ToString()); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments