- Make the discovery information be preloaded via a bootstrap.js file before angular runs

- Have ApiService generate all the api methods specified by the API discovery information
- Change all call sites (except for a select few when it does not make sense) to use ApiService
This commit is contained in:
Joseph Schorr 2013-12-26 17:45:16 -05:00
parent 1904e6d0c8
commit 56bb46ffb2
6 changed files with 423 additions and 212 deletions

View file

@ -1,6 +1,18 @@
var TEAM_PATTERN = '^[a-zA-Z][a-zA-Z0-9]+$';
var ROBOT_PATTERN = '^[a-zA-Z][a-zA-Z0-9]+$';
function getRestUrl(args) {
var url = '';
for (var i = 0; i < arguments.length; ++i) {
if (i > 0) {
url += '/';
}
url += encodeURI(arguments[i])
}
return url;
}
function getFirstTextLine(commentString) {
if (!commentString) { return ''; }
@ -34,11 +46,8 @@ function getFirstTextLine(commentString) {
return '';
}
function createRobotAccount(Restangular, is_org, orgname, name, callback) {
var url = is_org ? getRestUrl('organization', orgname, 'robots', name) :
getRestUrl('user/robots', name);
var createRobot = Restangular.one(url);
createRobot.customPUT().then(callback, function(resp) {
function createRobotAccount(ApiService, is_org, orgname, name, callback) {
ApiService.createRobot(is_org ? orgname : null, null, {'robot_shortname': name}).then(callback, function(resp) {
bootbox.dialog({
"message": resp.data ? resp.data : 'The robot account could not be created',
"title": "Cannot create robot account",
@ -52,14 +61,18 @@ function createRobotAccount(Restangular, is_org, orgname, name, callback) {
});
}
function createOrganizationTeam(Restangular, orgname, teamname, callback) {
var createTeam = Restangular.one(getRestUrl('organization', orgname, 'team', teamname));
function createOrganizationTeam(ApiService, orgname, teamname, callback) {
var data = {
'name': teamname,
'role': 'member'
};
var params = {
'orgname': orgname,
'teamname': teamname
};
createTeam.customPOST(data).then(callback, function() {
ApiService.updateOrganizationTeam(data, params).then(callback, function() {
bootbox.dialog({
"message": resp.data ? resp.data : 'The team could not be created',
"title": "Cannot create team",
@ -73,17 +86,6 @@ function createOrganizationTeam(Restangular, orgname, teamname, callback) {
});
}
function getRestUrl(args) {
var url = '';
for (var i = 0; i < arguments.length; ++i) {
if (i > 0) {
url += '/';
}
url += encodeURI(arguments[i])
}
return url;
}
function getMarkedDown(string) {
return Markdown.getSanitizingConverter().makeHtml(string || '');
}
@ -91,6 +93,137 @@ function getMarkedDown(string) {
// Start the application code itself.
quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'restangular', 'angularMoment', 'angulartics', /*'angulartics.google.analytics',*/ 'angulartics.mixpanel', '$strap.directives', 'ngCookies'], function($provide, cfpLoadingBarProvider) {
cfpLoadingBarProvider.includeSpinner = false;
$provide.factory('ApiService', ['Restangular', function(Restangular) {
var apiService = {};
var getResource = function(path) {
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
};
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 formatMethodName = function(endpointName) {
var formatted = '';
for (var i = 0; i < endpointName.length; ++i) {
var c = endpointName[i];
if (c == '_') {
c = endpointName[i + 1].toUpperCase();
i++;
}
formatted += c;
}
return formatted;
};
var buildUrl = function(path, parameters) {
// We already have /api/ on the URLs, so remove them from the paths.
path = path.substr('/api/'.length, path.length);
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);
var colon = varName.indexOf(':');
var isPathVar = false;
if (colon > 0) {
isPathVar = true;
varName = varName.substr(colon + 1);
}
if (!parameters[varName]) {
throw new Error('Missing parameter: ' + varName);
}
url += isPathVar ? parameters[varName] : encodeURI(parameters[varName]);
i = end;
continue;
}
url += c;
}
return url;
};
var getGenericMethodName = function(userMethodName) {
return formatMethodName(userMethodName.replace('_user', ''));
};
var buildMethodsForEndpoint = function(endpoint) {
var method = endpoint.methods[0].toLowerCase();
var methodName = formatMethodName(endpoint['name']);
apiService[methodName] = function(opt_options, opt_parameters) {
return Restangular.one(buildUrl(endpoint['path'], opt_parameters))['custom' + method.toUpperCase()](opt_options);
};
if (method == 'get') {
apiService[methodName + 'AsResource'] = function(opt_parameters) {
return getResource(buildUrl(endpoint['path'], opt_parameters));
};
}
if (endpoint['user_method']) {
apiService[getGenericMethodName(endpoint['user_method'])] = function(orgname, opt_options, opt_parameters) {
if (orgname) {
if (orgname.name) {
orgname = orgname.name;
}
var params = jQuery.extend({'orgname' : orgname}, opt_parameters || {});
return apiService[methodName](opt_options, params);
} else {
return apiService[formatMethodName(endpoint['user_method'])](opt_options, opt_parameters);
}
};
}
};
// Construct the methods for each API endpoint.
if (!window.__endpoints) {
return apiService;
}
for (var i = 0; i < window.__endpoints.length; ++i) {
var endpoint = window.__endpoints[i];
buildMethodsForEndpoint(endpoint);
}
return apiService;
}]);
$provide.factory('CookieService', ['$cookies', '$cookieStore', function($cookies, $cookieStore) {
var cookieService = {};
@ -113,7 +246,7 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
return cookieService;
}]);
$provide.factory('UserService', ['Restangular', 'CookieService', function(Restangular, CookieService) {
$provide.factory('UserService', ['ApiService', 'CookieService', function(ApiService, CookieService) {
var userResponse = {
verified: false,
anonymous: true,
@ -139,8 +272,7 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
};
userService.load = function(opt_callback) {
var userFetch = Restangular.one('user/');
userFetch.get().then(function(loadedUser) {
ApiService.getLoggedInUser().then(function(loadedUser) {
userResponse = loadedUser;
if (!userResponse.anonymous) {
@ -198,48 +330,6 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
return userService;
}]);
$provide.factory('ApiService', ['Restangular', function(Restangular) {
var apiService = {}
apiService.at = function(locationPieces) {
var location = getRestUrl.apply(this, arguments);
var info = {
'url': location,
'caller': Restangular.one(location),
'withOptions': function(options) {
info.options = options;
return info;
},
'get': function(processor, opt_errorHandler) {
var options = info.options;
var caller = info.caller;
var result = {
'loading': true,
'value': null,
'hasError': false
};
caller.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 info;
};
return apiService;
}]);
$provide.factory('KeyService', ['$location', function($location) {
var keyService = {}
@ -254,8 +344,8 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
return keyService;
}]);
$provide.factory('PlanService', ['Restangular', 'KeyService', 'UserService', 'CookieService',
function(Restangular, KeyService, UserService, CookieService) {
$provide.factory('PlanService', ['KeyService', 'UserService', 'CookieService', 'ApiService',
function(KeyService, UserService, CookieService, ApiService) {
var plans = null;
var planDict = {};
var planService = {};
@ -353,8 +443,7 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
return;
}
var getPlans = Restangular.one('plans');
getPlans.get().then(function(data) {
ApiService.listPlans().then(function(data) {
var i = 0;
for(i = 0; i < data.plans.length; i++) {
planDict[data.plans[i].stripeId] = data.plans[i];
@ -412,13 +501,7 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
};
planService.getSubscription = function(orgname, success, failure) {
var url = planService.getSubscriptionUrl(orgname);
var getSubscription = Restangular.one(url);
getSubscription.get().then(success, failure);
};
planService.getSubscriptionUrl = function(orgname) {
return orgname ? getRestUrl('organization', orgname, 'plan') : 'user/plan';
ApiService.getSubscription(orgname).then(success, failure);
};
planService.setSubscription = function(orgname, planId, success, failure, opt_token) {
@ -430,9 +513,7 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
subscriptionDetails['token'] = opt_token.id;
}
var url = planService.getSubscriptionUrl(orgname);
var createSubscriptionRequest = Restangular.one(url);
createSubscriptionRequest.customPUT(subscriptionDetails).then(function(resp) {
ApiService.updateSubscription(orgname, subscriptionDetails).then(function(resp) {
success(resp);
planService.getPlan(planId, function(plan) {
for (var i = 0; i < listeners.length; ++i) {
@ -443,9 +524,7 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
};
planService.getCardInfo = function(orgname, callback) {
var url = orgname ? getRestUrl('organization', orgname, 'card') : 'user/card';
var getCard = Restangular.one(url);
getCard.customGET().then(function(resp) {
ApiService.getCard(orgname).then(function(resp) {
callback(resp.card);
}, function() {
callback({'is_valid': false});
@ -492,12 +571,10 @@ quayApp = angular.module('quay', ['ngRoute', 'chieffancypants.loadingBar', 'rest
'token': token.id
};
var url = orgname ? getRestUrl('organization', orgname, 'card') : 'user/card';
var changeCardRequest = Restangular.one(url);
changeCardRequest.customPOST(cardInfo).then(callbacks['success'], function(resp) {
planService.handleCardError(resp);
callbacks['failure'](resp);
});
ApiService.setCard(orgname, cardInfo).then(callbacks['success'], function(resp) {
planService.handleCardError(resp);
callbacks['failure'](resp);
});
});
};
@ -739,10 +816,9 @@ quayApp.directive('userSetup', function () {
'signInStarted': '&signInStarted',
'signedIn': '&signedIn'
},
controller: function($scope, $location, $timeout, Restangular, KeyService, UserService) {
controller: function($scope, $location, $timeout, ApiService, KeyService, UserService) {
$scope.sendRecovery = function() {
var signinPost = Restangular.one('recovery');
signinPost.customPOST($scope.recovery).then(function() {
ApiService.requestRecoveryEmail($scope.recovery).then(function() {
$scope.invalidEmail = false;
$scope.sent = true;
}, function(result) {
@ -772,7 +848,7 @@ quayApp.directive('signinForm', function () {
'signInStarted': '&signInStarted',
'signedIn': '&signedIn'
},
controller: function($scope, $location, $timeout, Restangular, KeyService, UserService) {
controller: function($scope, $location, $timeout, ApiService, KeyService, UserService) {
$scope.showGithub = function() {
$scope.markStarted();
@ -799,8 +875,7 @@ quayApp.directive('signinForm', function () {
$scope.signin = function() {
$scope.markStarted();
var signinPost = Restangular.one('signin');
signinPost.customPOST($scope.user).then(function() {
ApiService.signinUser($scope.user).then(function() {
$scope.needsEmailVerification = false;
$scope.invalidCredentials = false;
@ -840,7 +915,7 @@ quayApp.directive('signupForm', function () {
scope: {
},
controller: function($scope, $location, $timeout, Restangular, KeyService, UserService) {
controller: function($scope, $location, $timeout, ApiService, KeyService, UserService) {
$('.form-signup').popover();
angulartics.waitForVendorApi(mixpanel, 500, function(loadedMixpanel) {
@ -857,8 +932,7 @@ quayApp.directive('signupForm', function () {
$('.form-signup').popover('hide');
$scope.registering = true;
var newUserPost = Restangular.one('user/');
newUserPost.customPOST($scope.newUser).then(function() {
ApiService.createNewUser($scope.newUser).then(function() {
$scope.awaitingConfirmation = true;
$scope.registering = false;
@ -911,7 +985,7 @@ quayApp.directive('dockerAuthDialog', function () {
'shown': '=shown',
'counter': '=counter'
},
controller: function($scope, $element, Restangular) {
controller: function($scope, $element) {
$scope.isDownloadSupported = function() {
try { return !!new Blob(); } catch(e){}
return false;
@ -981,7 +1055,7 @@ quayApp.directive('billingInvoices', function () {
'user': '=user',
'visible': '=visible'
},
controller: function($scope, $element, $sce, Restangular) {
controller: function($scope, $element, $sce, ApiService) {
$scope.loading = false;
$scope.invoiceExpanded = {};
@ -1000,13 +1074,7 @@ quayApp.directive('billingInvoices', function () {
$scope.loading = true;
var url = getRestUrl('user/invoices');
if ($scope.organization) {
url = getRestUrl('organization', $scope.organization.name, 'invoices');
}
var getInvoices = Restangular.one(url);
getInvoices.get().then(function(resp) {
ApiService.listInvoices($scope.organization).then(function(resp) {
$scope.invoices = resp.invoices;
$scope.loading = false;
});
@ -1036,7 +1104,7 @@ quayApp.directive('logsView', function () {
'repository': '=repository',
'performer': '=performer'
},
controller: function($scope, $element, $sce, Restangular) {
controller: function($scope, $element, $sce, Restangular, ApiService) {
$scope.loading = true;
$scope.logs = null;
$scope.kindsAllowed = null;
@ -1152,6 +1220,8 @@ quayApp.directive('logsView', function () {
$scope.loading = true;
// Note: We construct the URLs here manually because we also use it for the download
// path.
var url = getRestUrl('user/logs');
if ($scope.organization) {
url = getRestUrl('organization', $scope.organization.name, 'logs');
@ -1255,7 +1325,7 @@ quayApp.directive('robotsManager', function () {
'organization': '=organization',
'user': '=user'
},
controller: function($scope, $element, Restangular) {
controller: function($scope, $element, ApiService) {
$scope.ROBOT_PATTERN = ROBOT_PATTERN;
$scope.robots = null;
$scope.loading = false;
@ -1280,7 +1350,7 @@ quayApp.directive('robotsManager', function () {
$scope.createRobot = function(name) {
if (!name) { return; }
createRobotAccount(Restangular, !!$scope.organization, $scope.organization ? $scope.organization.name : '', name,
createRobotAccount(ApiService, !!$scope.organization, $scope.organization ? $scope.organization.name : '', name,
function(created) {
$scope.robots.push(created);
});
@ -1288,11 +1358,7 @@ quayApp.directive('robotsManager', function () {
$scope.deleteRobot = function(info) {
var shortName = $scope.getShortenedName(info.name);
var url = $scope.organization ? getRestUrl('organization', $scope.organization.name, 'robots', shortName) :
getRestUrl('user/robots', shortName);
var deleteRobot = Restangular.one(url);
deleteRobot.customDELETE().then(function(resp) {
ApiService.deleteRobot($scope.organization, null, {'robot_shortname': shortName}).then(function(resp) {
for (var i = 0; i < $scope.robots.length; ++i) {
if ($scope.robots[i].name == info.name) {
$scope.robots.splice(i, 1);
@ -1318,9 +1384,7 @@ quayApp.directive('robotsManager', function () {
if ($scope.loading) { return; }
$scope.loading = true;
var url = $scope.organization ? getRestUrl('organization', $scope.organization.name, 'robots') : 'user/robots';
var getRobots = Restangular.one(url);
getRobots.customGET($scope.obj).then(function(resp) {
ApiService.getRobots($scope.organization).then(function(resp) {
$scope.robots = resp.robots;
$scope.loading = false;
});
@ -1553,7 +1617,7 @@ quayApp.directive('headerBar', function () {
restrict: 'C',
scope: {
},
controller: function($scope, $element, $location, UserService, PlanService, Restangular) {
controller: function($scope, $element, $location, UserService, PlanService, ApiService) {
$scope.overPlan = false;
var checkOverPlan = function() {
@ -1562,8 +1626,7 @@ quayApp.directive('headerBar', function () {
return;
}
var checkPrivate = Restangular.one('user/private');
checkPrivate.customGET().then(function(resp) {
ApiService.getUserPrivateCount().then(function(resp) {
$scope.overPlan = resp.privateCount > resp.reposAllowed;
});
};
@ -1575,10 +1638,9 @@ quayApp.directive('headerBar', function () {
PlanService.registerListener(this, checkOverPlan);
$scope.signout = function() {
var signoutPost = Restangular.one('signout');
signoutPost.customPOST().then(function() {
UserService.load();
$location.path('/');
ApiService.logout().then(function() {
UserService.load();
$location.path('/');
});
};
@ -1609,7 +1671,7 @@ quayApp.directive('entitySearch', function () {
'includeTeams': '=includeTeams',
'isOrganization': '=isOrganization'
},
controller: function($scope, $element, Restangular, UserService) {
controller: function($scope, $element, Restangular, UserService, ApiService) {
$scope.lazyLoading = true;
$scope.isAdmin = false;
@ -1619,16 +1681,12 @@ quayApp.directive('entitySearch', function () {
$scope.isAdmin = UserService.isNamespaceAdmin($scope.namespace);
if ($scope.isOrganization && $scope.includeTeams) {
var url = getRestUrl('organization', $scope.namespace);
var getOrganization = Restangular.one(url);
getOrganization.customGET().then(function(resp) {
ApiService.getOrganization(null, {'orgname': $scope.namespace}).then(function(resp) {
$scope.teams = resp.teams;
});
}
var url = $scope.isOrganization ? getRestUrl('organization', $scope.namespace, 'robots') : 'user/robots';
var getRobots = Restangular.one(url);
getRobots.customGET().then(function(resp) {
ApiService.getRobots($scope.isOrganization ? $scope.namespace : null).then(function(resp) {
$scope.robots = resp.robots;
$scope.lazyLoading = false;
}, function() {
@ -1648,7 +1706,7 @@ quayApp.directive('entitySearch', function () {
return;
}
createOrganizationTeam(Restangular, $scope.namespace, teamname, function(created) {
createOrganizationTeam(ApiService, $scope.namespace, teamname, function(created) {
$scope.setEntity(created.name, 'team', false);
$scope.teams[teamname] = created;
});
@ -1667,7 +1725,7 @@ quayApp.directive('entitySearch', function () {
return;
}
createRobotAccount(Restangular, $scope.isOrganization, $scope.namespace, robotname, function(created) {
createRobotAccount(ApiService, $scope.isOrganization, $scope.namespace, robotname, function(created) {
$scope.setEntity(created.name, 'user', true);
$scope.robots.push(created);
});
@ -1793,7 +1851,7 @@ quayApp.directive('billingOptions', function () {
'user': '=user',
'organization': '=organization'
},
controller: function($scope, $element, PlanService, Restangular) {
controller: function($scope, $element, PlanService, ApiService) {
$scope.invoice_email = false;
$scope.currentCard = null;
@ -1863,9 +1921,7 @@ quayApp.directive('billingOptions', function () {
var save = function() {
$scope.working = true;
var url = $scope.organization ? getRestUrl('organization', $scope.organization.name) : 'user/';
var conductSave = Restangular.one(url);
conductSave.customPUT($scope.obj).then(function(resp) {
ApiService.changeDetails($scope.organization, $scope.obj).then(function(resp) {
$scope.working = false;
});
};
@ -1900,7 +1956,7 @@ quayApp.directive('planManager', function () {
'readyForPlan': '&readyForPlan',
'planChanged': '&planChanged'
},
controller: function($scope, $element, PlanService, Restangular) {
controller: function($scope, $element, PlanService, ApiService) {
var hasSubscription = false;
$scope.isPlanVisible = function(plan, subscribedPlan) {