def apply_middlewares(app, middlewares=None):
    """Apply WSGI middlewares to a Flasks application

    Example:

    .. doctest::
        >>> from flask import Flask
        >>> import base64

        >>> app = Flask(__name__)

        >>> class AuthMiddleware:
        ...     def __init__(self, wsgi):
        ...         self.wsgi = wsgi
        ...
        ...     @staticmethod
        ...     def is_authenticated(header):
        ...         if not header:
        ...             return False
        ...         _, encoded = header.split(None, 1)
        ...         decoded = base64.b64decode(encoded).decode('UTF-8')
        ...         username, password = decoded.split(':', 1)
        ...         return username == password
        ...
        ...     def __call__(self, environ, start_response):
        ...         if self.is_authenticated(environ.get('HTTP_AUTHORIZATION')):
        ...             return self.wsgi(environ, start_response)
        ...         start_response('401 Authentication Required',
        ...             [('Content-Type', 'text/html'),
        ...              ('WWW-Authenticate', 'Basic realm="Login"')])
        ...         return [b'Login']

        >>> middlewares = [AuthMiddleware]

        >>> apply_middlewares(app, middlewares)

    :param app: Flask application
    :type app: :class:`flask.Flask`
    :param middlewares: WSGI middleware to apply on the application.
        Expects a list of elements which are either

        - A class taking a wsgi as an argument
        - A function that takes a :class:`flask.Flask` as argument and even eventually apply a middleware on it
    :type middlewares: list
    """
    middlewares = middlewares or []
    for middleware in middlewares:
        if isinstance(middleware, type):
            app.wsgi_app = middleware(app.wsgi_app)
        else:
            middleware(app)
