Skip to content

Commit 32d5b11

Browse files
authored
Merge pull request #512 from watson-developer-cloud/5979-load-credentials-from-file
Enabled loading credentials from an external ibm-credentials.env file
2 parents 1341118 + dd73e6d commit 32d5b11

File tree

27 files changed

+967
-21
lines changed

27 files changed

+967
-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: 52 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,54 @@ 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+
foreach (string path in credentialsPaths)
109+
{
110+
if (Utility.LoadEnvFile(path))
111+
{
112+
break;
113+
}
114+
}
115+
116+
string ApiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
117+
string Username = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
118+
string Password = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
119+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
120+
121+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
122+
{
123+
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()));
124+
}
125+
126+
if (!string.IsNullOrEmpty(ApiKey))
127+
{
128+
TokenOptions tokenOptions = new TokenOptions()
129+
{
130+
IamApiKey = ApiKey
131+
};
132+
133+
Credentials = new Credentials(tokenOptions, ServiceUrl);
134+
135+
if (string.IsNullOrEmpty(Credentials.Url))
136+
{
137+
Credentials.Url = Url;
138+
}
139+
}
140+
141+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
142+
{
143+
Credentials = new Credentials(Username, Password, Url);
144+
}
145+
}
146+
}
147+
99148
/// <summary>
100149
/// Assistant constructor.
101150
/// </summary>
@@ -104,12 +153,12 @@ public Assistant(Credentials credentials)
104153
{
105154
if (credentials.HasCredentials() || credentials.HasWatsonAuthenticationToken() || credentials.HasIamTokenData())
106155
{
107-
Credentials = credentials;
108-
109156
if (string.IsNullOrEmpty(credentials.Url))
110157
{
111158
credentials.Url = Url;
112159
}
160+
161+
Credentials = credentials;
113162
}
114163
else
115164
{

Scripts/Services/Assistant/v2/Assistant.cs

Lines changed: 50 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,54 @@ 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+
foreach (string path in credentialsPaths)
92+
{
93+
if (Utility.LoadEnvFile(path))
94+
{
95+
break;
96+
}
97+
}
98+
99+
string ApiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
100+
string Username = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
101+
string Password = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
102+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
103+
104+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
105+
{
106+
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()));
107+
}
108+
109+
if (!string.IsNullOrEmpty(ApiKey))
110+
{
111+
TokenOptions tokenOptions = new TokenOptions()
112+
{
113+
IamApiKey = ApiKey
114+
};
115+
116+
Credentials = new Credentials(tokenOptions, ServiceUrl);
117+
118+
if (string.IsNullOrEmpty(Credentials.Url))
119+
{
120+
Credentials.Url = Url;
121+
}
122+
}
123+
124+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
125+
{
126+
Credentials = new Credentials(Username, Password, Url);
127+
}
128+
}
129+
}
130+
82131
/// <summary>
83132
/// Assistant constructor.
84133
/// </summary>

Scripts/Services/CompareComply/v1/CompareComply.cs

Lines changed: 50 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,54 @@ 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+
foreach (string path in credentialsPaths)
91+
{
92+
if (Utility.LoadEnvFile(path))
93+
{
94+
break;
95+
}
96+
}
97+
98+
string ApiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
99+
string Username = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
100+
string Password = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
101+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
102+
103+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
104+
{
105+
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()));
106+
}
107+
108+
if (!string.IsNullOrEmpty(ApiKey))
109+
{
110+
TokenOptions tokenOptions = new TokenOptions()
111+
{
112+
IamApiKey = ApiKey
113+
};
114+
115+
Credentials = new Credentials(tokenOptions, ServiceUrl);
116+
117+
if (string.IsNullOrEmpty(Credentials.Url))
118+
{
119+
Credentials.Url = Url;
120+
}
121+
}
122+
123+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
124+
{
125+
Credentials = new Credentials(Username, Password, Url);
126+
}
127+
}
128+
}
129+
81130
/// <summary>
82131
/// CompareComply constructor.
83132
/// </summary>

Scripts/Services/Conversation/v1/Conversation.cs

Lines changed: 50 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,54 @@ 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+
foreach (string path in credentialsPaths)
111+
{
112+
if (Utility.LoadEnvFile(path))
113+
{
114+
break;
115+
}
116+
}
117+
118+
string ApiKey = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_APIKEY");
119+
string Username = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_USERNAME");
120+
string Password = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_PASSWORD");
121+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceId.ToUpper() + "_URL");
122+
123+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
124+
{
125+
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()));
126+
}
127+
128+
if (!string.IsNullOrEmpty(ApiKey))
129+
{
130+
TokenOptions tokenOptions = new TokenOptions()
131+
{
132+
IamApiKey = ApiKey
133+
};
134+
135+
Credentials = new Credentials(tokenOptions, ServiceUrl);
136+
137+
if (string.IsNullOrEmpty(Credentials.Url))
138+
{
139+
Credentials.Url = Url;
140+
}
141+
}
142+
143+
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
144+
{
145+
Credentials = new Credentials(Username, Password, Url);
146+
}
147+
}
148+
}
149+
101150
/// <summary>
102151
/// Conversation constructor
103152
/// </summary>

0 commit comments

Comments
 (0)