commit
6e6b3c675f
23 changed files with 257 additions and 174 deletions
|
@ -101,27 +101,14 @@ angular.module('quay').factory('ApiService', ['Restangular', '$q', 'UtilService'
|
|||
|
||||
var getMatchingUserOperationName = function(orgOperationName, method, userRelatedResource) {
|
||||
if (userRelatedResource) {
|
||||
var operations = userRelatedResource['operations'];
|
||||
for (var i = 0; i < operations.length; ++i) {
|
||||
var operation = operations[i];
|
||||
if (operation['method'].toLowerCase() == method) {
|
||||
return operation['nickname'];
|
||||
}
|
||||
if (userRelatedResource[method.toLowerCase()]) {
|
||||
return userRelatedResource[method.toLowerCase()]['operationId'];
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Could not find user operation matching org operation: ' + orgOperationName);
|
||||
};
|
||||
|
||||
var buildMethodsForEndpointResource = function(endpointResource, resourceMap) {
|
||||
var name = endpointResource['name'];
|
||||
var operations = endpointResource['operations'];
|
||||
for (var i = 0; i < operations.length; ++i) {
|
||||
var operation = operations[i];
|
||||
buildMethodsForOperation(operation, endpointResource, resourceMap);
|
||||
}
|
||||
};
|
||||
|
||||
var freshLoginInProgress = [];
|
||||
var reject = function(msg) {
|
||||
for (var i = 0; i < freshLoginInProgress.length; ++i) {
|
||||
|
@ -224,14 +211,13 @@ angular.module('quay').factory('ApiService', ['Restangular', '$q', 'UtilService'
|
|||
};
|
||||
};
|
||||
|
||||
var buildMethodsForOperation = function(operation, resource, resourceMap) {
|
||||
var method = operation['method'].toLowerCase();
|
||||
var operationName = operation['nickname'];
|
||||
var path = resource['path'];
|
||||
var buildMethodsForOperation = function(operation, method, path, resourceMap) {
|
||||
var operationName = operation['operationId'];
|
||||
var urlPath = path['path'];
|
||||
|
||||
// Add the operation itself.
|
||||
apiService[operationName] = function(opt_options, opt_parameters, opt_background, opt_forcessl) {
|
||||
var one = Restangular.one(buildUrl(path, opt_parameters, opt_forcessl));
|
||||
var one = Restangular.one(buildUrl(urlPath, opt_parameters, opt_forcessl));
|
||||
if (opt_background) {
|
||||
one.withHttpConfig({
|
||||
'ignoreLoadingBar': true
|
||||
|
@ -251,15 +237,15 @@ angular.module('quay').factory('ApiService', ['Restangular', '$q', 'UtilService'
|
|||
// If the method for the operation is a GET, add an operationAsResource method.
|
||||
if (method == 'get') {
|
||||
apiService[operationName + 'AsResource'] = function(opt_parameters, opt_background) {
|
||||
return getResource(buildUrl(path, opt_parameters), opt_background);
|
||||
return getResource(buildUrl(urlPath, opt_parameters), opt_background);
|
||||
};
|
||||
}
|
||||
|
||||
// If the resource has a user-related resource, then make a generic operation for this operation
|
||||
// If the operation has a user-related operation, then make a generic operation for this operation
|
||||
// that can call both the user and the organization versions of the operation, depending on the
|
||||
// parameters given.
|
||||
if (resource['quayUserRelated']) {
|
||||
var userOperationName = getMatchingUserOperationName(operationName, method, resourceMap[resource['quayUserRelated']]);
|
||||
if (path['quay_user_related']) {
|
||||
var userOperationName = getMatchingUserOperationName(operationName, method, resourceMap[path['quay_user_related']]);
|
||||
var genericOperationName = getGenericOperationName(userOperationName);
|
||||
apiService[genericOperationName] = function(orgname, opt_options, opt_parameters, opt_background) {
|
||||
if (orgname) {
|
||||
|
@ -280,19 +266,34 @@ angular.module('quay').factory('ApiService', ['Restangular', '$q', 'UtilService'
|
|||
return apiService;
|
||||
}
|
||||
|
||||
var allowedMethods = ['get', 'post', 'put', 'delete'];
|
||||
var resourceMap = {};
|
||||
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]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Build the map of resource names to their objects.
|
||||
for (var i = 0; i < window.__endpoints.length; ++i) {
|
||||
var endpointResource = window.__endpoints[i];
|
||||
resourceMap[endpointResource['name']] = endpointResource;
|
||||
}
|
||||
forEachOperation(function(operation, method, path) {
|
||||
resourceMap[path.name] = path;
|
||||
});
|
||||
|
||||
// Construct the methods for each API endpoint.
|
||||
for (var i = 0; i < window.__endpoints.length; ++i) {
|
||||
var endpointResource = window.__endpoints[i];
|
||||
buildMethodsForEndpointResource(endpointResource, resourceMap);
|
||||
}
|
||||
forEachOperation(function(operation, method, path) {
|
||||
buildMethodsForOperation(operation, method, path, resourceMap);
|
||||
});
|
||||
|
||||
apiService.getErrorMessage = function(resp, defaultMessage) {
|
||||
var message = defaultMessage;
|
||||
|
|
Reference in a new issue