2014-03-10 22:30:41 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from flask.ext.restful import Api, Resource
|
2014-03-11 03:54:55 +00:00
|
|
|
from flask.ext.restful.utils.cors import crossdomain
|
2014-03-10 22:30:41 +00:00
|
|
|
|
|
|
|
from endpoints.api import api, method_metadata, nickname
|
|
|
|
from endpoints.common import get_route_data
|
|
|
|
from app import app
|
|
|
|
|
|
|
|
|
|
|
|
discovery_api = Api(api)
|
2014-03-11 03:54:55 +00:00
|
|
|
discovery_api.decorators = [crossdomain(origin='*')]
|
2014-03-10 22:30:41 +00:00
|
|
|
|
|
|
|
param_regex = re.compile(r'<([\w]+:)?([\w]+)>')
|
|
|
|
|
|
|
|
def swagger_route_data():
|
|
|
|
apis = []
|
2014-03-11 03:54:55 +00:00
|
|
|
models = {}
|
2014-03-10 22:30:41 +00:00
|
|
|
for rule in app.url_map.iter_rules():
|
|
|
|
endpoint_method = app.view_functions[rule.endpoint]
|
|
|
|
|
|
|
|
if 'view_class' in dir(endpoint_method):
|
|
|
|
operations = []
|
|
|
|
|
|
|
|
method_names = list(rule.methods.difference(['HEAD', 'OPTIONS']))
|
|
|
|
for method_name in method_names:
|
|
|
|
method = getattr(endpoint_method.view_class, method_name.lower(), None)
|
|
|
|
|
|
|
|
parameters = []
|
|
|
|
for param in rule.arguments:
|
|
|
|
parameters.append({
|
|
|
|
'paramType': 'path',
|
|
|
|
'name': param,
|
|
|
|
'dataType': 'string',
|
|
|
|
'description': 'Param description.',
|
2014-03-11 03:54:55 +00:00
|
|
|
'required': True,
|
2014-03-10 22:30:41 +00:00
|
|
|
})
|
|
|
|
|
2014-03-11 03:54:55 +00:00
|
|
|
req_schema_name = method_metadata(method, 'request_schema')
|
|
|
|
if req_schema_name:
|
|
|
|
parameters.append({
|
|
|
|
'paramType': 'body',
|
|
|
|
'name': 'request_body',
|
|
|
|
'description': 'Request body contents.',
|
|
|
|
'dataType': req_schema_name,
|
|
|
|
'required': True,
|
|
|
|
})
|
|
|
|
|
|
|
|
schema = endpoint_method.view_class.schemas[req_schema_name]
|
|
|
|
models[req_schema_name] = schema
|
|
|
|
|
2014-03-10 22:30:41 +00:00
|
|
|
if method is not None:
|
|
|
|
operations.append({
|
|
|
|
'method': method_name,
|
|
|
|
'nickname': method_metadata(method, 'nickname'),
|
|
|
|
'type': 'void',
|
|
|
|
'parameters': parameters,
|
|
|
|
})
|
|
|
|
|
|
|
|
swagger_path = param_regex.sub(r'{\2}', rule.rule)
|
|
|
|
apis.append({
|
|
|
|
'path': swagger_path,
|
|
|
|
'description': 'Resource description.',
|
|
|
|
'operations': operations,
|
|
|
|
})
|
|
|
|
|
|
|
|
swagger_data = {
|
|
|
|
'apiVersion': 'v1',
|
|
|
|
'swaggerVersion': '1.2',
|
|
|
|
'basePath': 'https://quay.io/',
|
2014-03-11 03:54:55 +00:00
|
|
|
'resourcePath': '/',
|
2014-03-10 22:30:41 +00:00
|
|
|
'apis': apis,
|
2014-03-11 03:54:55 +00:00
|
|
|
'models': models,
|
2014-03-10 22:30:41 +00:00
|
|
|
}
|
|
|
|
return swagger_data
|
|
|
|
|
|
|
|
|
|
|
|
class DiscoveryResource(Resource):
|
|
|
|
@nickname('discovery')
|
|
|
|
def get(self):
|
|
|
|
return swagger_route_data()
|
|
|
|
|
|
|
|
|
|
|
|
discovery_api.add_resource(DiscoveryResource, '/v1/discovery')
|