Skip to content

Commit 807bf77

Browse files
committed
chore(WatsonService): Do not attemtpt to load credentials if no credentials files are found. Throw a
1 parent 33fdd46 commit 807bf77

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

src/IBM.WatsonDeveloperCloud/Service/WatsonService.cs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,43 @@ protected WatsonService(string serviceName)
9393
this.Client = new WatsonHttpClient();
9494
ServiceName = serviceName;
9595

96-
foreach (string path in Utility.GetCredentialsPaths())
97-
{
98-
if (Utility.LoadEnvFile(path))
99-
{
100-
break;
96+
var credentialsPaths = Utility.GetCredentialsPaths();
97+
if (credentialsPaths.Count > 0)
98+
{
99+
foreach (string path in credentialsPaths)
100+
{
101+
if (Utility.LoadEnvFile(path))
102+
{
103+
break;
104+
}
101105
}
102-
}
103106

104-
ApiKey = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_APIKEY");
105-
Endpoint = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_URL");
106-
107-
if (string.IsNullOrEmpty(ApiKey))
108-
{
109-
throw new NullReferenceException(ServiceName.ToUpper() + "_APIKEY did not exist. Please add credentials with this key in ibm-credentials.env");
110-
}
111-
112-
if (string.IsNullOrEmpty(Endpoint))
113-
{
114-
throw new NullReferenceException(ServiceName.ToUpper() + "_URL did not exist. Please add url with this key in ibm-credentials.env");
115-
}
116-
117-
TokenOptions tokenOptions = new TokenOptions()
118-
{
119-
IamApiKey = ApiKey,
120-
ServiceUrl = Endpoint
121-
};
122-
123-
SetCredential(tokenOptions);
107+
try
108+
{
109+
ApiKey = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_APIKEY");
110+
}
111+
catch(Exception e)
112+
{
113+
throw new NullReferenceException(string.Format("{0}_APIKEY did not exist. Please add credentials with this key in ibm-credentials.env. Error: {1}", ServiceName.ToUpper(), e.Message));
114+
}
115+
116+
try
117+
{
118+
Endpoint = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_URL");
119+
}
120+
catch(Exception e)
121+
{
122+
throw new NullReferenceException(string.Format("{0}_URL did not exist. Please add url with this key in ibm-credentials.env. Error: {1}", ServiceName.ToUpper(), e.Message));
123+
}
124+
125+
TokenOptions tokenOptions = new TokenOptions()
126+
{
127+
IamApiKey = ApiKey,
128+
ServiceUrl = Endpoint
129+
};
130+
131+
SetCredential(tokenOptions);
132+
}
124133
}
125134

126135
protected WatsonService(string serviceName, string url)

0 commit comments

Comments
 (0)