def load_record_from_shards_async(shards, content_id, attr, callback):
    '''Load bytes from stream for app and content_id.

    :attr: must be one of 'data' or 'metadata'.

    Once loading is complete, callback will be invoked with a GAsyncResult,
    use EosCompanionAppService.finish_load_all_in_stream_to_bytes
    to get the result or handle the corresponding error.

    Returns LOAD_FROM_ENGINE_SUCCESS if a stream could be loaded,
    LOAD_FROM_ENGINE_NO_SUCH_CONTENT if the content wasn't found.
    '''
    def _callback(_, result):
        '''Marshal the GAsyncReady callback into an (error, data) callback.'''
        try:
            bytes_data = EosCompanionAppService.finish_load_all_in_stream_to_bytes(
                result)
        except GLib.Error as error:
            callback(error, None)
            return

        callback(None, bytes_data)

    status, blob = load_record_blob_from_shards(shards, content_id, attr)

    if status == LOAD_FROM_ENGINE_NO_SUCH_CONTENT:
        GLib.idle_add(lambda: callback(
            GLib.Error(
                'EKN ID {} not found in shards'.format(content_id),
                GLib.quark_to_string(EosCompanionAppService.error_quark()),
                EosCompanionAppService.Error.INVALID_CONTENT_ID), None))
        return

    EosCompanionAppService.load_all_in_stream_to_bytes(
        blob.get_stream(),
        chunk_size=BYTE_CHUNK_SIZE,
        cancellable=None,
        callback=_callback)
def conditionally_wrap_stream(stream, content_size, content_type, version,
                              query, adjuster, cache, cancellable, callback):
    '''Inspect content_type to adjust stream content.'''
    def _content_adjusted_callback(error, adjusted):
        '''Callback once we have finished adjusting the content.'''
        if error is not None:
            callback(error, None)
            return

        memory_stream = Gio.MemoryInputStream.new_from_bytes(adjusted)
        callback(None, (memory_stream, adjusted.get_size()))

    def _read_stream_callback(_, result):
        '''Callback once we have finished loading the stream to bytes.'''
        try:
            content_bytes = EosCompanionAppService.finish_load_all_in_stream_to_bytes(
                result)
        except GLib.Error as error:
            callback(error, None)
            return

        adjuster.render_async(content_type, content_bytes, version, query,
                              cache, cancellable, _content_adjusted_callback)

    if adjuster.needs_adjustment(content_type):
        EosCompanionAppService.load_all_in_stream_to_bytes(
            stream,
            chunk_size=BYTE_CHUNK_SIZE,
            cancellable=cancellable,
            callback=_read_stream_callback)
        return

    # We call callback here on idle so as to ensure that both invocations
    # are asynchronous (mixing asynchronous with synchronous disguised as
    # asynchronous is a bad idea)
    GLib.idle_add(lambda: callback(None, (stream, content_size)))