Skip to content

Commit b56bacb

Browse files
committed
feat(All Services): Enabled loading credentials from an external ibm-credentials.env file
1 parent 1341118 commit b56bacb

27 files changed

+1110
-21
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,34 @@ void Start()
179179
}
180180
```
181181

182+
### ibm-credentials.env
183+
You can create an ibm-credentials.env file for authentication. This has the basic format
184+
```
185+
VISUAL_RECOGNITION_APIKEY=<visual-recognition-apikey>
186+
VISUAL_RECOGNITION_URL=<visual-recognition-service-url>
187+
ASSISTANT_APIKEY=<assistant-apikey>
188+
ASSISTANT_URL=<assistant-service-url>
189+
```
190+
The SDK will search for this file in the following order
191+
- Path specified by environmental variable `IBM_CREDENTIALS_FILE`
192+
- System home directory
193+
- Top level of the project directory
194+
195+
Using a `ibm-credentials.env` file you can easily instantiate and authenticate a service. If you are using an IAM Apikey you will need to invoke this using a coroutine to wait for the authorization token.
196+
```cs
197+
public IEnumerator ExampleAutoService()
198+
{
199+
Assistant assistantService = new Assistant();
200+
assistantService.VersionDate = _assistantVersionDate;
201+
202+
// Wait for authorization token
203+
while (!assistantService.Credentials.HasIamTokenData())
204+
yield return null;
205+
206+
var listWorkspacesResult = assistantService.ListWorkspaces();
207+
}
208+
```
209+
182210
## Callbacks
183211
Success and failure callbacks are required. You can specify the return type in the callback.
184212
```cs

Scripts/Services/Assistant/v1/Assistant.cs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
using System;
2525
using MiniJSON;
2626
using UnityEngine.Networking;
27+
using Utility = IBM.Watson.DeveloperCloud.Utilities.Utility;
2728

