def __init__(self, *args, **kwargs):
        api_spec_base = kwargs.pop('api_spec_base', None)

        self._swagger_object = {
            'swagger': '2.0',
            'info': {
                'title': '',
                'description': '',
                'termsOfService': '',
                'version': '0.0'
            },
            'host': '',
            'basePath': '',
            'schemes': [],
            'consumes': [],
            'produces': [],
            'paths': {},
            'definitions': {},
            'parameters': {},
            'responses': {},
            'securityDefinitions': {},
            'security': [],
            'tags': [],
            'externalDocs': {}
        }

        if api_spec_base is not None:
            self._swagger_object = copy.deepcopy(api_spec_base)

        add_parameters(self._swagger_object, kwargs)

        api_spec_url = kwargs.pop('api_spec_url', '/api/swagger')
        add_api_spec_resource = kwargs.pop('add_api_spec_resource', True)

        super(Api, self).__init__(*args, **kwargs)

        if self.app and not self._swagger_object['info']['title']:
            self._swagger_object['info']['title'] = self.app.name

        # Unless told otherwise, create and register the swagger endpoint
        if add_api_spec_resource:
            api_spec_urls = [
                '{0}.json'.format(api_spec_url),
                '{0}.html'.format(api_spec_url),
            ]

            self.add_resource(create_swagger_endpoint(self.get_swagger_doc()),
                              *api_spec_urls,
                              endpoint='swagger')
예제 #2
0
    def __init__(self, *args, **kwargs):
        api_spec_base = kwargs.pop('api_spec_base', None)

        if api_spec_base is not None:
            self._swagger_object = copy.deepcopy(api_spec_base)
        else:
            self._swagger_object = {
                'swagger': '2.0',
                'info': {
                    'title': kwargs.pop('title', ''),
                    'description': kwargs.pop('description', ''),
                    'termsOfService': kwargs.pop('terms', ''),
                    'version': kwargs.pop('api_version', '0.0')
                },
                'host': kwargs.pop('host', ''),
                'basePath': kwargs.pop('base_path', ''),
                'schemes': kwargs.pop('schemes', []),
                'consumes': kwargs.pop('consumes', []),
                'produces': kwargs.pop('produces', []),
                'paths': {},
                'definitions': {},
                'parameters': {},
                'responses': {},
                'securityDefinitions': {},
                'security': [],
                'tags': kwargs.pop('tags', []),
                'externalDocs': {}
            }

        contact = kwargs.pop('contact', {})
        if contact:
            self._swagger_object['info']['contact'] = contact
        license = kwargs.pop('license', {})
        if license:
            self._swagger_object['info']['license'] = license
        api_spec_url = kwargs.pop('api_spec_url', '/api/swagger')
        add_api_spec_resource = kwargs.pop('add_api_spec_resource', True)
        super(Api, self).__init__(*args, **kwargs)
        if self.app and not self._swagger_object['info']['title']:
            self._swagger_object['info']['title'] = self.app.name

        # Unless told otherwise, create and register the swagger endpoint
        if add_api_spec_resource:
            api_spec_urls = [
                '{0}.json'.format(api_spec_url),
                '{0}.html'.format(api_spec_url),
            ]
            self.add_resource(create_swagger_endpoint(self), *api_spec_urls, endpoint='swagger')
def get_swagger_blueprint(docs, api_spec_url='/api/swagger', **kwargs):
    """
    Returns a Flask blueprint to serve the given list of swagger document objects.
    :param docs: A list of of swagger document objects
    :param api_spec_url: The URL path that serves the swagger specification document
    :return: A Flask blueprint
    """
    swagger_object = {}
    paths = {}
    definitions = {}

    for doc in docs:
        # Paths and definitions are appended, but overwrite other fields
        if 'paths' in doc:
            paths.update(doc['paths'])

        if 'definitions' in doc:
            definitions.update(doc['definitions'])

        swagger_object.update(doc)

    swagger_object['paths'] = paths
    swagger_object['definitions'] = definitions

    if kwargs:
        add_parameters(swagger_object, kwargs)

    blueprint = Blueprint('swagger', __name__)

    api = restful_Api(blueprint)

    api_spec_urls = [
        '{0}.json'.format(api_spec_url),
        '{0}.html'.format(api_spec_url),
    ]

    api.add_resource(create_swagger_endpoint(swagger_object),
                     *api_spec_urls,
                     endpoint='swagger')

    return blueprint
예제 #4
0
 def __init__(self, *args, **kwargs):
     self._swagger_object = {
         'swagger': '2.0',
         'info': {
             'title': kwargs.pop('title', ''),
             'description': kwargs.pop('description', ''),
             'termsOfService': kwargs.pop('terms', ''),
             'version': kwargs.pop('api_version', '0.0')
         },
         'host': kwargs.pop('host', ''),
         'basePath': kwargs.pop('base_path', ''),
         'schemes': kwargs.pop('schemes', []),
         'consumes': kwargs.pop('consumes', []),
         'produces': kwargs.pop('produces', []),
         'paths': {},
         'definitions': {},
         'parameters': {},
         'responses': {},
         'securityDefinitions': {},
         'security': [],
         'tags': kwargs.pop('tags', []),
         'externalDocs': {}
     }
     contact = kwargs.pop('contact', {})
     if contact:
         self._swagger_object['info']['contact'] = contact
     license = kwargs.pop('license', {})
     if license:
         self._swagger_object['info']['license'] = license
     api_spec_url = kwargs.pop('api_spec_url', '/api/swagger')
     super(Api, self).__init__(*args, **kwargs)
     if self.app and not self._swagger_object['info']['title']:
         self._swagger_object['info']['title'] = self.app.name
     api_spec_urls = [
         '{0}.json'.format(api_spec_url),
         '{0}.html'.format(api_spec_url),
     ]
     self.add_resource(create_swagger_endpoint(self),
                       *api_spec_urls,
                       endpoint='swagger')