def gen_arrow_multipart_buffers()

in http/get_multipart/python/server/server.py [0:0]


def gen_arrow_multipart_buffers(boundary, schema, source, is_last_part=False):
    """
    Generate buffers for the Arrow Stream part of a multipart response.

    That is, an HTTP response started with the header:

        Content-type: multipart/mixed; boundary=the_boundary_string

    The buffers, when taken together, will form the following structure:

        --the_boundary_string<CR><LF>
        Content-Type: application/vnd.apache.arrow.stream<CR><LF>
        <CR><LF>
        <Arrow Stream data>
        <CR><LF>

    If is_last_part is True, the boundary string will be appended with two
    hyphens at the end of the last buffer to indicate the end of the multipart
    response:

        --the_boundary_string--<CR><LF>
    """
    with io.BytesIO() as sink, pa.ipc.new_stream(sink, schema) as writer:
        sink.write(
            f"--{boundary}\r\n"
            "Content-Type: application/vnd.apache.arrow.stream\r\n"
            "\r\n".encode("utf-8")
        )
        for batch in source:
            writer.write_batch(batch)
            sink.truncate()
            with sink.getbuffer() as buffer:
                yield buffer
            sink.seek(0)

        writer.close()
        sink.write("\r\n".encode("utf-8"))
        if is_last_part:
            sink.write(f"--{boundary}--\r\n".encode("utf-8"))
        sink.truncate()
        with sink.getbuffer() as buffer:
            yield buffer