Skip to content

Commit 4df45e7

Browse files
committed
docs: add examples for discovery v2
1 parent 4e8d68b commit 4df45e7

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Examples/ExampleDiscoveryV2.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using IBM.Watson.Discovery.V2;
2+
using IBM.Watson.Discovery.V2.Model;
3+
using IBM.Cloud.SDK.Utilities;
4+
using IBM.Cloud.SDK.Authentication;
5+
using IBM.Cloud.SDK.Authentication.Bearer;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using UnityEngine;
9+
using IBM.Cloud.SDK;
10+
11+
public class ExampleDiscoveryV2 : MonoBehaviour
12+
{
13+
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
14+
[Space(10)]
15+
[Tooltip("The Bearer Token.")]
16+
[SerializeField]
17+
private string bearerToken;
18+
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/discovery/api\"")]
19+
[SerializeField]
20+
private string serviceUrl;
21+
[Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")]
22+
[SerializeField]
23+
private string versionDate;
24+
#endregion
25+
26+
private DiscoveryService service;
27+
// Start is called before the first frame update
28+
void Start()
29+
{
30+
LogSystem.InstallDefaultReactors();
31+
Runnable.Run(CreateService());
32+
}
33+
34+
// Update is called once per frame
35+
public IEnumerator CreateService()
36+
{
37+
if (string.IsNullOrEmpty(bearerToken))
38+
{
39+
throw new IBMException("Plesae provide Bearer Token for the service.");
40+
}
41+
42+
// Option 1
43+
// Create credential and instantiate service using bearer token
44+
BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(bearerToken: bearerToken);
45+
46+
// Otions 2
47+
// Create credential and instantiate service using username/password
48+
// var authenticator = new CloudPakForDataAuthenticator(
49+
// url: "https://{cpd_cluster_host}{:port}",
50+
// username: "{username}",
51+
// password: "{password}"
52+
// );
53+
54+
// Wait for tokendata
55+
while (!authenticator.CanAuthenticate())
56+
yield return null;
57+
58+
service = new DiscoveryService(versionDate, authenticator);
59+
service.SetServiceUrl("service_url");
60+
61+
62+
Runnable.Run(ExampleListCollections());
63+
}
64+
65+
private IEnumerator ExampleListCollections()
66+
{
67+
Log.Debug("DiscoveryServiceV2", "ListCollections");
68+
ListCollectionsResponse listCollectionsResponse = null;
69+
service.ListCollections(
70+
callback: (DetailedResponse<ListCollectionsResponse> response, IBMError error) =>
71+
{
72+
Log.Debug("DiscoveryServiceV2", "ListCollections result: {0}", response.Response);
73+
listCollectionsResponse = response.Result;
74+
},
75+
projectId: "{project_id}"
76+
);
77+
78+
while (listCollectionsResponse == null)
79+
yield return null;
80+
}
81+
}

Examples/ExampleDiscoveryV2.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Use this SDK to build Watson-powered applications in Unity.
1212
* [Before you begin](#before-you-begin)
1313
* [Getting the Watson SDK and adding it to Unity](#getting-the-watson-sdk-and-adding-it-to-unity)
1414
* [Installing the SDK source into your Unity project](#installing-the-sdk-source-into-your-unity-project)
15+
* [Discovery v2 only on CP4D](#discovery-v2-only-on-cp4d)
1516
* [Configuring your service credentials](#configuring-your-service-credentials)
1617
* [Authentication](#authentication)
1718
* [Watson Services](#watson-services)
@@ -43,6 +44,9 @@ You can get the latest SDK release by clicking [here][latest_release_sdk]. **You
4344
### Installing the SDK source into your Unity project
4445
Move the **`unity-sdk`** and **`unity-sdk-core`** directories into the **`Assets`** directory of your Unity project. _Optional: rename the SDK directory from `unity-sdk` to `Watson` and the Core directory from `unity-sdk-core` to `IBMSdkCore`_.
4546

47+
## Discovery v2 only on CP4D
48+
49+
Discovery v2 is only available on Cloud Pak for Data.
4650

4751
## Configuring your service credentials
4852
To create instances of Watson services and their credentials, follow the steps below.

0 commit comments

Comments
 (0)