2015-02-19 21:21:54 +00:00
|
|
|
/**
|
|
|
|
* Service which exposes the server-defined API as a nice set of helper methods and automatic
|
|
|
|
* callbacks. Any method defined on the server is exposed here as an equivalent method. Also
|
|
|
|
* defines some helper functions for working with API responses.
|
|
|
|
*/
|
2015-05-11 17:47:40 +00:00
|
|
|
angular.module('quay').factory('ApiService', ['Restangular', '$q', 'UtilService', function(Restangular, $q, UtilService) {
|
2015-02-19 21:21:54 +00:00
|
|
|
var apiService = {};
|
|
|
|
|
|
|
|
var getResource = function(path, opt_background) {
|
|
|
|
var resource = {};
|
|
|
|
resource.url = path;
|
|
|
|
resource.withOptions = function(options) {
|
|
|
|
this.options = options;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
resource.get = function(processor, opt_errorHandler) {
|
|
|
|
var options = this.options;
|
|
|
|
var performer = Restangular.one(this.url);
|
|
|
|
|
|
|
|
var result = {
|
|
|
|
'loading': true,
|
|
|
|
'value': null,
|
|
|
|
'hasError': false
|
|
|
|
};
|
|
|
|
|
|
|
|
if (opt_background) {
|
|
|
|
performer.withHttpConfig({
|
|
|
|
'ignoreLoadingBar': true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
performer.get(options).then(function(resp) {
|
|
|
|
result.value = processor(resp);
|
|
|
|
result.loading = false;
|
|
|
|
}, function(resp) {
|
|
|
|
result.hasError = true;
|
|
|
|
result.loading = false;
|
|
|
|
if (opt_errorHandler) {
|
|
|
|
opt_errorHandler(resp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
};
|
|
|
|
|
|
|
|
var buildUrl = function(path, parameters, opt_forcessl) {
|
|
|
|
// We already have /api/v1/ on the URLs, so remove them from the paths.
|
|
|
|
path = path.substr('/api/v1/'.length, path.length);
|
|
|
|
|
|
|
|
// Build the path, adjusted with the inline parameters.
|
|
|
|
var used = {};
|
|
|
|
var url = '';
|
|
|
|
for (var i = 0; i < path.length; ++i) {
|
|
|
|
var c = path[i];
|
|
|
|
if (c == '{') {
|
|
|
|
var end = path.indexOf('}', i);
|
|
|
|
var varName = path.substr(i + 1, end - i - 1);
|
|
|
|
|
|
|
|
if (!parameters[varName]) {
|
|
|
|
throw new Error('Missing parameter: ' + varName);
|
|
|
|
}
|
|
|
|
|
|
|
|
used[varName] = true;
|
|
|
|
url += parameters[varName];
|
|
|
|
i = end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
url += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append any query parameters.
|
|
|
|
var isFirst = true;
|
|
|
|
for (var paramName in parameters) {
|
|
|
|
if (!parameters.hasOwnProperty(paramName)) { continue; }
|
|
|
|
if (used[paramName]) { continue; }
|
|
|
|
|
|
|
|
var value = parameters[paramName];
|
|
|
|
if (value) {
|
|
|
|
url += isFirst ? '?' : '&';
|
|
|
|
url += paramName + '=' + encodeURIComponent(value)
|
|
|
|
isFirst = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are forcing SSL, return an absolutel URL with an SSL prefix.
|
|
|
|
if (opt_forcessl) {
|
|
|
|
path = 'https://' + window.location.host + '/api/v1/' + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getGenericOperationName = function(userOperationName) {
|
|
|
|
return userOperationName.replace('User', '');
|
|
|
|
};
|
|
|
|
|
|
|
|
var getMatchingUserOperationName = function(orgOperationName, method, userRelatedResource) {
|
|
|
|
if (userRelatedResource) {
|
2015-05-14 21:15:39 +00:00
|
|
|
if (userRelatedResource[method.toLowerCase()]) {
|
|
|
|
return userRelatedResource[method.toLowerCase()]['operationId'];
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Could not find user operation matching org operation: ' + orgOperationName);
|
|
|
|
};
|
|
|
|
|
|
|
|
var freshLoginInProgress = [];
|
|
|
|
var reject = function(msg) {
|
|
|
|
for (var i = 0; i < freshLoginInProgress.length; ++i) {
|
|
|
|
freshLoginInProgress[i].deferred.reject({'data': {'message': msg}});
|
|
|
|
}
|
|
|
|
freshLoginInProgress = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
var retry = function() {
|
|
|
|
for (var i = 0; i < freshLoginInProgress.length; ++i) {
|
|
|
|
freshLoginInProgress[i].retry();
|
|
|
|
}
|
|
|
|
freshLoginInProgress = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
var freshLoginFailCheck = function(opName, opArgs) {
|
|
|
|
return function(resp) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
// If the error is a fresh login required, show the dialog.
|
|
|
|
if (resp.status == 401 && resp.data['error_type'] == 'fresh_login_required') {
|
|
|
|
var retryOperation = function() {
|
|
|
|
apiService[opName].apply(apiService, opArgs).then(function(resp) {
|
|
|
|
deferred.resolve(resp);
|
|
|
|
}, function(resp) {
|
|
|
|
deferred.reject(resp);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var verifyNow = function() {
|
|
|
|
var info = {
|
|
|
|
'password': $('#freshPassword').val()
|
|
|
|
};
|
|
|
|
|
|
|
|
$('#freshPassword').val('');
|
|
|
|
|
|
|
|
// Conduct the sign in of the user.
|
|
|
|
apiService.verifyUser(info).then(function() {
|
|
|
|
// On success, retry the operations. if it succeeds, then resolve the
|
|
|
|
// deferred promise with the result. Otherwise, reject the same.
|
|
|
|
retry();
|
|
|
|
}, function(resp) {
|
|
|
|
// Reject with the sign in error.
|
|
|
|
reject('Invalid verification credentials');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add the retry call to the in progress list. If there is more than a single
|
|
|
|
// in progress call, we skip showing the dialog (since it has already been
|
|
|
|
// shown).
|
|
|
|
freshLoginInProgress.push({
|
|
|
|
'deferred': deferred,
|
|
|
|
'retry': retryOperation
|
|
|
|
})
|
|
|
|
|
|
|
|
if (freshLoginInProgress.length > 1) {
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
var box = bootbox.dialog({
|
|
|
|
"message": 'It has been more than a few minutes since you last logged in, ' +
|
|
|
|
'so please verify your password to perform this sensitive operation:' +
|
|
|
|
'<form style="margin-top: 10px" action="javascript:void(0)">' +
|
|
|
|
'<input id="freshPassword" class="form-control" type="password" placeholder="Current Password">' +
|
|
|
|
'</form>',
|
|
|
|
"title": 'Please Verify',
|
|
|
|
"buttons": {
|
|
|
|
"verify": {
|
|
|
|
"label": "Verify",
|
|
|
|
"className": "btn-success",
|
|
|
|
"callback": verifyNow
|
|
|
|
},
|
|
|
|
"close": {
|
|
|
|
"label": "Cancel",
|
|
|
|
"className": "btn-default",
|
|
|
|
"callback": function() {
|
|
|
|
reject('Verification canceled')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
box.bind('shown.bs.modal', function(){
|
|
|
|
box.find("input").focus();
|
|
|
|
box.find("form").submit(function() {
|
|
|
|
if (!$('#freshPassword').val()) { return; }
|
|
|
|
|
|
|
|
box.modal('hide');
|
|
|
|
verifyNow();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return a new promise. We'll accept or reject it based on the result
|
|
|
|
// of the login.
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we just 'raise' the error via the reject method on the promise.
|
|
|
|
return $q.reject(resp);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-05-14 21:15:39 +00:00
|
|
|
var buildMethodsForOperation = function(operation, method, path, resourceMap) {
|
|
|
|
var operationName = operation['operationId'];
|
2015-07-29 22:25:44 +00:00
|
|
|
var urlPath = path['x-path'];
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
|
|
// Add the operation itself.
|
|
|
|
apiService[operationName] = function(opt_options, opt_parameters, opt_background, opt_forcessl) {
|
2015-05-14 21:15:39 +00:00
|
|
|
var one = Restangular.one(buildUrl(urlPath, opt_parameters, opt_forcessl));
|
2015-02-19 21:21:54 +00:00
|
|
|
if (opt_background) {
|
|
|
|
one.withHttpConfig({
|
|
|
|
'ignoreLoadingBar': true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var opObj = one['custom' + method.toUpperCase()](opt_options);
|
|
|
|
|
|
|
|
// If the operation requires_fresh_login, then add a specialized error handler that
|
|
|
|
// will defer the operation's result if sudo is requested.
|
2015-07-29 22:25:44 +00:00
|
|
|
if (operation['x-requires-fresh-login']) {
|
2015-02-19 21:21:54 +00:00
|
|
|
opObj = opObj.catch(freshLoginFailCheck(operationName, arguments));
|
|
|
|
}
|
|
|
|
return opObj;
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the method for the operation is a GET, add an operationAsResource method.
|
|
|
|
if (method == 'get') {
|
|
|
|
apiService[operationName + 'AsResource'] = function(opt_parameters, opt_background) {
|
2015-05-14 21:15:39 +00:00
|
|
|
return getResource(buildUrl(urlPath, opt_parameters), opt_background);
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:15:39 +00:00
|
|
|
// If the operation has a user-related operation, then make a generic operation for this operation
|
2015-02-19 21:21:54 +00:00
|
|
|
// that can call both the user and the organization versions of the operation, depending on the
|
|
|
|
// parameters given.
|
2015-07-29 22:25:44 +00:00
|
|
|
if (path['x-user-related']) {
|
|
|
|
var userOperationName = getMatchingUserOperationName(operationName, method, resourceMap[path['x-user-related']]);
|
2015-02-19 21:21:54 +00:00
|
|
|
var genericOperationName = getGenericOperationName(userOperationName);
|
|
|
|
apiService[genericOperationName] = function(orgname, opt_options, opt_parameters, opt_background) {
|
|
|
|
if (orgname) {
|
|
|
|
if (orgname.name) {
|
|
|
|
orgname = orgname.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
var params = jQuery.extend({'orgname' : orgname}, opt_parameters || {}, opt_background);
|
|
|
|
return apiService[operationName](opt_options, params);
|
|
|
|
} else {
|
|
|
|
return apiService[userOperationName](opt_options, opt_parameters, opt_background);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!window.__endpoints) {
|
|
|
|
return apiService;
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:15:39 +00:00
|
|
|
var allowedMethods = ['get', 'post', 'put', 'delete'];
|
2015-02-19 21:21:54 +00:00
|
|
|
var resourceMap = {};
|
2015-05-14 21:15:39 +00:00
|
|
|
var forEachOperation = function(callback) {
|
|
|
|
for (var path in window.__endpoints) {
|
|
|
|
if (!window.__endpoints.hasOwnProperty(path)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var method in window.__endpoints[path]) {
|
|
|
|
if (!window.__endpoints[path].hasOwnProperty(method)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allowedMethods.indexOf(method.toLowerCase()) < 0) { continue; }
|
|
|
|
callback(window.__endpoints[path][method], method, window.__endpoints[path]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
|
|
// Build the map of resource names to their objects.
|
2015-05-14 21:15:39 +00:00
|
|
|
forEachOperation(function(operation, method, path) {
|
2015-07-29 22:25:44 +00:00
|
|
|
resourceMap[path['x-name']] = path;
|
2015-05-14 21:15:39 +00:00
|
|
|
});
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
|
|
// Construct the methods for each API endpoint.
|
2015-05-14 21:15:39 +00:00
|
|
|
forEachOperation(function(operation, method, path) {
|
|
|
|
buildMethodsForOperation(operation, method, path, resourceMap);
|
|
|
|
});
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
|
|
apiService.getErrorMessage = function(resp, defaultMessage) {
|
|
|
|
var message = defaultMessage;
|
|
|
|
if (resp['data']) {
|
|
|
|
message = resp['data']['error_message'] || resp['data']['message'] || resp['data']['error_description'] || message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
|
|
|
apiService.errorDisplay = function(defaultMessage, opt_handler) {
|
|
|
|
return function(resp) {
|
|
|
|
var message = apiService.getErrorMessage(resp, defaultMessage);
|
|
|
|
if (opt_handler) {
|
|
|
|
var handlerMessage = opt_handler(resp);
|
|
|
|
if (handlerMessage) {
|
|
|
|
message = handlerMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 17:47:40 +00:00
|
|
|
message = UtilService.stringToHTML(message);
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
bootbox.dialog({
|
|
|
|
"message": message,
|
|
|
|
"title": defaultMessage,
|
|
|
|
"buttons": {
|
|
|
|
"close": {
|
|
|
|
"label": "Close",
|
|
|
|
"className": "btn-primary"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return apiService;
|
|
|
|
}]);
|