This is an automated email from the ASF dual-hosted git repository.
jamesthomas pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/incubator-openwhisk-client-js.git
The following commit(s) were added to refs/heads/master by this push:
new 6cf8e35 support setting the apiversion (#151)
6cf8e35 is described below
commit 6cf8e35838702b6477ea2855516adc021f1bb6c1
Author: Shazron Abdullah <[email protected]>
AuthorDate: Tue Mar 12 19:57:06 2019 +0800
support setting the apiversion (#151)
* support setting the apiversion
* Updated README
---
README.md | 1 +
lib/client.js | 12 ++++++++----
test/unit/client.test.js | 13 +++++++++++++
3 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index ee3edd5..b09c3ee 100644
--- a/README.md
+++ b/README.md
@@ -110,6 +110,7 @@ _Client constructor supports the following mandatory
parameters:_
*Client constructor supports the following optional parameters:*
- **api.** Full API URL for OpenWhisk platform, e.g.
`https://openwhisk.ng.bluemix.net/api/v1/`. This value overrides `apihost` if
both are present.
+- **apiversion** Api version for the OpenWhisk platform, e.g. the `v1` in
`https://openwhisk.ng.bluemix.net/api/v1/`, when used with `apihost` (and `api`
is not set)
- **namespace**. Namespace for resource requests, defaults to `_`.
- **ignore_certs**. Turns off server SSL/TLS certificate verification. This
allows the client to be used against local deployments of OpenWhisk with a
self-signed certificate. Defaults to false.
- **apigw_token**. API Gateway service authentication token. This is mandatory
for using an external API Gateway service, rather than the built-in api gateway.
diff --git a/lib/client.js b/lib/client.js
index 70205c4..7e4e10d 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -74,6 +74,7 @@ class Client {
* @param {string} [options.api]
* @param {string} [options.api_key]
* @param {string} [options.apihost]
+ * @param {string} [options.apiversion]
* @param {string} [options.namespace]
* @param {boolean} [options.ignore_certs]
* @param {string} [options.apigw_token]
@@ -94,9 +95,12 @@ class Client {
? process.env['__OW_IGNORE_CERTS'].toLowerCase() === 'true'
: false)
+ // if apiversion is available, use it
+ const apiversion = options.apiversion || 'v1'
+
// if apihost is available, parse this into full API url
const api = options.api ||
- this.urlFromApihost(options.apihost || process.env['__OW_API_HOST'])
+ this.urlFromApihost(options.apihost || process.env['__OW_API_HOST'],
apiversion)
// optional tokens for API GW service
const apigwToken = options.apigw_token || process.env['__OW_APIGW_TOKEN']
@@ -113,12 +117,12 @@ class Client {
throw new Error(`${messages.INVALID_OPTIONS_ERROR} Missing either api or
apihost parameters.`)
}
- return {apiKey: apiKey, api, ignoreCerts: ignoreCerts, namespace:
options.namespace, apigwToken: apigwToken, apigwSpaceGuid: apigwSpaceGuid,
authHandler: options.auth_handler, noUserAgent: options.noUserAgent, cert:
options.cert, key: options.key}
+ return {apiKey: apiKey, api, apiVersion: apiversion, ignoreCerts:
ignoreCerts, namespace: options.namespace, apigwToken: apigwToken,
apigwSpaceGuid: apigwSpaceGuid, authHandler: options.auth_handler, noUserAgent:
options.noUserAgent, cert: options.cert, key: options.key}
}
- urlFromApihost (apihost) {
+ urlFromApihost (apihost, apiversion = 'v1') {
if (!apihost) return apihost
- let url = `${apihost}/api/v1/`
+ let url = `${apihost}/api/${apiversion}/`
// if apihost does not the protocol, assume HTTPS
if (!url.match(/http(s)?:\/\//)) {
diff --git a/test/unit/client.test.js b/test/unit/client.test.js
index d4f7da6..3684108 100644
--- a/test/unit/client.test.js
+++ b/test/unit/client.test.js
@@ -12,6 +12,7 @@ test('should use default constructor options', t => {
const client = new Client({api_key: 'aaa', apihost: 'my_host'})
t.false(client.options.ignoreCerts)
t.is(client.options.apiKey, 'aaa')
+ t.is(client.options.apiVersion, 'v1')
t.is(client.options.api, 'https://my_host/api/v1/')
t.falsy(client.options.namespace)
t.falsy(client.options.cert)
@@ -24,12 +25,14 @@ test('should support explicit constructor options', t => {
ignore_certs: true,
api_key: 'aaa',
api: 'my_host',
+ apiversion: 'v2',
apigw_token: 'oauth_token',
apigw_space_guid: 'space_guid',
cert: 'mycert=',
key: 'mykey='
})
t.is(client.options.api, 'my_host')
+ t.is(client.options.apiVersion, 'v2')
t.true(client.options.ignoreCerts)
t.is(client.options.namespace, 'ns')
t.is(client.options.apigwToken, 'oauth_token')
@@ -38,6 +41,16 @@ test('should support explicit constructor options', t => {
t.is(client.options.key, 'mykey=')
})
+test('apihost and apiversion set', t => {
+ const client = new Client({
+ api_key: 'aaa',
+ apihost: 'https://my_host',
+ apiversion: 'v2'
+ })
+ t.is(client.options.api, 'https://my_host/api/v2/')
+ t.is(client.options.apiVersion, 'v2')
+})
+
test('should support deprecated explicit constructor options', t => {
const client = new Client({
namespace: 'ns',