@@ -54,6 +54,23 @@ The credentials for each service contain either a `username`, `password` and end
54
54
55
55
** WARNING:** You are responsible for securing your own credentials. Any user with your service credentials can access your service instances!
56
56
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
+
57
74
## Authentication
58
75
Before you can use a service, it must be authenticated with the service instance's ` username ` , ` password ` and ` url ` .
59
76
@@ -81,22 +98,87 @@ void Start()
81
98
}
82
99
```
83
100
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
+ ```
100
182
101
183
## Authentication Tokens
102
184
You use tokens to write applications that make authenticated requests to IBM Watson™ services without embedding service credentials in every call.
0 commit comments