Merge "Passed tokens may now be promises."

This commit is contained in:
Jenkins 2016-09-02 16:15:52 +00:00 committed by Gerrit Code Review
commit 50c952b19a
1 changed files with 35 additions and 14 deletions

View File

@ -56,6 +56,30 @@ export default class Keystone {
return pointer; 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. * Retrieve all the API versions available.
* *
@ -186,13 +210,15 @@ export default class Keystone {
* @returns {Promise.<T>} A promise which will resolve if the token has been successfully revoked. * @returns {Promise.<T>} A promise which will resolve if the token has been successfully revoked.
*/ */
tokenRevoke (token, adminToken = null) { tokenRevoke (token, adminToken = null) {
let headers = { return Promise
'X-Subject-Token': token, .all([this.serviceEndpoint(), token, adminToken])
'X-Auth-Token': adminToken || token .then(([url, token, adminToken]) => {
}; return [url, {
'X-Subject-Token': token,
return this.serviceEndpoint() 'X-Auth-Token': adminToken || token
.then((url) => this.http.httpRequest('DELETE', `${url}auth/tokens`, headers)); }];
})
.then(([url, headers]) => this.http.httpRequest('DELETE', `${url}auth/tokens`, headers));
} }
/** /**
@ -202,14 +228,9 @@ export default class Keystone {
* @returns {Promise.<T>} A promise which will resolve with the service catalog. * @returns {Promise.<T>} A promise which will resolve with the service catalog.
*/ */
catalogList (token = null) { catalogList (token = null) {
const headers = {};
if (token) {
headers['X-Auth-Token'] = token;
}
return this return this
.serviceEndpoint() ._requestComponents(token)
.then((url) => this.http.httpRequest('GET', `${url}auth/catalog`, headers)) .then(([url, headers]) => this.http.httpRequest('GET', `${url}auth/catalog`, headers))
.then((response) => response.json()) .then((response) => response.json())
.then((body) => body.catalog); .then((body) => body.catalog);
} }