Skip to content

Commit 8540e9f

Browse files
committed
docs(Examples): Updated examples in the readme
1 parent a6e4729 commit 8540e9f

File tree

1 file changed

+152
-37
lines changed

1 file changed

+152
-37
lines changed

README.md

Lines changed: 152 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ You supply either an IAM service **API key** or an **access token**:
107107

108108
#### Supplying the IAM API key
109109
```cs
110+
Credentials credentials;
111+
AssistantService assistant;
112+
string versionDate = "<service-version-date>";
113+
110114
IEnumerator TokenExample()
111115
{
112116
// Create IAM token options and supply the apikey. IamUrl is the URL used to get the
@@ -122,9 +126,11 @@ IEnumerator TokenExample()
122126
while (!credentials.HasIamTokenData())
123127
yield return null;
124128

125-
assistant = new Assistant(credentials);
126-
assistant.VersionDate = "2019-03-28";
127-
assistant.ListWorkspaces(OnListWorkspaces);
129+
assistant = new AssistantService(
130+
versionDate: versionDate,
131+
credentials: credentials
132+
);
133+
assistant.ListWorkspaces(callback: OnListWorkspaces);
128134
}
129135

130136
private void OnListWorkspaces(DetailedResponse<WorkspaceCollection> response, IBMError error)
@@ -135,6 +141,10 @@ private void OnListWorkspaces(DetailedResponse<WorkspaceCollection> response, IB
135141

136142
#### Supplying the access token
137143
```cs
144+
Credentials credentials;
145+
AssistantService assistant;
146+
string versionDate = "<service-version-date>";
147+
138148
void TokenExample()
139149
{
140150
// Create IAM token options and supply the access token.
@@ -144,11 +154,13 @@ void TokenExample()
144154
};
145155

146156
// Create credentials using the IAM token options
147-
credentials = new Credentials(iamTokenOptions, "<service-url");
157+
credentials = new Credentials(iamTokenOptions, "<service-url>");
148158

149-
assistant = new Assistant(credentials);
150-
assistant.VersionDate = "2018-02-16";
151-
assistant.ListWorkspaces(OnListWorkspaces);
159+
assistant = new AssistantService(
160+
versionDate: versionDate,
161+
credentials: credentials
162+
);
163+
assistant.ListWorkspaces(callback: OnListWorkspaces);
152164
}
153165

