Skip to content

Commit 9ae91cd

Browse files
committed
update main readme with callback and customdata documentation, incorrect ifdef for restconnector security import, incorrect name in document conversion
1 parent 2b32ac2 commit 9ae91cd

File tree

3 files changed

+101
-19
lines changed

3 files changed

+101
-19
lines changed

README.md

Lines changed: 98 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ The credentials for each service contain either a `username`, `password` and end
5454

5555
**WARNING:** You are responsible for securing your own credentials. Any user with your service credentials can access your service instances!
5656

57+
## Watson Services
58+
To get started with the Watson Services in Unity, click on each service below to read through each of their `README.md`'s and their codes.
59+
* [Alchemy Language](/Scripts/Services/AlchemyAPI/v1)
60+
* [Conversation](/Scripts/Services/Conversation/v1)
61+
* [Discovery](/Scripts/Services/Discovery/v1)
62+
* [Document Conversion](/Scripts/Services/DocumentConversion/v1) **Deprecated**
63+
* [Language Translator](/Scripts/Services/LanguageTranslator/v2)
64+
* [Natural Language Classifier](/Scripts/Services/NaturalLanguageClassifier/v2)
65+
* [Natural Language Understanding](/Scripts/Services/NaturalLanguageUnderstanding/v1)
66+
* [Personality Insights](/Scripts/Services/PersonalityInsights/v3)
67+
* [Retrieve and Rank](/Scripts/Services/RetrieveAndRank/v1) **Deprecated**
68+
* [Speech to Text](/Scripts/Services/SpeechToText/v1)
69+
* [Text to Speech](/Scripts/Services/TextToSpeech/v1)
70+
* [Tone Analyzer](/Scripts/Services/ToneAnalyzer/v3)
71+
* [Tradeoff Analytics](/Scripts/Services/TradeoffAnalytics/v1)
72+
* [Visual Recognition](/Scripts/Services/VisualRecognition/v3)
73+
5774
## Authentication
5875
Before you can use a service, it must be authenticated with the service instance's `username`, `password` and `url`.
5976

