Skip to content

Commit 4f4f823

Browse files
authored
Merge pull request #236 from watson-developer-cloud/feature-user-set-endpoint
User set endpoint
2 parents 5ea7a4f + 6e06d41 commit 4f4f823

File tree

9 files changed

+53
-28
lines changed

9 files changed

+53
-28
lines changed

src/IBM.WatsonDeveloperCloud/Service/IWatsonService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
*/
1717

1818
using IBM.WatsonDeveloperCloud.Http;
19-
19+
using IBM.WatsonDeveloperCloud.Util;
20+
2021
namespace IBM.WatsonDeveloperCloud.Service
2122
{
2223
public interface IWatsonService
@@ -25,10 +26,12 @@ public interface IWatsonService
2526

2627
string ServiceName { get; set; }
2728
string ApiKey { get; set; }
28-
string Endpoint { get; set; }
2929
string UserName { get; set; }
3030
string Password { get; set; }
3131

3232
void SetCredential(string userName, string password);
33+
void SetCredential(string apikey);
34+
void SetCredential(TokenOptions tokenOptions);
35+
void SetEndpoint(string endpoint);
3336
}
34-
}
37+
}

src/IBM.WatsonDeveloperCloud/Service/WatsonService.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ namespace IBM.WatsonDeveloperCloud.Service
2424
public abstract class WatsonService : IWatsonService
2525
{
2626
const string PATH_AUTHORIZATION_V1_TOKEN = "/authorization/api/v1/token";
27-
27+
2828
public IClient Client { get; set; }
2929

3030
public string ServiceName { get; set; }
3131
public string ApiKey { get; set; }
32-
public string Endpoint { get
32+
protected string Endpoint { get
3333
{
3434
if (this.Client.BaseClient == null ||
3535
this.Client.BaseClient.BaseAddress == null)
@@ -45,6 +45,7 @@ public string Endpoint { get
4545
public string UserName { get; set; }
4646
public string Password { get; set; }
4747
protected TokenManager _tokenManager = null;
48+
protected bool _userSetEndpoint = false;
4849

4950
protected WatsonService(string serviceName)
5051
{
@@ -81,22 +82,45 @@ protected WatsonService(string serviceName, string url, IClient httpClient)
8182
//this.Endpoint = CredentialUtils.GetApiUrl(serviceName);
8283
}
8384

85+
/// <summary>
86+
/// Sets the username and password credentials.
87+
/// </summary>
88+
/// <param name="userName">The username</param>
89+
/// <param name="password">The password</param>
8490
public void SetCredential(string userName, string password)
8591
{
8692
this.UserName = userName;
8793
this.Password = password;
8894
}
8995

96+
/// <summary>
97+
/// Sets the apikey of the service.
98+
/// Also sets the endpoint if the user has not set the endpoint.
99+
/// </summary>
100+
/// <param name="apikey"></param>
90101
public void SetCredential(string apikey)
91102
{
92103
this.ApiKey = apikey;
93-
this.Endpoint = "https://gateway-a.watsonplatform.net/visual-recognition/api";
104+
if (!_userSetEndpoint)
105+
this.Endpoint = "https://gateway-a.watsonplatform.net/visual-recognition/api";
94106
}
95107

108+
/// <summary>
109+
/// Sets the tokenOptions for the service.
110+
/// Also sets the endpoint if the user has not set the endpoint.
111+
/// </summary>
112+
/// <param name="options"></param>
96113
public void SetCredential(TokenOptions options)
97114
{
98-
this.Endpoint = options.IamUrl;
115+
if(!_userSetEndpoint)
116+
this.Endpoint = options.IamUrl;
99117
_tokenManager = new TokenManager(options);
100118
}
119+
120+
public void SetEndpoint(string url)
121+
{
122+
_userSetEndpoint = true;
123+
this.Endpoint = url;
124+
}
101125
}
102-
}
126+
}

test/IBM.WatsonDeveloperCloud.Assistant.v1.IntegrationTests/AssistantServiceIntegrationTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ public void Setup()
9494
}
9595
#endregion
9696

97-
_service = new AssistantService(_username, _password, "2018-02-16")
98-
{
99-
Endpoint = _endpoint
100-
};
97+
_service = new AssistantService(_username, _password, "2018-02-16");
98+
_service.SetEndpoint(_endpoint);
10199

102100
#if DELETE_DOTNET_WORKSPACES
103101
var workspaces = _service.ListWorkspaces();

test/IBM.WatsonDeveloperCloud.Discovery.v1.IntegrationTests/DiscoveryIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void Setup()
9797
#endregion
9898

9999
_service = new DiscoveryService(_username, _password, version);
100-
_service.Endpoint = _endpoint;
100+
_service.SetEndpoint(_endpoint);
101101

102102

103103
var environments = ListEnvironments();

test/IBM.WatsonDeveloperCloud.LanguageTranslator.v2.IntegrationTests/LanguageTranslatorServiceIntegrationTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public void Setup()
8282
public void GetIdentifiableLanguages_Sucess()
8383
{
8484
_service =
85-
new LanguageTranslatorService(_username, _password);
86-
_service.Endpoint = _endpoint;
85+
new LanguageTranslatorService(_username, _password);
86+
_service.SetEndpoint(_endpoint);
8787

8888
var results = _service.ListIdentifiableLanguages();
8989

@@ -95,8 +95,8 @@ public void GetIdentifiableLanguages_Sucess()
9595
public void Identify_Sucess()
9696
{
9797
_service =
98-
new LanguageTranslatorService(_username, _password);
99-
_service.Endpoint = _endpoint;
98+
new LanguageTranslatorService(_username, _password);
99+
_service.SetEndpoint(_endpoint);
100100

101101
var results = _service.Identify(_text);
102102

@@ -108,8 +108,8 @@ public void Identify_Sucess()
108108
public void Translate_Sucess()
109109
{
110110
_service =
111-
new LanguageTranslatorService(_username, _password);
112-
_service.Endpoint = _endpoint;
111+
new LanguageTranslatorService(_username, _password);
112+
_service.SetEndpoint(_endpoint);
113113

114114
var translateRequest = new TranslateRequest()
115115
{
@@ -130,8 +130,8 @@ public void Translate_Sucess()
130130
public void LisListModels_Sucess()
131131
{
132132
_service =
133-
new LanguageTranslatorService(_username, _password);
134-
_service.Endpoint = _endpoint;
133+
new LanguageTranslatorService(_username, _password);
134+
_service.SetEndpoint(_endpoint);
135135

136136
var results = _service.ListModels();
137137

@@ -143,8 +143,8 @@ public void LisListModels_Sucess()
143143
public void GetModelDetails_Success()
144144
{
145145
_service =
146-
new LanguageTranslatorService(_username, _password);
147-
_service.Endpoint = _endpoint;
146+
new LanguageTranslatorService(_username, _password);
147+
_service.SetEndpoint(_endpoint);
148148

149149
var results = _service.GetModel(_baseModel);
150150

@@ -157,7 +157,7 @@ public void Model_Success()
157157
{
158158
_service =
159159
new LanguageTranslatorService(_username, _password);
160-
_service.Endpoint = _endpoint;
160+
_service.SetEndpoint(_endpoint);
161161

162162
TranslationModel createModelResult;
163163

test/IBM.WatsonDeveloperCloud.NLC.v1.IntegrationTests/NaturalLanguageClassifierServiceIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void Setup()
7676
#endregion
7777

7878
_service = new NaturalLanguageClassifierService(_username, _password);
79-
_service.Endpoint = _endpoint;
79+
_service.SetEndpoint(_endpoint);
8080
}
8181

8282
#region Classifiers

test/IBM.WatsonDeveloperCloud.NaturalLanguageUnderstanding.v1.IntTests/NaturalLanguageUnderstandingIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void Setup()
7373
#endregion
7474

7575
_service = new NaturalLanguageUnderstandingService(_username, _password, "2017-02-27");
76-
_service.Endpoint = _endpoint;
76+
_service.SetEndpoint(_endpoint);
7777
}
7878

7979
[TestMethod]

test/IBM.WatsonDeveloperCloud.SpeechToText.v1.IntegrationTests/SpeechToTextServiceIntegrationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void Setup()
8888
#endregion
8989

9090
_service = new SpeechToTextService(_username, _password);
91-
_service.Endpoint = _endpoint;
91+
_service.SetEndpoint(_endpoint);
9292
}
9393

9494
#region Models

test/IBM.WatsonDeveloperCloud.TextToSpeech.v1.IntegrationTests/TextToSpeechServiceIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void Setup()
7878
#endregion
7979

8080
_service = new TextToSpeechService(_username, _password);
81-
_service.Endpoint = _endpoint;
81+
_service.SetEndpoint(_endpoint);
8282
}
8383

8484
#region Voices

0 commit comments

Comments
 (0)