154166
private void OnListWorkspaces(DetailedResponse<WorkspaceCollection> response, IBMError error)
@@ -159,46 +171,96 @@ private void OnListWorkspaces(DetailedResponse<WorkspaceCollection> response, IB
159171

160172
### Username and password
161173
```cs
162-
using IBM.Watson.Assistant.v1;
163-
using IBM.Cloud.SDK.Utilities;
174+
Credentials credentials;
175+
AssistantService assistant;
176+
string versionDate = "<service-version-date>";
164177

165-
void Start()
178+
void UsernamePasswordExample()
166179
{
167-
Credentials credentials = new Credentials(<username>, <password>, <url>);
168-
Assistant assistant = new Assistant(credentials);
180+
Credentials credentials = new Credentials("<username>", "<password>", "<url>");
181+
assistant = new AssistantService(
182+
versionDate: versionDate,
183+
credentials: credentials
184+
);
169185
}
170186
```
171187

172188
## Callbacks
173189
A success callback is required. You can specify the return type in the callback.
174190
```cs
191+
AssistantService assistant;
192+
string assistantVersionDate = "<assistant-version-date>";
193+
Credentials assistantCredentials;
194+
string workspaceId = "<workspaceId>";
195+
196+
DiscoveryService discovery;
197+
string discoveryVersionDate = "<discovery-version-date>";
198+
Credentials discoveryCredentials;
199+
175200
private void Example()
176201
{
202+
assistant = new AssistantService(
203+
versionDate: assistantVersionDate,
204+
credentials: assistantCredentials
205+
);
206+
207+
discovery = new DiscoveryService(
208+
versionDate: discoveryVersionDate,
209+
credentials: discoveryCredentials
210+
);
211+
177212
// Call with sepcific callbacks
178-
assistant.Message(OnMessage, workspaceId);
179-
discovery.GetEnvironments(OnGetEnvironments);
213+
assistant.Message(
214+
callback: OnMessage,
215+
workspaceId: workspaceId
216+
);
217+
218+
discovery.ListEnvironments(
219+
callback: OnGetEnvironments
220+
);
180221
}
181222

182-
// OnMessage callback
183-
private void OnMessage(DetailedResponse<JObject> resp, IBMError error)
223+
private void OnMessage(DetailedResponse<MessageResponse> response, IBMError error)
184224
{
185-
Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", resp.Response);
225+
Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", response.Response);
186226
}
187227

188-
// OnGetEnvironments callback
189-
private void OnGetEnvironments(DetailedResponse<GetEnvironmentsResponse> resp, IBMError error)
228+
private void OnGetEnvironments(DetailedResponse<ListEnvironmentsResponse> response, IBMError error)
190229
{
191-
Log.Debug("ExampleCallback.OnGetEnvironments()", "Response received: {0}", resp.Response);
230+
Log.Debug("ExampleCallback.OnGetEnvironments()", "Response received: {0}", response.Response);
192231
}
193232
```
194233

195234
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.
196235
```cs
236+
AssistantService assistant;
237+
string assistantVersionDate = "<assistant-version-date>";
238+
Credentials assistantCredentials;
239+
string workspaceId = "<workspaceId>";
240+
241+
DiscoveryService discovery;
242+
string discoveryVersionDate = "<discovery-version-date>";
243+
Credentials discoveryCredentials;
244+
197245
private void Example()
198246
{
247+
assistant = new AssistantService(
248+
versionDate: assistantVersionDate,
249+
credentials: assistantCredentials
250+
);
251+
199252
// Call with generic callbacks
200-
assistant.Message(OnSuccess, "<workspace-id>", "");
201-
discovery.GetEnvironments(OnSuccess);
253+
JObject input = new JObject();
254+
input.Add("text", "");
255+
assistant.Message(
256+
callback: OnSuccess,
257+
workspaceId: workspaceId,
258+
input: input
259+
);
260+
261+
discovery.ListEnvironments(
262+
callback: OnSuccess
263+
);
202264
}
203265

204266
// Generic success callback
@@ -210,12 +272,22 @@ private void OnSuccess<T>(DetailedResponse<T> resp, IBMError error)
210272

211273
You can also use an anonymous callback
212274
```cs
275+
AssistantService assistant;
276+
string assistantVersionDate = "<assistant-version-date>";
277+
Credentials assistantCredentials;
278+
string workspaceId = "<workspaceId>";
279+
213280
private void Example()
214281
{
282+
assistant = new AssistantService(
283+
versionDate: assistantVersionDate,
284+
credentials: assistantCredentials
285+
);
286+
215287
assistant.ListWorkspaces(
216288
callback: (DetailedResponse<WorkspaceCollection> response, IBMError error) =>
217289
{
218-
Log.Debug("xampleCallback.OnSuccess()", "ListWorkspaces result: {0}", response.Response);
290+
Log.Debug("ExampleCallback.OnSuccess()", "ListWorkspaces result: {0}", response.Response);
219291
},
220292
pageLimit: 1,
221293
includeCount: true,
@@ -227,18 +299,26 @@ private void Example()
227299

228300
You can check the `error` response to see if there was an error in the call.
229301
```cs
302+
AssistantService assistant;
303+
string assistantVersionDate = "<assistant-version-date>";
304+
Credentials assistantCredentials;
305+
string workspaceId = "<workspaceId>";
306+
230307
private void Example()
231308
{
232-
// Call with sepcific callbacks
309+
assistant = new AssistantService(
310+
versionDate: assistantVersionDate,
311+
credentials: assistantCredentials
312+
);
313+
233314
assistant.Message(OnMessage, workspaceId);
234315
}
235316

236-
// OnMessage callback
237-
private void OnMessage(DetailedResponse<JObject> resp, IBMError error)
317+
private void OnMessage(DetailedResponse<MessageResponse> response, IBMError error)
238318
{
239-
if(error == null)
319+
if (error == null)
240320
{
241-
Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", resp.Response);
321+
Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", response.Response);
242322
}
243323
else
244324
{
@@ -251,26 +331,52 @@ private void OnMessage(DetailedResponse<JObject> resp, IBMError error)
251331
You can send custom request headers by adding them to the service.
252332

253333
```cs
334+
AssistantService assistant;
335+
string assistantVersionDate = "<assistant-version-date>";
336+
Credentials assistantCredentials;
337+
string workspaceId = "<workspaceId>";
338+
254339
void Example()
255340
{
256-
assistant.AddHeader("X-Watson-Metadata", "customer_id=some-assistant-customer-id");
341+
assistant = new AssistantService(
342+
versionDate: assistantVersionDate,
343+
credentials: assistantCredentials
344+
);
345+
346+
// Add custom header to the REST call
347+
assistant.WithHeader("X-Watson-Metadata", "customer_id=some-assistant-customer-id");
257348
assistant.Message(OnSuccess, "<workspace-id>");
258349
}
350+
351+
private void OnSuccess(DetailedResponse<MessageResponse> response, IBMError error)
352+
{
353+
Log.Debug("ExampleCallback.OnMessage()", "Response received: {0}", response.Response);
354+
}
259355
```
260356

261357
## Response Headers
262358
You can get response headers in the `headers` object in the DetailedResponse.
263359

264360
```cs
361+
AssistantService assistant;
362+
string assistantVersionDate = "<assistant-version-date>";
363+
Credentials assistantCredentials;
364+
string workspaceId = "<workspaceId>";
365+
265366
void Example()
266367
{
368+
assistant = new AssistantService(
369+
versionDate: assistantVersionDate,
370+
credentials: assistantCredentials
371+
);
372+
267373
assistant.Message(OnMessage, "<workspace-id>");
268374
}
269375

270-
private void OnMessage(DetailedResponse<JOBject> resp, IBMError error)
376+
private void OnMessage(DetailedResponse<MessageResponse> response, IBMError error)
271377
{
272378
// List all headers in the response headers object
273-
foreach (KeyValuePair<string, string> kvp in resp.Headers)
379+
foreach (KeyValuePair<string, object> kvp in response.Headers)
274380
{
275381
Log.Debug("ExampleCustomHeader.OnMessage()", "{0}: {1}", kvp.Key, kvp.Value);
276382
}
@@ -283,13 +389,22 @@ Watson services have upgraded their hosts to TLS 1.2. The US South region has a
283389
## Disabling SSL verification
284390
You can disable SSL verifciation when making a service call.
285391
```cs
286-
// Create credential and instantiate service
287-
Credentials credentials = new Credentials(<username>, <password>, <service-url>);
392+
AssistantService assistant;
393+
string assistantVersionDate = "<assistant-version-date>";
394+
Credentials assistantCredentials;
395+
string workspaceId = "<workspaceId>";
288396

289-
credentials.DisableSslVerification = true;
290-
_service = new Assistant(credentials);
291-
_service.VersionDate = <version-date>;
292-
_service.DisableSslVerification = true;
397+
void Example()
398+
{
399+
credentials.DisableSslVerification = true;
400+
assistant = new AssistantService(
401+
versionDate: assistantVersionDate,
402+
credentials: assistantCredentials
403+
);
404+
405+
// disable ssl verification
406+
assistant.DisableSslVerification = true;
407+
}
293408
```
294409

295410
## IBM Cloud Private

0 commit comments

Comments
 (0)