Holy black magic batman, move the query parameters to decorators and expose them through discovery.
This commit is contained in:
parent
b3e0dfae48
commit
978d68f0e0
5 changed files with 209 additions and 135 deletions
|
@ -1,17 +1,26 @@
|
|||
import re
|
||||
import logging
|
||||
|
||||
from flask.ext.restful import Api, Resource
|
||||
from flask.ext.restful.utils.cors import crossdomain
|
||||
from flask.ext.restful import Resource, reqparse
|
||||
|
||||
from endpoints.api import api, method_metadata, nickname
|
||||
from endpoints.common import get_route_data
|
||||
from endpoints.api import resource, method_metadata, nickname, truthy_bool
|
||||
from app import app
|
||||
|
||||
|
||||
discovery_api = Api(api)
|
||||
discovery_api.decorators = [crossdomain(origin='*')]
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
PARAM_REGEX = re.compile(r'<([\w]+:)?([\w]+)>')
|
||||
|
||||
|
||||
TYPE_CONVERTER = {
|
||||
truthy_bool: 'boolean',
|
||||
str: 'string',
|
||||
basestring: 'string',
|
||||
reqparse.text_type: 'string',
|
||||
int: 'integer',
|
||||
}
|
||||
|
||||
param_regex = re.compile(r'<([\w]+:)?([\w]+)>')
|
||||
|
||||
def swagger_route_data():
|
||||
apis = []
|
||||
|
@ -49,6 +58,22 @@ def swagger_route_data():
|
|||
schema = endpoint_method.view_class.schemas[req_schema_name]
|
||||
models[req_schema_name] = schema
|
||||
|
||||
logger.debug('method dir: %s', dir(method))
|
||||
if '__api_query_params' in dir(method):
|
||||
for param_spec in method.__api_query_params:
|
||||
new_param = {
|
||||
'paramType': 'query',
|
||||
'name': param_spec['name'],
|
||||
'description': param_spec['help'],
|
||||
'dataType': TYPE_CONVERTER[param_spec['type']],
|
||||
'required': param_spec['required'],
|
||||
}
|
||||
|
||||
if len(param_spec['choices']) > 0:
|
||||
new_param['enum'] = list(param_spec['choices'])
|
||||
|
||||
parameters.append(new_param)
|
||||
|
||||
if method is not None:
|
||||
operations.append({
|
||||
'method': method_name,
|
||||
|
@ -57,7 +82,7 @@ def swagger_route_data():
|
|||
'parameters': parameters,
|
||||
})
|
||||
|
||||
swagger_path = param_regex.sub(r'{\2}', rule.rule)
|
||||
swagger_path = PARAM_REGEX.sub(r'{\2}', rule.rule)
|
||||
apis.append({
|
||||
'path': swagger_path,
|
||||
'description': 'Resource description.',
|
||||
|
@ -67,18 +92,24 @@ def swagger_route_data():
|
|||
swagger_data = {
|
||||
'apiVersion': 'v1',
|
||||
'swaggerVersion': '1.2',
|
||||
'basePath': 'https://quay.io/',
|
||||
'basePath': 'http://ci.devtable.com:5000',
|
||||
'resourcePath': '/',
|
||||
'info': {
|
||||
'title': 'Quay.io API',
|
||||
'description': ('This API allows you to perform many of the operations '
|
||||
'required to work with Quay.io repositories, users, and '
|
||||
'organizations. You can find out more at '
|
||||
'<a href="https://quay.io">Quay.io</a>.'),
|
||||
'termsOfServiceUrl': 'https://quay.io/tos',
|
||||
'contact': 'support@quay.io',
|
||||
},
|
||||
'apis': apis,
|
||||
'models': models,
|
||||
}
|
||||
return swagger_data
|
||||
|
||||
|
||||
@resource('/v1/discovery')
|
||||
class DiscoveryResource(Resource):
|
||||
@nickname('discovery')
|
||||
def get(self):
|
||||
return swagger_route_data()
|
||||
|
||||
|
||||
discovery_api.add_resource(DiscoveryResource, '/v1/discovery')
|
Reference in a new issue