Fix the API service to use the new Swagger description form
This commit is contained in:
parent
0bc1c29dff
commit
3fb2a33ee7
5 changed files with 42 additions and 38 deletions
|
@ -94,9 +94,15 @@ def swagger_route_data(include_internal=False, compact=False):
|
||||||
swagger_path = PARAM_REGEX.sub(r'{\2}', rule.rule)
|
swagger_path = PARAM_REGEX.sub(r'{\2}', rule.rule)
|
||||||
path_swagger = {
|
path_swagger = {
|
||||||
'name': fully_qualified_name(view_class),
|
'name': fully_qualified_name(view_class),
|
||||||
|
'path': swagger_path,
|
||||||
'tag': tag_name
|
'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
|
paths[swagger_path] = path_swagger
|
||||||
|
|
||||||
# Add any global path parameters.
|
# 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:
|
if requires_fresh_login is not None:
|
||||||
operation_swagger['requires_fresh_login'] = True
|
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.
|
# Add the path parameters.
|
||||||
if rule.arguments:
|
if rule.arguments:
|
||||||
for path_parameter in rule.arguments:
|
for path_parameter in rule.arguments:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
""" Acces usage logs for organizations or repositories. """
|
""" Access usage logs for organizations or repositories. """
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,7 @@ def robots_list(prefix, include_permissions=False):
|
||||||
return {'robots': robots.values()}
|
return {'robots': robots.values()}
|
||||||
|
|
||||||
@resource('/v1/user/robots')
|
@resource('/v1/user/robots')
|
||||||
|
@internal_only
|
||||||
class UserRobotList(ApiResource):
|
class UserRobotList(ApiResource):
|
||||||
""" Resource for listing user robots. """
|
""" Resource for listing user robots. """
|
||||||
@require_user_admin
|
@require_user_admin
|
||||||
|
|
|
@ -101,27 +101,14 @@ angular.module('quay').factory('ApiService', ['Restangular', '$q', 'UtilService'
|
||||||
|
|
||||||
var getMatchingUserOperationName = function(orgOperationName, method, userRelatedResource) {
|
var getMatchingUserOperationName = function(orgOperationName, method, userRelatedResource) {
|
||||||
if (userRelatedResource) {
|
if (userRelatedResource) {
|
||||||
var operations = userRelatedResource['operations'];
|
if (userRelatedResource[method.toLowerCase()]) {
|
||||||
for (var i = 0; i < operations.length; ++i) {
|
return userRelatedResource[method.toLowerCase()]['operationId'];
|
||||||
var operation = operations[i];
|
|
||||||
if (operation['method'].toLowerCase() == method) {
|
|
||||||
return operation['nickname'];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('Could not find user operation matching org operation: ' + orgOperationName);
|
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 freshLoginInProgress = [];
|
||||||
var reject = function(msg) {
|
var reject = function(msg) {
|
||||||
for (var i = 0; i < freshLoginInProgress.length; ++i) {
|
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 buildMethodsForOperation = function(operation, method, path, resourceMap) {
|
||||||
var method = operation['method'].toLowerCase();
|
var operationName = operation['operationId'];
|
||||||
var operationName = operation['nickname'];
|
var urlPath = path['path'];
|
||||||
var path = resource['path'];
|
|
||||||
|
|
||||||
// Add the operation itself.
|
// Add the operation itself.
|
||||||
apiService[operationName] = function(opt_options, opt_parameters, opt_background, opt_forcessl) {
|
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) {
|
if (opt_background) {
|
||||||
one.withHttpConfig({
|
one.withHttpConfig({
|
||||||
'ignoreLoadingBar': true
|
'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 the method for the operation is a GET, add an operationAsResource method.
|
||||||
if (method == 'get') {
|
if (method == 'get') {
|
||||||
apiService[operationName + 'AsResource'] = function(opt_parameters, opt_background) {
|
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
|
// that can call both the user and the organization versions of the operation, depending on the
|
||||||
// parameters given.
|
// parameters given.
|
||||||
if (resource['quayUserRelated']) {
|
if (path['quay_user_related']) {
|
||||||
var userOperationName = getMatchingUserOperationName(operationName, method, resourceMap[resource['quayUserRelated']]);
|
var userOperationName = getMatchingUserOperationName(operationName, method, resourceMap[path['quay_user_related']]);
|
||||||
var genericOperationName = getGenericOperationName(userOperationName);
|
var genericOperationName = getGenericOperationName(userOperationName);
|
||||||
apiService[genericOperationName] = function(orgname, opt_options, opt_parameters, opt_background) {
|
apiService[genericOperationName] = function(orgname, opt_options, opt_parameters, opt_background) {
|
||||||
if (orgname) {
|
if (orgname) {
|
||||||
|
@ -280,19 +266,34 @@ angular.module('quay').factory('ApiService', ['Restangular', '$q', 'UtilService'
|
||||||
return apiService;
|
return apiService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var allowedMethods = ['get', 'post', 'put', 'delete'];
|
||||||
var resourceMap = {};
|
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.
|
// Build the map of resource names to their objects.
|
||||||
for (var i = 0; i < window.__endpoints.length; ++i) {
|
forEachOperation(function(operation, method, path) {
|
||||||
var endpointResource = window.__endpoints[i];
|
resourceMap[path.name] = path;
|
||||||
resourceMap[endpointResource['name']] = endpointResource;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// Construct the methods for each API endpoint.
|
// Construct the methods for each API endpoint.
|
||||||
for (var i = 0; i < window.__endpoints.length; ++i) {
|
forEachOperation(function(operation, method, path) {
|
||||||
var endpointResource = window.__endpoints[i];
|
buildMethodsForOperation(operation, method, path, resourceMap);
|
||||||
buildMethodsForEndpointResource(endpointResource, resourceMap);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
apiService.getErrorMessage = function(resp, defaultMessage) {
|
apiService.getErrorMessage = function(resp, defaultMessage) {
|
||||||
var message = defaultMessage;
|
var message = defaultMessage;
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window.__endpoints = {{ route_data|safe }}.apis;
|
window.__endpoints = {{ route_data|safe }}.paths;
|
||||||
window.__features = {{ feature_set|safe }};
|
window.__features = {{ feature_set|safe }};
|
||||||
window.__config = {{ config_set|safe }};
|
window.__config = {{ config_set|safe }};
|
||||||
window.__oauth = {{ oauth_set|safe }};
|
window.__oauth = {{ oauth_set|safe }};
|
||||||
|
|
Reference in a new issue