Skip to content

Commit

Permalink
Merge pull request #402 from watson-developer-cloud/rc-2.4.0
Browse files Browse the repository at this point in the history
Watson Unity SDK v2.4.0
  • Loading branch information
mediumTaj authored Jun 13, 2018
2 parents 64f983c + 09a0c7e commit 98a7b08
Show file tree
Hide file tree
Showing 42 changed files with 2,756 additions and 247 deletions.
52 changes: 47 additions & 5 deletions Examples/ServiceExamples/Scripts/ExampleAssistant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,30 @@
public class ExampleAssistant : MonoBehaviour
{
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
[Space(10)]
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/assistant/api\"")]
[SerializeField]
private string _serviceUrl;
[Tooltip("The workspaceId to run the example.")]
[SerializeField]
private string _workspaceId;
[Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")]
[SerializeField]
private string _versionDate;
[Header("CF Authentication")]
[Tooltip("The authentication username.")]
[SerializeField]
private string _username;
[Tooltip("The authentication password.")]
[SerializeField]
private string _password;
[Header("IAM Authentication")]
[Tooltip("The IAM apikey.")]
[SerializeField]
private string _url;
[SerializeField]
private string _versionDate;
private string _iamApikey;
[Tooltip("The IAM url used to authenticate the apikey (optional). This defaults to \"https://iam.bluemix.net/identity/token\".")]
[SerializeField]
private string _workspaceId;
private string _iamUrl;
#endregion

private string _createdWorkspaceId;
Expand Down Expand Up @@ -113,9 +127,37 @@ public class ExampleAssistant : MonoBehaviour
void Start()
{
LogSystem.InstallDefaultReactors();
Runnable.Run(CreateService());
}

private IEnumerator CreateService()
{
// Create credential and instantiate service
Credentials credentials = new Credentials(_username, _password, _url);
Credentials credentials = null;
if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))
{
// Authenticate using username and password
credentials = new Credentials(_username, _password, _serviceUrl);
}
else if (!string.IsNullOrEmpty(_iamApikey))
{
// Authenticate using iamApikey
TokenOptions tokenOptions = new TokenOptions()
{
IamApiKey = _iamApikey,
IamUrl = _iamUrl
};

credentials = new Credentials(tokenOptions, _serviceUrl);

// Wait for tokendata
while (!credentials.HasIamTokenData())
yield return null;
}
else
{
throw new WatsonException("Please provide either username and password or IAM apikey to authenticate the service.");
}

_service = new Assistant(credentials);
_service.VersionDate = _versionDate;
Expand Down
62 changes: 52 additions & 10 deletions Examples/ServiceExamples/Scripts/ExampleConversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,33 @@
public class ExampleConversation : MonoBehaviour
{
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
[Space(10)]
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/conversation/api\"")]
[SerializeField]
private string _serviceUrl;
[Tooltip("The workspaceId to run the example.")]
[SerializeField]
private string _workspaceId;
[Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")]
[SerializeField]
private string _versionDate;
[Header("CF Authentication")]
[Tooltip("The authentication username.")]
[SerializeField]
private string _username;
[Tooltip("The authentication password.")]
[SerializeField]
private string _password;
[Header("IAM Authentication")]
[Tooltip("The IAM apikey.")]
[SerializeField]
private string _url;
private string _iamApikey;
[Tooltip("The IAM url used to authenticate the apikey (optional). This defaults to \"https://iam.bluemix.net/identity/token\".")]
[SerializeField]
private string _workspaceId;
[SerializeField]
private string _versionDate;
private string _iamUrl;
#endregion

private Conversation _conversation;
private Conversation _service;

private string[] _questionArray = { "can you turn up the AC", "can you turn on the wipers", "can you turn off the wipers", "can you turn down the ac", "can you unlock the door" };
private fsSerializer _serializer = new fsSerializer();
Expand All @@ -50,19 +64,47 @@ public class ExampleConversation : MonoBehaviour
void Start()
{
LogSystem.InstallDefaultReactors();
Runnable.Run(CreateService());
}

private IEnumerator CreateService()
{
// Create credential and instantiate service
Credentials credentials = new Credentials(_username, _password, _url);
Credentials credentials = null;
if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))
{
// Authenticate using username and password
credentials = new Credentials(_username, _password, _serviceUrl);
}
else if (!string.IsNullOrEmpty(_iamApikey))
{
// Authenticate using iamApikey
TokenOptions tokenOptions = new TokenOptions()
{
IamApiKey = _iamApikey,
IamUrl = _iamUrl
};

credentials = new Credentials(tokenOptions, _serviceUrl);

// Wait for tokendata
while (!credentials.HasIamTokenData())
yield return null;
}
else
{
throw new WatsonException("Please provide either username and password or IAM apikey to authenticate the service.");
}

_conversation = new Conversation(credentials);
_conversation.VersionDate = _versionDate;
_service = new Conversation(credentials);
_service.VersionDate = _versionDate;

Runnable.Run(Examples());
}

private IEnumerator Examples()
{
if (!_conversation.Message(OnMessage, OnFail, _workspaceId, "hello"))
if (!_service.Message(OnMessage, OnFail, _workspaceId, "hello"))
Log.Debug("ExampleConversation.Message()", "Failed to message!");

while (_waitingForResponse)
Expand Down Expand Up @@ -111,7 +153,7 @@ private void AskQuestion()
context = _context
};

if (!_conversation.Message(OnMessage, OnFail, _workspaceId, messageRequest))
if (!_service.Message(OnMessage, OnFail, _workspaceId, messageRequest))
Log.Debug("ExampleConversation.AskQuestion()", "Failed to message!");
}

Expand Down
Loading

0 comments on commit 98a7b08

Please sign in to comment.