From 773b2b8ee34998bf20264d43ac59b378b2329326 Mon Sep 17 00:00:00 2001 From: Corentin Ardeois Date: Tue, 6 Sep 2016 11:44:43 -0400 Subject: [PATCH] Add more service abstraction This patch add _requestComponents and serviceEndpoint methods to abstract service, as these methods ared used for both Keystone and Glance services. Change-Id: Iccc334c0bbd79c1207855260932dab984f2d9d6c --- src/glance.js | 45 ---------------------------------- src/keystone.js | 47 ------------------------------------ src/util/abstract_service.js | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 92 deletions(-) diff --git a/src/glance.js b/src/glance.js index bbfa0a1..c869aee 100644 --- a/src/glance.js +++ b/src/glance.js @@ -55,51 +55,6 @@ export default class Glance extends AbstractService { this._config = endpointConfig; } - /** - * This method resolves any passed token into an appropriate header, as well as the base URL - * for the glance API. these variables may then be used to feed other requests. - * - * @param {Promise|String} token A promise, or string, representing a token. - * @returns {Promise} A promise which resolves with [url, token]. - * @private - */ - _requestComponents (token = null) { - // Make sure the token is a promise. - let headerPromise = new Promise((resolve) => resolve(token)) - .then((token) => { - if (token) { - return { - 'X-Auth-Token': token - }; - } - return {}; - }); - return Promise.all([this.serviceEndpoint(), headerPromise]); - } - - /** - * Return the root API endpoint for the current supported glance version. - * - * @returns {Promise.|*} A promise which will resolve with the endpoint URL string. - */ - serviceEndpoint () { - if (!this._endpointPromise) { - this._endpointPromise = this.version() - .then((version) => { - if (version.links) { - for (let i = 0; i < version.links.length; i++) { - let link = version.links[i]; - if (link.rel === 'self' && link.href) { - return link.href; - } - } - } - throw new Error("No service endpoint discovered."); - }); - } - return this._endpointPromise; - } - /** * List the images available on glance. * diff --git a/src/keystone.js b/src/keystone.js index eee0961..029e871 100644 --- a/src/keystone.js +++ b/src/keystone.js @@ -58,30 +58,6 @@ export default class Keystone extends AbstractService { return pointer; } - /** - * This method builds common components of a keystone request. It converts any passed token - * into a promise, resolves the base URL, and then passes the results as an .all() promise, - * which may be destructured in a followup request. - * - * @param {Promise|String} token A promise, or string, representing a token. - * @returns {Promise} A promise which resolves with [url, token]. - * @private - */ - _requestComponents (token = null) { - // Make sure the token is a promise. - let headerPromise = Promise - .resolve(token) - .then((token) => { - if (token) { - return { - 'X-Auth-Token': token - }; - } - return {}; - }); - return Promise.all([this.serviceEndpoint(), headerPromise]); - } - /** * Retrieve all the API versions available. * @@ -92,29 +68,6 @@ export default class Keystone extends AbstractService { .then((versions) => versions.values); } - /** - * Return the root API endpoint for the current supported keystone version. - * - * @returns {Promise.|*} A promise which will resolve with the endpoint URL string. - */ - serviceEndpoint () { - if (!this._endpointPromise) { - this._endpointPromise = this.version() - .then((version) => { - if (version.links) { - for (let i = 0; i < version.links.length; i++) { - let link = version.links[i]; - if (link.rel === 'self' && link.href) { - return link.href; - } - } - } - throw new Error("No service endpoint discovered."); - }); - } - return this._endpointPromise; - } - /** * Issue a token from the provided credentials. Credentials will be read from the * configuration, unless they have been explicitly provided. Note that both the userDomainName diff --git a/src/util/abstract_service.js b/src/util/abstract_service.js index adfebfe..8a8e425 100644 --- a/src/util/abstract_service.js +++ b/src/util/abstract_service.js @@ -80,4 +80,51 @@ export default class AbstractService { throw new Error("No supported API version available."); }); } + + /** + * Return the root API endpoint for the current supported glance version. + * + * @returns {Promise.|*} A promise which will resolve with the endpoint URL string. + */ + serviceEndpoint () { + if (!this._endpointPromise) { + this._endpointPromise = this.version() + .then((version) => { + if (version.links) { + for (let i = 0; i < version.links.length; i++) { + let link = version.links[i]; + if (link.rel === 'self' && link.href) { + return link.href; + } + } + } + throw new Error("No service endpoint discovered."); + }); + } + return this._endpointPromise; + } + + /** + * This method builds common components for a request to the implemented service. + * It converts any passed token into a promise, resolves the base URL, and then passes + * the results as an .all() promise, which may be destructured in a followup request. + * + * @param {Promise|String} token A promise, or string, representing a token. + * @returns {Promise} A promise which resolves with [url, token]. + * @private + */ + _requestComponents (token = null) { + // Make sure the token is a promise. + let headerPromise = Promise + .resolve(token) + .then((token) => { + if (token) { + return { + 'X-Auth-Token': token + }; + } + return {}; + }); + return Promise.all([this.serviceEndpoint(), headerPromise]); + } }