Merge branch 'swaggerlikeus' of https://bitbucket.org/yackob03/quay into swaggerlikeus

This commit is contained in:
Joseph Schorr 2014-03-14 18:57:35 -04:00
commit 767ab1085a
17 changed files with 1357 additions and 28 deletions

View file

@ -3,7 +3,8 @@ import logging
from flask.ext.restful import reqparse
from endpoints.api import ApiResource, resource, method_metadata, nickname, truthy_bool
from endpoints.api import (ApiResource, resource, method_metadata, nickname, truthy_bool,
parse_args, query_param)
from app import app
from auth import scopes
@ -23,7 +24,12 @@ TYPE_CONVERTER = {
}
def swagger_route_data():
def fully_qualified_name(method_view_class):
inst = method_view_class()
return '%s.%s' % (inst.__module__, inst.__class__.__name__)
def swagger_route_data(include_internal):
apis = []
models = {}
for rule in app.url_map.iter_rules():
@ -47,7 +53,9 @@ def swagger_route_data():
'required': True,
})
if method is not None:
if method is None:
logger.debug('Unable to find method for %s in class %s', method_name, view_class)
else:
req_schema_name = method_metadata(method, 'request_schema')
if req_schema_name:
parameters.append({
@ -87,17 +95,34 @@ def swagger_route_data():
scope = method_metadata(method, 'oauth2_scope')
if scope:
new_operation['authorizations'] = {
'oauth2': [scope]
'oauth2': [scope],
}
operations.append(new_operation)
internal = method_metadata(method, 'internal')
if internal is not None:
new_operation['internal'] = True
if not internal or (internal and include_internal):
operations.append(new_operation)
swagger_path = PARAM_REGEX.sub(r'{\2}', rule.rule)
apis.append({
new_resource = {
'path': swagger_path,
'description': view_class.__doc__ if view_class.__doc__ else "",
'operations': operations,
})
'name': fully_qualified_name(view_class),
}
related_user_res = method_metadata(view_class, 'related_user_resource')
if related_user_res is not None:
new_resource['quayUserRelated'] = fully_qualified_name(related_user_res)
internal = method_metadata(view_class, 'internal')
if internal is not None:
new_resource['internal'] = True
if not internal or (internal and include_internal):
apis.append(new_resource)
swagger_data = {
'apiVersion': 'v1',
@ -133,7 +158,9 @@ def swagger_route_data():
@resource('/v1/discovery')
class DiscoveryResource(ApiResource):
"""Ability to inspect the API for usage information and documentation."""
@parse_args
@query_param('internal', 'Whether to include internal APIs.', type=truthy_bool, default=False)
@nickname('discovery')
def get(self):
def get(self, args):
""" List all of the API endpoints available in the swagger API format."""
return swagger_route_data()
return swagger_route_data(args['internal'])