Skip to content

Commit 98b2afc

Browse files
committed
chore: Look for username and password in credentials env file
1 parent 1b4e7be commit 98b2afc

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/IBM.WatsonDeveloperCloud/Service/WatsonService.cs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,51 @@ protected WatsonService(string serviceName)
104104
}
105105
}
106106

107-
ApiKey = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_APIKEY");
108-
if (string.IsNullOrEmpty(ApiKey))
107+
string apiKey = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_APIKEY");
108+
if (!string.IsNullOrEmpty(apiKey))
109+
ApiKey = apiKey;
110+
string un = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_USERNAME");
111+
if (!string.IsNullOrEmpty(un))
112+
UserName = un;
113+
string pw = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_PASSWORD");
114+
if (!string.IsNullOrEmpty(pw))
115+
Password = pw;
116+
string ServiceUrl = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_URL");
117+
118+
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)))
109119
{
110-
throw new NullReferenceException(string.Format("{0}_APIKEY did not exist. Please add credentials with this key in ibm-credentials.env.", ServiceName.ToUpper()));
120+
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.", ServiceName.ToUpper()));
111121
}
112122

113-
Endpoint = Environment.GetEnvironmentVariable(ServiceName.ToUpper() + "_URL");
114-
if (string.IsNullOrEmpty(Endpoint))
123+
if (!string.IsNullOrEmpty(ApiKey))
115124
{
116-
throw new NullReferenceException(string.Format("{0}_URL did not exist. Please add url with this key in ibm-credentials.env.", ServiceName.ToUpper()));
125+
TokenOptions tokenOptions = new TokenOptions()
126+
{
127+
IamApiKey = ApiKey
128+
};
129+
130+
if (!string.IsNullOrEmpty(ServiceUrl))
131+
tokenOptions.ServiceUrl = ServiceUrl;
132+
133+
if (!string.IsNullOrEmpty(tokenOptions.ServiceUrl))
134+
{
135+
Endpoint = tokenOptions.ServiceUrl;
136+
}
137+
else
138+
{
139+
tokenOptions.ServiceUrl = Url;
140+
}
141+
142+
SetCredential(tokenOptions);
117143
}
118144

119-
TokenOptions tokenOptions = new TokenOptions()
145+
if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
120146
{
121-
IamApiKey = ApiKey,
122-
ServiceUrl = Endpoint
123-
};
147+
if (!string.IsNullOrEmpty(ServiceUrl))
148+
Endpoint = ServiceUrl;
124149

125-
SetCredential(tokenOptions);
150+
SetCredential(UserName, Password);
151+
}
126152
}
127153
}
128154

0 commit comments

Comments
 (0)