<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def wrap_session(func=None, *, storage=MemoryStorage):
 """ A middleware that adds the session management to the request. This middleware optionally accepts a `storage` keyword only parameter for provide own session storage implementation. If it is not provided, the in memory session storage will be used. :param storage: A storage factory/constructor. :type storage: callable or class """


    if func is None:
        return functools.partial(wrap_session, storage=storage)

    # Initialize the storage
    storage = storage()

    def wrapper(request, *args, **kwargs):
        session_key = storage.get_session_key(request)
        request.session = storage.retrieve(request, session_key)
        response = func(request, *args, **kwargs)

        storage.store(request, response, session_key, request.session)
        storage.persist_session_key(request, response, session_key)
        return response

    return wrapper