2829
namespace IBM.Watson.DeveloperCloud.Services.Assistant.v1
2930
{
3031
public class Assistant : IWatsonService
3132
{
32-
private const string ServiceId = "Assistantv1";
33+
private const string ServiceId = "assistant";
3334
private fsSerializer _serializer = new fsSerializer();
3435

3536
private Credentials _credentials = null;
@@ -96,6 +97,65 @@ public bool DisableSslVerification
9697
public delegate void FailCallback(RESTConnector.Error error, Dictionary<string, object> customData);
9798
#endregion
9899

100+
/// <summary>
101+
/// Assistant constructor. Use this constructor to auto load credentials via ibm-credentials.env file.
102+
/// </summary>
103+
public Assistant()
104+
{
105+
var credentialsPaths = Utility.GetCredentialsPaths();
106+
if (credentialsPaths.Count > 0)
107+
{
108+
string ApiKey = "";
109+
string Endpoint = "";
110+
string Username = "";
111+
string Password = "";
112+
113+
foreach (string path in credentialsPaths)
114+
{
115+
if (Utility.LoadEnvFile(path))
116+
{
117+
break;
118+
}
119+
}
120+
121+
string apiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
122+
if (!string.IsNullOrEmpty(apiKey))
123+
ApiKey = apiKey;
124+
string un = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
125+
if (!string.IsNullOrEmpty(un))
126+
Username = un;
127+
string pw = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
128+
if (!string.IsNullOrEmpty(pw))
129+
Password = pw;
130+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
131+
132+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
133+
{
134+
throw new NullReferenceException(string.Format("Either {0}_APIKEY or {0}_USERNAME and {0}_PASSWORD did not exist. Please add credentials with this key in ibm-credentials.env.", ServiceId.ToUpper()));
135+
}
136+
137+
if (!string.IsNullOrEmpty(ApiKey))
138+
{
139+
TokenOptions tokenOptions = new TokenOptions()
140+
{
141+
IamApiKey = ApiKey
142+
};
143+
144+
Credentials = new Credentials(tokenOptions, ServiceUrl);
145+
146+
if (string.IsNullOrEmpty(Credentials.Url))
147+
{
148+
Credentials.Url = Url;
149+
}
150+
}
151+
152+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
153+
{
154+
Credentials = new Credentials(Username, Password, Url);
155+
}
156+
}
157+
}
158+
99159
/// <summary>
100160
/// Assistant constructor.
101161
/// </summary>
@@ -104,12 +164,12 @@ public Assistant(Credentials credentials)
104164
{
105165
if (credentials.HasCredentials() || credentials.HasWatsonAuthenticationToken() || credentials.HasIamTokenData())
106166
{
107-
Credentials = credentials;
108-
109167
if (string.IsNullOrEmpty(credentials.Url))
110168
{
111169
credentials.Url = Url;
112170
}
171+
172+
Credentials = credentials;
113173
}
114174
else
115175
{

Scripts/Services/Assistant/v2/Assistant.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
using System.Collections.Generic;
2525
using System.Text;
2626
using UnityEngine.Networking;
27+
using Utility = IBM.Watson.DeveloperCloud.Utilities.Utility;
2728

2829
namespace IBM.Watson.DeveloperCloud.Services.Assistant.v2
2930
{
3031
public class Assistant : IWatsonService
3132
{
32-
private const string ServiceId = "AssistantV2";
33+
private const string ServiceId = "assistant";
3334
private fsSerializer _serializer = new fsSerializer();
3435

3536
private Credentials _credentials = null;
@@ -79,6 +80,65 @@ public bool DisableSslVerification
7980
set { disableSslVerification = value; }
8081
}
8182

83+
/// <summary>
84+
/// Assistant constructor. Use this constructor to auto load credentials via ibm-credentials.env file.
85+
/// </summary>
86+
public Assistant()
87+
{
88+
var credentialsPaths = Utility.GetCredentialsPaths();
89+
if (credentialsPaths.Count > 0)
90+
{
91+
string ApiKey = "";
92+
string Endpoint = "";
93+
string Username = "";
94+
string Password = "";
95+
96+
foreach (string path in credentialsPaths)
97+
{
98+
if (Utility.LoadEnvFile(path))
99+
{
100+
break;
101+
}
102+
}
103+
104+
string apiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
105+
if (!string.IsNullOrEmpty(apiKey))
106+
ApiKey = apiKey;
107+
string un = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
108+
if (!string.IsNullOrEmpty(un))
109+
Username = un;
110+
string pw = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
111+
if (!string.IsNullOrEmpty(pw))
112+
Password = pw;
113+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
114+
115+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
116+
{
117+
throw new NullReferenceException(string.Format("Either {0}_APIKEY or {0}_USERNAME and {0}_PASSWORD did not exist. Please add credentials with this key in ibm-credentials.env.", ServiceId.ToUpper()));
118+
}
119+
120+
if (!string.IsNullOrEmpty(ApiKey))
121+
{
122+
TokenOptions tokenOptions = new TokenOptions()
123+
{
124+
IamApiKey = ApiKey
125+
};
126+
127+
Credentials = new Credentials(tokenOptions, Endpoint);
128+
129+
if (string.IsNullOrEmpty(Credentials.Url))
130+
{
131+
Credentials.Url = Url;
132+
}
133+
}
134+
135+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
136+
{
137+
Credentials = new Credentials(Username, Password, Url);
138+
}
139+
}
140+
}
141+
82142
/// <summary>
83143
/// Assistant constructor.
84144
/// </summary>

Scripts/Services/CompareComply/v1/CompareComply.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
using System.Collections.Generic;
2424
using System.Text;
2525
using UnityEngine.Networking;
26+
using Utility = IBM.Watson.DeveloperCloud.Utilities.Utility;
2627

2728
namespace IBM.Watson.DeveloperCloud.Services.CompareComply.v1
2829
{
2930
public class CompareComply : IWatsonService
3031
{
31-
private const string ServiceId = "CompareComplyV1";
32+
private const string ServiceId = "compare_comply";
3233
private fsSerializer _serializer = new fsSerializer();
3334

3435
private Credentials credentials = null;
@@ -78,6 +79,65 @@ public bool DisableSslVerification
7879
set { disableSslVerification = value; }
7980
}
8081

82+
/// <summary>
83+
/// CompareComply constructor. Use this constructor to auto load credentials via ibm-credentials.env file.
84+
/// </summary>
85+
public CompareComply()
86+
{
87+
var credentialsPaths = Utility.GetCredentialsPaths();
88+
if (credentialsPaths.Count > 0)
89+
{
90+
string ApiKey = "";
91+
string Endpoint = "";
92+
string Username = "";
93+
string Password = "";
94+
95+
foreach (string path in credentialsPaths)
96+
{
97+
if (Utility.LoadEnvFile(path))
98+
{
99+
break;
100+
}
101+
}
102+
103+
string apiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
104+
if (!string.IsNullOrEmpty(apiKey))
105+
ApiKey = apiKey;
106+
string un = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
107+
if (!string.IsNullOrEmpty(un))
108+
Username = un;
109+
string pw = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
110+
if (!string.IsNullOrEmpty(pw))
111+
Password = pw;
112+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
113+
114+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
115+
{
116+
throw new NullReferenceException(string.Format("Either {0}_APIKEY or {0}_USERNAME and {0}_PASSWORD did not exist. Please add credentials with this key in ibm-credentials.env.", ServiceId.ToUpper()));
117+
}
118+
119+
if (!string.IsNullOrEmpty(ApiKey))
120+
{
121+
TokenOptions tokenOptions = new TokenOptions()
122+
{
123+
IamApiKey = ApiKey
124+
};
125+
126+
Credentials = new Credentials(tokenOptions, ServiceUrl);
127+
128+
if (string.IsNullOrEmpty(Credentials.Url))
129+
{
130+
Credentials.Url = Url;
131+
}
132+
}
133+
134+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
135+
{
136+
Credentials = new Credentials(Username, Password, Url);
137+
}
138+
}
139+
}
140+
81141
/// <summary>
82142
/// CompareComply constructor.
83143
/// </summary>

Scripts/Services/Conversation/v1/Conversation.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using MiniJSON;
2525
using System.Collections.Generic;
2626
using UnityEngine.Networking;
27+
using Utility = IBM.Watson.DeveloperCloud.Utilities.Utility;
2728

2829
namespace IBM.Watson.DeveloperCloud.Services.Conversation.v1
2930
{
@@ -89,7 +90,7 @@ public bool DisableSslVerification
8990
#endregion
9091

9192
#region Private Data
92-
private const string ServiceId = "ConversationV1";
93+
private const string ServiceId = "conversation";
9394
private const string Workspaces = "/v1/workspaces";
9495
private Credentials _credentials = null;
9596
private string _url = "https://gateway.watsonplatform.net/conversation/api";
@@ -98,6 +99,65 @@ public bool DisableSslVerification
9899
#endregion
99100

100101
#region Constructor
102+
/// <summary>
103+
/// Conversation constructor. Use this constructor to auto load credentials via ibm-credentials.env file.
104+
/// </summary>
105+
public Conversation()
106+
{
107+
var credentialsPaths = Utility.GetCredentialsPaths();
108+
if (credentialsPaths.Count > 0)
109+
{
110+
string ApiKey = "";
111+
string Endpoint = "";
112+
string Username = "";
113+
string Password = "";
114+
115+
foreach (string path in credentialsPaths)
116+
{
117+
if (Utility.LoadEnvFile(path))
118+
{
119+
break;
120+
}
121+
}
122+
123+
string apiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
124+
if (!string.IsNullOrEmpty(apiKey))
125+
ApiKey = apiKey;
126+
string un = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
127+
if (!string.IsNullOrEmpty(un))
128+
Username = un;
129+
string pw = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
130+
if (!string.IsNullOrEmpty(pw))
131+
Password = pw;
132+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
133+
134+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
135+
{
136+
throw new NullReferenceException(string.Format("Either {0}_APIKEY or {0}_USERNAME and {0}_PASSWORD did not exist. Please add credentials with this key in ibm-credentials.env.", ServiceId.ToUpper()));
137+
}
138+
139+
if (!string.IsNullOrEmpty(ApiKey))
140+
{
141+
TokenOptions tokenOptions = new TokenOptions()
142+
{
143+
IamApiKey = ApiKey
144+
};
145+
146+
Credentials = new Credentials(tokenOptions, ServiceUrl);
147+
148+
if (string.IsNullOrEmpty(Credentials.Url))
149+
{
150+
Credentials.Url = Url;
151+
}
152+
}
153+
154+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
155+
{
156+
Credentials = new Credentials(Username, Password, Url);
157+
}
158+
}
159+
}
160+
101161
/// <summary>
102162
/// Conversation constructor
103163
/// </summary>

0 commit comments

Comments
 (0)