Skip to content

Commit 10782d7

Browse files
authored
Merge pull request #835 from watson-developer-cloud/mdk/set-service-url-method
chore: Add setServiceUrl method to base service class
2 parents 32d0ea0 + 5de5b21 commit 10782d7

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ npm install watson-developer-cloud
5757

5858
## Usage
5959

60-
The [examples][examples] folder has basic and advanced examples. The examples within each service assume that you already have [service credentials](#getting-credentials).
60+
The [examples][examples] folder has basic and advanced examples. The examples within each service assume that you already have [service credentials](#getting-credentials).
6161

6262
Credentials are checked for in the following order:
6363

@@ -69,7 +69,7 @@ Credentials are checked for in the following order:
6969

7070
3. IBM-Cloud-supplied credentials (via the `VCAP_SERVICES` JSON-encoded environment property)
7171

72-
If you run your app in IBM Cloud, the SDK gets credentials from the [`VCAP_SERVICES`][vcap_services] environment variable.
72+
If you run your app in IBM Cloud, the SDK gets credentials from the [`VCAP_SERVICES`][vcap_services] environment variable.
7373

7474
### Client-side usage
7575

@@ -187,6 +187,18 @@ var discovery = new DiscoveryV1({
187187
});
188188
```
189189

190+
### Setting the service URL
191+
192+
You can set the service URL by calling the setServiceUrl() method.
193+
194+
```javascript
195+
const discovery = new DiscoveryV1({
196+
version: '<version-date>'
197+
});
198+
199+
discovery.setServiceUrl('https://gateway-wdc.watsonplatform.net/discovery/api');
200+
```
201+
190202
### Sending request headers
191203

192204
Custom headers can be passed with any request. Each method has an optional parameter `headers` which can be used to pass in these custom headers, which can override headers that we use as parameters.
@@ -204,7 +216,7 @@ assistant.message({
204216
headers: {
205217
'Custom-Header': 'custom',
206218
'Accept-Language': 'custom'
207-
219+
208220
}
209221
}, function(err, result, response) {
210222
if (err)

lib/base_service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ export class BaseService {
186186
this._options.rejectUnauthorized = !options.disable_ssl_verification;
187187
}
188188

189+
/**
190+
* Set the URL of the service instance.
191+
*
192+
* @param {string} url - The url of the service instance
193+
* @returns {void}
194+
*/
195+
public setServiceUrl(url: string) {
196+
this._options.url = stripTrailingSlash(url);
197+
}
198+
189199
/**
190200
* Retrieve this service's credentials - useful for passing to the authorization service
191201
*
@@ -265,7 +275,7 @@ export class BaseService {
265275

266276
/**
267277
* Wrapper around `sendRequest` that determines whether or not IAM tokens
268-
* are being used to authenticate the request. If so, the token is
278+
* are being used to authenticate the request. If so, the token is
269279
* retrieved by the token manager.
270280
*
271281
* @param {Object} parameters - service request options passed in by user

test/unit/baseService.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ describe('BaseService', function() {
9090
expect(actual).toEqual(expected);
9191
});
9292

93+
it('should allow URL to be changed after service init', function() {
94+
const instance = new TestService({ username: 'user', password: 'pass' });
95+
const expected = 'https://gateway-wdc.watsonplatform.net/test/api';
96+
instance.setServiceUrl(expected);
97+
const actual = instance.getCredentials()['url'];
98+
expect(actual).toEqual(expected);
99+
});
100+
93101
it('should return credentials and url from the environment', function() {
94102
process.env.TEST_USERNAME = 'env_user';
95103
process.env.TEST_PASSWORD = 'env_pass';

0 commit comments

Comments
 (0)