First attempt at using flask-restful and swagger api documentation.

This commit is contained in:
jakedt 2014-03-10 18:30:41 -04:00
parent 52d2229482
commit de1a44f853
5 changed files with 283 additions and 4 deletions

View file

@ -0,0 +1,66 @@
import re
from flask.ext.restful import Api, Resource
from endpoints.api import api, method_metadata, nickname
from endpoints.common import get_route_data
from app import app
discovery_api = Api(api)
param_regex = re.compile(r'<([\w]+:)?([\w]+)>')
def swagger_route_data():
apis = []
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.',
'required': True
})
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/',
'apis': apis,
}
return swagger_data
class DiscoveryResource(Resource):
@nickname('discovery')
def get(self):
return swagger_route_data()
discovery_api.add_resource(DiscoveryResource, '/v1/discovery')