From 3fb2a33ee7484528941a4bcfb6aafd7369574084 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 14 May 2015 17:15:39 -0400 Subject: [PATCH] Fix the API service to use the new Swagger description form --- endpoints/api/discovery.py | 10 +++-- endpoints/api/logs.py | 2 +- endpoints/api/robot.py | 1 + static/js/services/api-service.js | 65 ++++++++++++++++--------------- templates/base.html | 2 +- 5 files changed, 42 insertions(+), 38 deletions(-) diff --git a/endpoints/api/discovery.py b/endpoints/api/discovery.py index f1aefa568..210410996 100644 --- a/endpoints/api/discovery.py +++ b/endpoints/api/discovery.py @@ -94,9 +94,15 @@ def swagger_route_data(include_internal=False, compact=False): swagger_path = PARAM_REGEX.sub(r'{\2}', rule.rule) path_swagger = { 'name': fully_qualified_name(view_class), + 'path': swagger_path, 'tag': tag_name } + if include_internal: + related_user_res = method_metadata(view_class, 'related_user_resource') + if related_user_res is not None: + path_swagger['quay_user_related'] = fully_qualified_name(related_user_res) + paths[swagger_path] = path_swagger # Add any global path parameters. @@ -139,10 +145,6 @@ def swagger_route_data(include_internal=False, compact=False): if requires_fresh_login is not None: operation_swagger['requires_fresh_login'] = True - related_user_res = method_metadata(view_class, 'related_user_resource') - if related_user_res is not None: - operation_swagger['quayUserRelated'] = fully_qualified_name(related_user_res) - # Add the path parameters. if rule.arguments: for path_parameter in rule.arguments: diff --git a/endpoints/api/logs.py b/endpoints/api/logs.py index a69c129d0..55c6110ad 100644 --- a/endpoints/api/logs.py +++ b/endpoints/api/logs.py @@ -1,4 +1,4 @@ -""" Acces usage logs for organizations or repositories. """ +""" Access usage logs for organizations or repositories. """ import json diff --git a/endpoints/api/robot.py b/endpoints/api/robot.py index b1967f59d..b64eb0cca 100644 --- a/endpoints/api/robot.py +++ b/endpoints/api/robot.py @@ -70,6 +70,7 @@ def robots_list(prefix, include_permissions=False): return {'robots': robots.values()} @resource('/v1/user/robots') +@internal_only class UserRobotList(ApiResource): """ Resource for listing user robots. """ @require_user_admin diff --git a/static/js/services/api-service.js b/static/js/services/api-service.js index 5952463fd..10c8b137f 100644 --- a/static/js/services/api-service.js +++ b/static/js/services/api-service.js @@ -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; diff --git a/templates/base.html b/templates/base.html index 91dee7842..77f28c2a0 100644 --- a/templates/base.html +++ b/templates/base.html @@ -41,7 +41,7 @@ {% endblock %}