@@ -81,22 +98,87 @@ void Start()
8198
}
8299
```
83100

84-
## Watson Services
85-
To get started with the Watson Services in Unity, click on each service below to read through each of their `README.md`'s and their codes.
86-
* [Alchemy Language](/Scripts/Services/AlchemyAPI/v1)
87-
* [Conversation](/Scripts/Services/Conversation/v1)
88-
* [Discovery](/Scripts/Services/Discovery/v1)
89-
* [Document Conversion](/Scripts/Services/DocumentConversion/v1) **Deprecated**
90-
* [Language Translator](/Scripts/Services/LanguageTranslator/v2)
91-
* [Natural Language Classifier](/Scripts/Services/NaturalLanguageClassifier/v2)
92-
* [Natural Language Understanding](/Scripts/Services/NaturalLanguageUnderstanding/v1)
93-
* [Personality Insights](/Scripts/Services/PersonalityInsights/v3)
94-
* [Retrieve and Rank](/Scripts/Services/RetrieveAndRank/v1) **Deprecated**
95-
* [Speech to Text](/Scripts/Services/SpeechToText/v1)
96-
* [Text to Speech](/Scripts/Services/TextToSpeech/v1)
97-
* [Tone Analyzer](/Scripts/Services/ToneAnalyzer/v3)
98-
* [Tradeoff Analytics](/Scripts/Services/TradeoffAnalytics/v1)
99-
* [Visual Recognition](/Scripts/Services/VisualRecognition/v3)
101+
## Callbacks
102+
Success and failure callbacks are required. You can specify the return type in the callback.
103+
```cs
104+
private void Example()
105+
{
106+
// Call with sepcific callbacks
107+
conversation.Message(OnMessage, OnGetEnvironmentsFail, _workspaceId, "");
108+
discovery.GetEnvironments(OnGetEnvironments, OnFail);
109+
}
110+
111+
// OnMessage callback
112+
private void OnMessage(object resp, Dictionary<string, object> customData)
113+
{
114+
Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", customData["json"].ToString());
115+
}
116+
117+
// OnGetEnvironments callback
118+
private void OnGetEnvironments(GetEnvironmentsResponse resp, Dictionary<string, object> customData)
119+
{
120+
Log.Debug("ExampleCallback.OnGetEnvironments()", "Response received: {0}", customData["json"].ToString());
121+
}
122+
123+
// OnMessageFail callback
124+
private void OnMessageFail(RESTConnector.Error error, Dictionary<string, object> customData)
125+
{
126+
Log.Error("ExampleCallback.OnMessageFail()", "Error received: {0}", error.ToString());
127+
}
128+
129+
// OnGetEnvironmentsFail callback
130+
private void OnGetEnvironmentsFail(RESTConnector.Error error, Dictionary<string, object> customData)
131+
{
132+
Log.Error("ExampleCallback.OnGetEnvironmentsFail()", "Error received: {0}", error.ToString());
133+
}
134+
```
135+
136+
Since the success callback signature is generic and the failure callback always has the same signature, you can use a single set of callbacks to handle multiple calls.
137+
```cs
138+
private void Example()
139+
{
140+
// Call with generic callbacks
141+
conversation.Message(OnSuccess, OnMessageFail, "<workspace-id>", "");
142+
discovery.GetEnvironments(OnSuccess, OnFail);
143+
}
144+
145+
// Generic success callback
146+
private void OnSuccess<T>(T resp, Dictionary<string, object> customData)
147+
{
148+
Log.Debug("ExampleCallback.OnSuccess()", "Response received: {0}", customData["json"].ToString());
149+
}
150+
151+
// Generic fail callback
152+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
153+
{
154+
Log.Error("ExampleCallback.OnFail()", "Error received: {0}", error.ToString());
155+
}
156+
```
157+
158+
## Custom data
159+
Custom data can be passed through a `Dictionary<string, object> customData` in each call. In most cases, the raw json response is returned in the customData under `"json"` entry. In cases where there is no returned json, the entry will contain the success and http response code of the call.
160+
161+
```cs
162+
void Example()
163+
{
164+
Dictionary<string, object> customData = new Dictionary<string, object>();
165+
customData.Add("foo", "bar");
166+
conversation.Message(OnSuccess, OnFail, "<workspace-id>", "", customData);
167+
}
168+
169+
// Generic success callback
170+
private void OnSuccess<T>(T resp, Dictionary<string, object> customData)
171+
{
172+
Log.Debug("ExampleCustomData.OnSuccess()", "Custom Data: {0}", customData["foo"].ToString()); // returns "bar"
173+
}
174+
175+
// Generic fail callback
176+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
177+
{
178+
Log.Error("ExampleCustomData.OnFail()", "Error received: {0}", error.ToString()); // returns error string
179+
Log.Debug("ExampleCustomData.OnFail()", "Custom Data: {0}", customData["foo"].ToString()); // returns "bar"
180+
}
181+
```
100182

101183
## Authentication Tokens
102184
You use tokens to write applications that make authenticated requests to IBM Watson™ services without embedding service credentials in every call.

Scripts/Connection/RESTConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
using UnityEngine;
2828
using UnityEngine.Networking;
2929

30-
#if UNITY_EDITOR
30+
#if !NETFX_CORE
3131
using System.Net;
3232
using System.Net.Security;
3333
#endif

Scripts/Services/DocumentConversion/v1/DocumentConversion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private void ConvertDocumentResponse(RESTConnector.Request req, RESTConnector.Re
228228
else if ((req as ConvertDocumentRequest).ConversionTarget == ConversionTarget.NormalizedHtml)
229229
{
230230
#if NETFX_CORE
231-
response.htmlContent = System.Text.Encoding.GetEncoding(0).GetString(resp.Data);
231+
result.htmlContent = System.Text.Encoding.GetEncoding(0).GetString(resp.Data);
232232
#else
233233
result.htmlContent = System.Text.Encoding.Default.GetString(resp.Data);
234234
#endif
@@ -237,7 +237,7 @@ private void ConvertDocumentResponse(RESTConnector.Request req, RESTConnector.Re
237237
else if ((req as ConvertDocumentRequest).ConversionTarget == ConversionTarget.NormalizedText)
238238
{
239239
#if NETFX_CORE
240-
response.textContent = System.Text.Encoding.GetEncoding(0).GetString(resp.Data);
240+
result.textContent = System.Text.Encoding.GetEncoding(0).GetString(resp.Data);
241241
#else
242242
result.textContent = System.Text.Encoding.Default.GetString(resp.Data);
243243
#endif

0 commit comments

Comments
 (0)