Beispiel #1
0
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 (Default value = "/api/swagger")
    :param **kwargs:
    :returns: 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 "components" in doc:
            definitions.update(doc["components"])

        swagger_object.update(doc)

    swagger_object["paths"] = paths
    swagger_object["components"] = definitions

    if kwargs:
        add_parameters(swagger_object, kwargs)

    blueprint = Blueprint("swagger", __name__)

    api = restful_Api(blueprint)

    api_spec_urls = [
        f"{api_spec_url}.json",
        f"{api_spec_url}.html",
    ]

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

    return blueprint
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