Skip to content

Commit

Permalink
Merge pull request #835 from watson-developer-cloud/mdk/set-service-u…
Browse files Browse the repository at this point in the history
…rl-method

chore: Add setServiceUrl method to base service class
  • Loading branch information
dpopp07 committed Feb 12, 2019
2 parents 32d0ea0 + 5de5b21 commit 10782d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
18 changes: 15 additions & 3 deletions README.md
Expand Up @@ -57,7 +57,7 @@ npm install watson-developer-cloud

## Usage

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

Credentials are checked for in the following order:

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

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

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

### Client-side usage

Expand Down Expand Up @@ -187,6 +187,18 @@ var discovery = new DiscoveryV1({
});
```

### Setting the service URL

You can set the service URL by calling the setServiceUrl() method.

```javascript
const discovery = new DiscoveryV1({
version: '<version-date>'
});

discovery.setServiceUrl('https://gateway-wdc.watsonplatform.net/discovery/api');
```

### Sending request headers

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.
Expand All @@ -204,7 +216,7 @@ assistant.message({
headers: {
'Custom-Header': 'custom',
'Accept-Language': 'custom'

}
}, function(err, result, response) {
if (err)
Expand Down
12 changes: 11 additions & 1 deletion lib/base_service.ts
Expand Up @@ -186,6 +186,16 @@ export class BaseService {
this._options.rejectUnauthorized = !options.disable_ssl_verification;
}

/**
* Set the URL of the service instance.
*
* @param {string} url - The url of the service instance
* @returns {void}
*/
public setServiceUrl(url: string) {
this._options.url = stripTrailingSlash(url);
}

/**
* Retrieve this service's credentials - useful for passing to the authorization service
*
Expand Down Expand Up @@ -265,7 +275,7 @@ export class BaseService {

/**
* Wrapper around `sendRequest` that determines whether or not IAM tokens
* are being used to authenticate the request. If so, the token is
* are being used to authenticate the request. If so, the token is
* retrieved by the token manager.
*
* @param {Object} parameters - service request options passed in by user
Expand Down
8 changes: 8 additions & 0 deletions test/unit/baseService.test.js
Expand Up @@ -90,6 +90,14 @@ describe('BaseService', function() {
expect(actual).toEqual(expected);
});

it('should allow URL to be changed after service init', function() {
const instance = new TestService({ username: 'user', password: 'pass' });
const expected = 'https://gateway-wdc.watsonplatform.net/test/api';
instance.setServiceUrl(expected);
const actual = instance.getCredentials()['url'];
expect(actual).toEqual(expected);
});

it('should return credentials and url from the environment', function() {
process.env.TEST_USERNAME = 'env_user';
process.env.TEST_PASSWORD = 'env_pass';
Expand Down

0 comments on commit 10782d7

Please sign in to comment.