From 5de5b211e96e8d8bc724947eec6b47717811268f Mon Sep 17 00:00:00 2001 From: Mike Kistler Date: Mon, 11 Feb 2019 14:00:55 -0800 Subject: [PATCH] chore: Add setServiceUrl method to base service class --- README.md | 18 +++++++++++++++--- lib/base_service.ts | 12 +++++++++++- test/unit/baseService.test.js | 8 ++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1c974dbc4e..f05495ba89 100755 --- a/README.md +++ b/README.md @@ -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: @@ -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 @@ -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: '' +}); + +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. @@ -204,7 +216,7 @@ assistant.message({ headers: { 'Custom-Header': 'custom', 'Accept-Language': 'custom' - + } }, function(err, result, response) { if (err) diff --git a/lib/base_service.ts b/lib/base_service.ts index c496ef0243..8967b52187 100644 --- a/lib/base_service.ts +++ b/lib/base_service.ts @@ -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 * @@ -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 diff --git a/test/unit/baseService.test.js b/test/unit/baseService.test.js index 93cd483745..b3e1fa90c1 100644 --- a/test/unit/baseService.test.js +++ b/test/unit/baseService.test.js @@ -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';