Skip to content

Commit 98a7b08

Browse files
authored
Merge pull request #402 from watson-developer-cloud/rc-2.4.0
Watson Unity SDK v2.4.0
2 parents 64f983c + 09a0c7e commit 98a7b08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2756
-247
lines changed

Examples/ServiceExamples/Scripts/ExampleAssistant.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,30 @@
2828
public class ExampleAssistant : MonoBehaviour
2929
{
3030
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
31+
[Space(10)]
32+
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/assistant/api\"")]
33+
[SerializeField]
34+
private string _serviceUrl;
35+
[Tooltip("The workspaceId to run the example.")]
36+
[SerializeField]
37+
private string _workspaceId;
38+
[Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")]
39+
[SerializeField]
40+
private string _versionDate;
41+
[Header("CF Authentication")]
42+
[Tooltip("The authentication username.")]
3143
[SerializeField]
3244
private string _username;
45+
[Tooltip("The authentication password.")]
3346
[SerializeField]
3447
private string _password;
48+
[Header("IAM Authentication")]
49+
[Tooltip("The IAM apikey.")]
3550
[SerializeField]
36-
private string _url;
37-
[SerializeField]
38-
private string _versionDate;
51+
private string _iamApikey;
52+
[Tooltip("The IAM url used to authenticate the apikey (optional). This defaults to \"https://iam.bluemix.net/identity/token\".")]
3953
[SerializeField]
40-
private string _workspaceId;
54+
private string _iamUrl;
4155
#endregion
4256

4357
private string _createdWorkspaceId;
@@ -113,9 +127,37 @@ public class ExampleAssistant : MonoBehaviour
113127
void Start()
114128
{
115129
LogSystem.InstallDefaultReactors();
130+
Runnable.Run(CreateService());
131+
}
116132

133+
private IEnumerator CreateService()
134+
{
117135
// Create credential and instantiate service
118-
Credentials credentials = new Credentials(_username, _password, _url);
136+
Credentials credentials = null;
137+
if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))
138+
{
139+
// Authenticate using username and password
140+
credentials = new Credentials(_username, _password, _serviceUrl);
141+
}
142+
else if (!string.IsNullOrEmpty(_iamApikey))
143+
{
144+
// Authenticate using iamApikey
145+
TokenOptions tokenOptions = new TokenOptions()
146+
{
147+
IamApiKey = _iamApikey,
148+
IamUrl = _iamUrl
149+
};
150+
151+
credentials = new Credentials(tokenOptions, _serviceUrl);
152+
153+
// Wait for tokendata
154+
while (!credentials.HasIamTokenData())
155+
yield return null;
156+
}
157+
else
158+
{
159+
throw new WatsonException("Please provide either username and password or IAM apikey to authenticate the service.");
160+
}
119161

120162
_service = new Assistant(credentials);
121163
_service.VersionDate = _versionDate;

Examples/ServiceExamples/Scripts/ExampleConversation.cs

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,33 @@
2727
public class ExampleConversation : MonoBehaviour
2828
{
2929
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
30+
[Space(10)]
31+
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/conversation/api\"")]
32+
[SerializeField]
33+
private string _serviceUrl;
34+
[Tooltip("The workspaceId to run the example.")]
35+
[SerializeField]
36+
private string _workspaceId;
37+
[Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")]
38+
[SerializeField]
39+
private string _versionDate;
40+
[Header("CF Authentication")]
41+
[Tooltip("The authentication username.")]
3042
[SerializeField]
3143
private string _username;
44+
[Tooltip("The authentication password.")]
3245
[SerializeField]
3346
private string _password;
47+
[Header("IAM Authentication")]
48+
[Tooltip("The IAM apikey.")]
3449
[SerializeField]
35-
private string _url;
50+
private string _iamApikey;
51+
[Tooltip("The IAM url used to authenticate the apikey (optional). This defaults to \"https://iam.bluemix.net/identity/token\".")]
3652
[SerializeField]
37-
private string _workspaceId;
38-
[SerializeField]
39-
private string _versionDate;
53+
private string _iamUrl;
4054
#endregion
4155

42-
private Conversation _conversation;
56+
private Conversation _service;
4357

4458
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" };
4559
private fsSerializer _serializer = new fsSerializer();
@@ -50,19 +64,47 @@ public class ExampleConversation : MonoBehaviour
5064
void Start()
5165
{
5266
LogSystem.InstallDefaultReactors();
67+
Runnable.Run(CreateService());
68+
}
5369

70+
private IEnumerator CreateService()
71+
{
5472
// Create credential and instantiate service
55-
Credentials credentials = new Credentials(_username, _password, _url);
73+
Credentials credentials = null;
74+
if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))
75+
{
76+
// Authenticate using username and password
77+
credentials = new Credentials(_username, _password, _serviceUrl);
78+
}
79+
else if (!string.IsNullOrEmpty(_iamApikey))
80+
{
81+
// Authenticate using iamApikey
82+
TokenOptions tokenOptions = new TokenOptions()
83+
{
84+
IamApiKey = _iamApikey,
85+
IamUrl = _iamUrl
86+
};
87+
88+
credentials = new Credentials(tokenOptions, _serviceUrl);
89+
90+
// Wait for tokendata
91+
while (!credentials.HasIamTokenData())
92+
yield return null;
93+
}
94+
else
95+
{
96+
throw new WatsonException("Please provide either username and password or IAM apikey to authenticate the service.");
97+
}
5698

57-
_conversation = new Conversation(credentials);
58-
_conversation.VersionDate = _versionDate;
99+
_service = new Conversation(credentials);
100+
_service.VersionDate = _versionDate;
59101

60102
Runnable.Run(Examples());
61103
}
62104

63105
private IEnumerator Examples()
64106
{
65-
if (!_conversation.Message(OnMessage, OnFail, _workspaceId, "hello"))
107+
if (!_service.Message(OnMessage, OnFail, _workspaceId, "hello"))
66108
Log.Debug("ExampleConversation.Message()", "Failed to message!");
67109

68110
while (_waitingForResponse)
@@ -111,7 +153,7 @@ private void AskQuestion()
111153
context = _context
112154
};
113155

114-
if (!_conversation.Message(OnMessage, OnFail, _workspaceId, messageRequest))
156+
if (!_service.Message(OnMessage, OnFail, _workspaceId, messageRequest))
115157
Log.Debug("ExampleConversation.AskQuestion()", "Failed to message!");
116158
}
117159

0 commit comments

Comments
 (0)