forked from watson-developer-cloud/unity-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleDiscoveryV1.cs
More file actions
92 lines (81 loc) · 3.01 KB
/
Copy pathExampleDiscoveryV1.cs
File metadata and controls
92 lines (81 loc) · 3.01 KB
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
using IBM.Watson.Discovery.V1;
using IBM.Watson.Discovery.V1.Model;
using IBM.Cloud.SDK.Utilities;
using IBM.Cloud.SDK.Authentication;
using IBM.Cloud.SDK.Authentication.Iam;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using IBM.Cloud.SDK;
public class ExampleDiscoveryV1 : MonoBehaviour
{
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
[Space(10)]
[Tooltip("The IAM apikey.")]
[SerializeField]
private string iamApikey;
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/discovery/api\"")]
[SerializeField]
private string serviceUrl;
[Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")]
[SerializeField]
private string versionDate;
#endregion
private DiscoveryService service;
// Start is called before the first frame update
void Start()
{
LogSystem.InstallDefaultReactors();
Runnable.Run(CreateService());
}
// Update is called once per frame
public IEnumerator CreateService()
{
if (string.IsNullOrEmpty(iamApikey))
{
throw new IBMException("Plesae provide IAM ApiKey for the service.");
}
// Create credential and instantiate service
IamAuthenticator authenticator = new IamAuthenticator(apikey: iamApikey);
// Wait for tokendata
while (!authenticator.CanAuthenticate())
yield return null;
service = new DiscoveryService(versionDate, authenticator);
if (!string.IsNullOrEmpty(serviceUrl))
{
service.SetServiceUrl(serviceUrl);
}
Runnable.Run(ExampleCreateEnvironment());
Runnable.Run(ExampleListEnvironments());
}
private IEnumerator ExampleCreateEnvironment()
{
ModelEnvironment createEnvironmentResponse = null;
service.CreateEnvironment(
callback: (DetailedResponse<ModelEnvironment> response, IBMError error) =>
{
Log.Debug("DiscoveryServiceV1", "CreateEnvironment result: {0}", response.Response);
createEnvironmentResponse = response.Result;
// environmentId = createEnvironmentResponse.EnvironmentId;
},
name: "my_environment",
description: "My environment"
);
while (createEnvironmentResponse == null)
yield return null;
}
private IEnumerator ExampleListEnvironments()
{
Log.Debug("DiscoveryServiceV1", "ListEnvironments");
ListEnvironmentsResponse listEnvironmentsResponse = null;
service.ListEnvironments(
callback: (DetailedResponse<ListEnvironmentsResponse> response, IBMError error) =>
{
Log.Debug("DiscoveryServiceV1", "ListEnvironments result: {0}", response.Response);
listEnvironmentsResponse = response.Result;
}
);
while (listEnvironmentsResponse == null)
yield return null;
}
}