-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathAssistantService.cs
776 lines (678 loc) · 35.4 KB
/
AssistantService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/**
* (C) Copyright IBM Corp. 2019, 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* IBM OpenAPI SDK Code Generator Version: 3.46.0-a4e29da0-20220224-210428
*/
using System.Collections.Generic;
using System.Text;
using IBM.Cloud.SDK;
using IBM.Cloud.SDK.Authentication;
using IBM.Cloud.SDK.Connection;
using IBM.Cloud.SDK.Utilities;
using IBM.Watson.Assistant.V2.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using UnityEngine.Networking;
namespace IBM.Watson.Assistant.V2
{
public partial class AssistantService : BaseService
{
private const string defaultServiceName = "assistant";
private const string defaultServiceUrl = "https://api.us-south.assistant.watson.cloud.ibm.com";
#region Version
private string version;
/// <summary>
/// Gets and sets the version of the service.
/// Release date of the API version you want to use. Specify dates in YYYY-MM-DD format. The current version is
/// `2021-11-27`.
/// </summary>
public string Version
{
get { return version; }
set { version = value; }
}
#endregion
#region DisableSslVerification
private bool disableSslVerification = false;
/// <summary>
/// Gets and sets the option to disable ssl verification
/// </summary>
public bool DisableSslVerification
{
get { return disableSslVerification; }
set { disableSslVerification = value; }
}
#endregion
/// <summary>
/// AssistantService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-11-27`.</param>
public AssistantService(string version) : this(version, defaultServiceName, ConfigBasedAuthenticatorFactory.GetAuthenticator(defaultServiceName)) {}
/// <summary>
/// AssistantService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-11-27`.</param>
/// <param name="authenticator">The service authenticator.</param>
public AssistantService(string version, Authenticator authenticator) : this(version, defaultServiceName, authenticator) {}
/// <summary>
/// AssistantService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-11-27`.</param>
/// <param name="serviceName">The service name to be used when configuring the client instance</param>
public AssistantService(string version, string serviceName) : this(version, serviceName, ConfigBasedAuthenticatorFactory.GetAuthenticator(serviceName)) {}
/// <summary>
/// AssistantService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-11-27`.</param>
/// <param name="serviceName">The service name to be used when configuring the client instance</param>
/// <param name="authenticator">The service authenticator.</param>
public AssistantService(string version, string serviceName, Authenticator authenticator) : base(authenticator, serviceName)
{
Authenticator = authenticator;
if (string.IsNullOrEmpty(version))
{
throw new ArgumentNullException("`version` is required");
}
else
{
Version = version;
}
if (string.IsNullOrEmpty(GetServiceUrl()))
{
SetServiceUrl(defaultServiceUrl);
}
}
/// <summary>
/// Create a session.
///
/// Create a new session. A session is used to send user input to a skill and receive responses. It also
/// maintains the state of the conversation. A session persists until it is deleted, or until it times out
/// because of inactivity. (For more information, see the
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-settings).
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson
/// Assistant user interface, open the assistant settings and click **API Details**. For information about
/// creating assistants, see the
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task).
///
/// **Note:** Currently, the v2 API does not support creating assistants.</param>
/// <returns><see cref="SessionResponse" />SessionResponse</returns>
public bool CreateSession(Callback<SessionResponse> callback, string assistantId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `CreateSession`");
if (string.IsNullOrEmpty(assistantId))
throw new ArgumentNullException("`assistantId` is required for `CreateSession`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<SessionResponse> req = new RequestObject<SessionResponse>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "CreateSession"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnCreateSessionResponse;
Connector.URL = GetServiceUrl() + string.Format("/v2/assistants/{0}/sessions", assistantId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnCreateSessionResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<SessionResponse> response = new DetailedResponse<SessionResponse>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<SessionResponse>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("AssistantService.OnCreateSessionResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<SessionResponse>)req).Callback != null)
((RequestObject<SessionResponse>)req).Callback(response, resp.Error);
}
/// <summary>
/// Delete session.
///
/// Deletes a session explicitly before it times out. (For more information about the session inactivity
/// timeout, see the [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-settings)).
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson
/// Assistant user interface, open the assistant settings and click **API Details**. For information about
/// creating assistants, see the
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task).
///
/// **Note:** Currently, the v2 API does not support creating assistants.</param>
/// <param name="sessionId">Unique identifier of the session.</param>
/// <returns><see cref="object" />object</returns>
public bool DeleteSession(Callback<object> callback, string assistantId, string sessionId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `DeleteSession`");
if (string.IsNullOrEmpty(assistantId))
throw new ArgumentNullException("`assistantId` is required for `DeleteSession`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(sessionId))
throw new ArgumentNullException("`sessionId` is required for `DeleteSession`");
RequestObject<object> req = new RequestObject<object>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbDELETE,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "DeleteSession"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnDeleteSessionResponse;
Connector.URL = GetServiceUrl() + string.Format("/v2/assistants/{0}/sessions/{1}", assistantId, sessionId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnDeleteSessionResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<object> response = new DetailedResponse<object>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<object>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("AssistantService.OnDeleteSessionResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<object>)req).Callback != null)
((RequestObject<object>)req).Callback(response, resp.Error);
}
/// <summary>
/// Send user input to assistant (stateful).
///
/// Send user input to an assistant and receive a response, with conversation state (including context data)
/// stored by Watson Assistant for the duration of the session.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson
/// Assistant user interface, open the assistant settings and click **API Details**. For information about
/// creating assistants, see the
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task).
///
/// **Note:** Currently, the v2 API does not support creating assistants.</param>
/// <param name="sessionId">Unique identifier of the session.</param>
/// <param name="input">An input object that includes the input text. (optional)</param>
/// <param name="context">Context data for the conversation. You can use this property to set or modify context
/// variables, which can also be accessed by dialog nodes. The context is stored by the assistant on a
/// per-session basis.
///
/// **Note:** The total size of the context data stored for a stateful session cannot exceed 100KB.
/// (optional)</param>
/// <param name="userId">A string value that identifies the user who is interacting with the assistant. The
/// client must provide a unique identifier for each individual end user who accesses the application. For
/// user-based plans, this user ID is used to identify unique users for billing purposes. This string cannot
/// contain carriage return, newline, or tab characters. If no value is specified in the input, **user_id** is
/// automatically set to the value of **context.global.session_id**.
///
/// **Note:** This property is the same as the **user_id** property in the global system context. If **user_id**
/// is specified in both locations, the value specified at the root is used. (optional)</param>
/// <returns><see cref="MessageResponse" />MessageResponse</returns>
public bool Message(Callback<MessageResponse> callback, string assistantId, string sessionId, MessageInput input = null, MessageContext context = null, string userId = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `Message`");
if (string.IsNullOrEmpty(assistantId))
throw new ArgumentNullException("`assistantId` is required for `Message`");
if (string.IsNullOrEmpty(sessionId))
throw new ArgumentNullException("`sessionId` is required for `Message`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<MessageResponse> req = new RequestObject<MessageResponse>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "Message"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
JObject bodyObject = new JObject();
if (input != null)
bodyObject["input"] = JToken.FromObject(input);
if (context != null)
bodyObject["context"] = JToken.FromObject(context);
if (!string.IsNullOrEmpty(userId))
bodyObject["user_id"] = userId;
req.Send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bodyObject));
req.OnResponse = OnMessageResponse;
Connector.URL = GetServiceUrl() + string.Format("/v2/assistants/{0}/sessions/{1}/message", assistantId, sessionId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnMessageResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<MessageResponse> response = new DetailedResponse<MessageResponse>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<MessageResponse>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("AssistantService.OnMessageResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<MessageResponse>)req).Callback != null)
((RequestObject<MessageResponse>)req).Callback(response, resp.Error);
}
/// <summary>
/// Send user input to assistant (stateless).
///
/// Send user input to an assistant and receive a response, with conversation state (including context data)
/// managed by your application.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson
/// Assistant user interface, open the assistant settings and click **API Details**. For information about
/// creating assistants, see the
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task).
///
/// **Note:** Currently, the v2 API does not support creating assistants.</param>
/// <param name="input">An input object that includes the input text. (optional)</param>
/// <param name="context">Context data for the conversation. You can use this property to set or modify context
/// variables, which can also be accessed by dialog nodes. The context is not stored by the assistant. To
/// maintain session state, include the context from the previous response.
///
/// **Note:** The total size of the context data for a stateless session cannot exceed 250KB. (optional)</param>
/// <param name="userId">A string value that identifies the user who is interacting with the assistant. The
/// client must provide a unique identifier for each individual end user who accesses the application. For
/// user-based plans, this user ID is used to identify unique users for billing purposes. This string cannot
/// contain carriage return, newline, or tab characters. If no value is specified in the input, **user_id** is
/// automatically set to the value of **context.global.session_id**.
///
/// **Note:** This property is the same as the **user_id** property in the global system context. If **user_id**
/// is specified in both locations in a message request, the value specified at the root is used.
/// (optional)</param>
/// <returns><see cref="MessageResponseStateless" />MessageResponseStateless</returns>
public bool MessageStateless(Callback<MessageResponseStateless> callback, string assistantId, MessageInputStateless input = null, MessageContextStateless context = null, string userId = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `MessageStateless`");
if (string.IsNullOrEmpty(assistantId))
throw new ArgumentNullException("`assistantId` is required for `MessageStateless`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<MessageResponseStateless> req = new RequestObject<MessageResponseStateless>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "MessageStateless"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
JObject bodyObject = new JObject();
if (input != null)
bodyObject["input"] = JToken.FromObject(input);
if (context != null)
bodyObject["context"] = JToken.FromObject(context);
if (!string.IsNullOrEmpty(userId))
bodyObject["user_id"] = userId;
req.Send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bodyObject));
req.OnResponse = OnMessageStatelessResponse;
Connector.URL = GetServiceUrl() + string.Format("/v2/assistants/{0}/message", assistantId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnMessageStatelessResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<MessageResponseStateless> response = new DetailedResponse<MessageResponseStateless>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<MessageResponseStateless>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("AssistantService.OnMessageStatelessResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<MessageResponseStateless>)req).Callback != null)
((RequestObject<MessageResponseStateless>)req).Callback(response, resp.Error);
}
/// <summary>
/// Identify intents and entities in multiple user utterances.
///
/// Send multiple user inputs to a dialog skill in a single request and receive information about the intents
/// and entities recognized in each input. This method is useful for testing and comparing the performance of
/// different skills or skill versions.
///
/// This method is available only with Enterprise with Data Isolation plans.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="skillId">Unique identifier of the skill. To find the skill ID in the Watson Assistant user
/// interface, open the skill settings and click **API Details**.</param>
/// <param name="input">An array of input utterances to classify. (optional)</param>
/// <returns><see cref="BulkClassifyResponse" />BulkClassifyResponse</returns>
public bool BulkClassify(Callback<BulkClassifyResponse> callback, string skillId, List<BulkClassifyUtterance> input = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `BulkClassify`");
if (string.IsNullOrEmpty(skillId))
throw new ArgumentNullException("`skillId` is required for `BulkClassify`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<BulkClassifyResponse> req = new RequestObject<BulkClassifyResponse>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "BulkClassify"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
JObject bodyObject = new JObject();
if (input != null && input.Count > 0)
bodyObject["input"] = JToken.FromObject(input);
req.Send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bodyObject));
req.OnResponse = OnBulkClassifyResponse;
Connector.URL = GetServiceUrl() + string.Format("/v2/skills/{0}/workspace/bulk_classify", skillId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnBulkClassifyResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<BulkClassifyResponse> response = new DetailedResponse<BulkClassifyResponse>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<BulkClassifyResponse>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("AssistantService.OnBulkClassifyResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<BulkClassifyResponse>)req).Callback != null)
((RequestObject<BulkClassifyResponse>)req).Callback(response, resp.Error);
}
/// <summary>
/// List log events for an assistant.
///
/// List the events from the log of an assistant.
///
/// This method requires Manager access, and is available only with Enterprise plans.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson
/// Assistant user interface, open the assistant settings and click **API Details**. For information about
/// creating assistants, see the
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task).
///
/// **Note:** Currently, the v2 API does not support creating assistants.</param>
/// <param name="sort">How to sort the returned log events. You can sort by **request_timestamp**. To reverse
/// the sort order, prefix the parameter value with a minus sign (`-`). (optional)</param>
/// <param name="filter">A cacheable parameter that limits the results to those matching the specified filter.
/// For more information, see the
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-filter-reference#filter-reference).
/// (optional)</param>
/// <param name="pageLimit">The number of records to return in each page of results. (optional)</param>
/// <param name="cursor">A token identifying the page of results to retrieve. (optional)</param>
/// <returns><see cref="LogCollection" />LogCollection</returns>
public bool ListLogs(Callback<LogCollection> callback, string assistantId, string sort = null, string filter = null, long? pageLimit = null, string cursor = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListLogs`");
if (string.IsNullOrEmpty(assistantId))
throw new ArgumentNullException("`assistantId` is required for `ListLogs`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<LogCollection> req = new RequestObject<LogCollection>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "ListLogs"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
if (!string.IsNullOrEmpty(sort))
{
req.Parameters["sort"] = sort;
}
if (!string.IsNullOrEmpty(filter))
{
req.Parameters["filter"] = filter;
}
if (pageLimit != null)
{
req.Parameters["page_limit"] = pageLimit;
}
if (!string.IsNullOrEmpty(cursor))
{
req.Parameters["cursor"] = cursor;
}
req.OnResponse = OnListLogsResponse;
Connector.URL = GetServiceUrl() + string.Format("/v2/assistants/{0}/logs", assistantId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnListLogsResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<LogCollection> response = new DetailedResponse<LogCollection>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<LogCollection>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("AssistantService.OnListLogsResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<LogCollection>)req).Callback != null)
((RequestObject<LogCollection>)req).Callback(response, resp.Error);
}
/// <summary>
/// Delete labeled data.
///
/// Deletes all data associated with a specified customer ID. The method has no effect if no data is associated
/// with the customer ID.
///
/// You associate a customer ID with data by passing the `X-Watson-Metadata` header with a request that passes
/// data. For more information about personal data and customer IDs, see [Information
/// security](https://cloud.ibm.com/docs/assistant?topic=assistant-information-security#information-security).
///
/// **Note:** This operation is intended only for deleting data associated with a single specific customer, not
/// for deleting data associated with multiple customers or for any other purpose. For more information, see
/// [Labeling and deleting data in Watson
/// Assistant](https://cloud.ibm.com/docs/assistant?topic=assistant-information-security#information-security-gdpr-wa).
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="customerId">The customer ID for which all data is to be deleted.</param>
/// <returns><see cref="object" />object</returns>
public bool DeleteUserData(Callback<object> callback, string customerId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `DeleteUserData`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(customerId))
throw new ArgumentNullException("`customerId` is required for `DeleteUserData`");
RequestObject<object> req = new RequestObject<object>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbDELETE,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "DeleteUserData"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
if (!string.IsNullOrEmpty(customerId))
{
req.Parameters["customer_id"] = customerId;
}
req.OnResponse = OnDeleteUserDataResponse;
Connector.URL = GetServiceUrl() + "/v2/user_data";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnDeleteUserDataResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<object> response = new DetailedResponse<object>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<object>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("AssistantService.OnDeleteUserDataResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<object>)req).Callback != null)
((RequestObject<object>)req).Callback(response, resp.Error);
}
}
}