|
| 1 | +/** |
| 2 | +* Copyright 2018 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.Collections; |
| 20 | +using System.Collections.Generic; |
| 21 | +using FullSerializer; |
| 22 | +using IBM.Watson.DeveloperCloud.Connection; |
| 23 | +using IBM.Watson.DeveloperCloud.Logging; |
| 24 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 25 | +using IBM.WatsonDeveloperCloud.Assistant.v2; |
| 26 | +using UnityEngine; |
| 27 | + |
| 28 | +namespace IBM.Watson.DeveloperCloud.Services.Assistant.v2 |
| 29 | +{ |
| 30 | + public class ExampleAssistantV2 : MonoBehaviour |
| 31 | + { |
| 32 | + #region PLEASE SET THESE VARIABLES IN THE INSPECTOR |
| 33 | + [Space(10)] |
| 34 | + [Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/assistant/api\"")] |
| 35 | + [SerializeField] |
| 36 | + private string _serviceUrl; |
| 37 | + [Tooltip("The assistantId to run the example.")] |
| 38 | + [SerializeField] |
| 39 | + private string _assistantId; |
| 40 | + [Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")] |
| 41 | + [SerializeField] |
| 42 | + private string _versionDate; |
| 43 | + [Header("CF Authentication")] |
| 44 | + [Tooltip("The authentication username.")] |
| 45 | + [SerializeField] |
| 46 | + private string _username; |
| 47 | + [Tooltip("The authentication password.")] |
| 48 | + [SerializeField] |
| 49 | + private string _password; |
| 50 | + [Header("IAM Authentication")] |
| 51 | + [Tooltip("The IAM apikey.")] |
| 52 | + [SerializeField] |
| 53 | + private string _iamApikey; |
| 54 | + [Tooltip("The IAM url used to authenticate the apikey (optional). This defaults to \"https://iam.bluemix.net/identity/token\".")] |
| 55 | + [SerializeField] |
| 56 | + private string _iamUrl; |
| 57 | + #endregion |
| 58 | + |
| 59 | + private Assistant _service; |
| 60 | + |
| 61 | + private fsSerializer _serializer = new fsSerializer(); |
| 62 | + |
| 63 | + private bool _createSessionTested = false; |
| 64 | + private bool _messageTested = false; |
| 65 | + private bool _deleteSessionTested = false; |
| 66 | + private string _sessionId; |
| 67 | + |
| 68 | + private void Start() |
| 69 | + { |
| 70 | + LogSystem.InstallDefaultReactors(); |
| 71 | + Runnable.Run(CreateService()); |
| 72 | + } |
| 73 | + |
| 74 | + private IEnumerator CreateService() |
| 75 | + { |
| 76 | + // Create credential and instantiate service |
| 77 | + Credentials credentials = null; |
| 78 | + if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password)) |
| 79 | + { |
| 80 | + // Authenticate using username and password |
| 81 | + credentials = new Credentials(_username, _password, _serviceUrl); |
| 82 | + } |
| 83 | + else if (!string.IsNullOrEmpty(_iamApikey)) |
| 84 | + { |
| 85 | + // Authenticate using iamApikey |
| 86 | + TokenOptions tokenOptions = new TokenOptions() |
| 87 | + { |
| 88 | + IamApiKey = _iamApikey, |
| 89 | + IamUrl = _iamUrl |
| 90 | + }; |
| 91 | + |
| 92 | + credentials = new Credentials(tokenOptions, _serviceUrl); |
| 93 | + |
| 94 | + // Wait for tokendata |
| 95 | + while (!credentials.HasIamTokenData()) |
| 96 | + yield return null; |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + throw new WatsonException("Please provide either username and password or IAM apikey to authenticate the service."); |
| 101 | + } |
| 102 | + |
| 103 | + _service = new Assistant(credentials); |
| 104 | + _service.VersionDate = _versionDate; |
| 105 | + |
| 106 | + Runnable.Run(Examples()); |
| 107 | + } |
| 108 | + |
| 109 | + private IEnumerator Examples() |
| 110 | + { |
| 111 | + Log.Debug("ExampleAssistantV2.Examples()", "Attempting to CreateSession"); |
| 112 | + _service.CreateSession(OnCreateSession, OnFail, _assistantId); |
| 113 | + |
| 114 | + while (!_createSessionTested) |
| 115 | + { |
| 116 | + yield return null; |
| 117 | + } |
| 118 | + |
| 119 | + Log.Debug("ExampleAssistantV2.Examples()", "Attempting to Message"); |
| 120 | + _service.Message(OnMessage, OnFail, _assistantId, _sessionId); |
| 121 | + |
| 122 | + while (!_messageTested) |
| 123 | + { |
| 124 | + yield return null; |
| 125 | + } |
| 126 | + |
| 127 | + Log.Debug("ExampleAssistantV2.Examples()", "Attempting to DeleteSession"); |
| 128 | + _service.DeleteSession(OnDeleteSession, OnFail, _assistantId, _sessionId); |
| 129 | + |
| 130 | + while (!_deleteSessionTested) |
| 131 | + { |
| 132 | + yield return null; |
| 133 | + } |
| 134 | + |
| 135 | + Log.Debug("ExampleAssistantV2.Examples()", "Assistant examples complete."); |
| 136 | + } |
| 137 | + |
| 138 | + private void OnDeleteSession(object response, Dictionary<string, object> customData) |
| 139 | + { |
| 140 | + Log.Debug("ExampleAssistantV2.OnDeleteSession()", "Session deleted."); |
| 141 | + _createSessionTested = true; |
| 142 | + } |
| 143 | + |
| 144 | + private void OnMessage(MessageResponse response, Dictionary<string, object> customData) |
| 145 | + { |
| 146 | + _messageTested = true; |
| 147 | + } |
| 148 | + |
| 149 | + private void OnCreateSession(SessionResponse response, Dictionary<string, object> customData) |
| 150 | + { |
| 151 | + Log.Debug("ExampleAssistantV2.OnCreateSession()", "Session: {0}", response.SessionId); |
| 152 | + _sessionId = response.SessionId; |
| 153 | + _createSessionTested = true; |
| 154 | + } |
| 155 | + |
| 156 | + private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData) |
| 157 | + { |
| 158 | + Log.Debug("ExampleAssistantV2.OnFail()", "Call failed: {0}: {1}", error.ErrorCode, error.ErrorMessage); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments