def __ConfigureMultipartRequest(self, http_request):
        """Configure http_request as a multipart request for this upload."""
        # This is a multipart/related upload.
        msg_root = mime_multipart.MIMEMultipart('related')
        # msg_root should not write out its own headers
        setattr(msg_root, '_write_headers', lambda self: None)

        # attach the body as one part
        msg = mime_nonmultipart.MIMENonMultipart(
            *http_request.headers['content-type'].split('/'))
        msg.set_payload(http_request.body)
        msg_root.attach(msg)

        # attach the media as the second part
        msg = mime_nonmultipart.MIMENonMultipart(*self.mime_type.split('/'))
        msg['Content-Transfer-Encoding'] = 'binary'
        msg.set_payload(self.stream.read())
        msg_root.attach(msg)

        # NOTE: We encode the body, but can't use
        #       `email.message.Message.as_string` because it prepends
        #       `> ` to `From ` lines.
        fp = six.BytesIO()
        if six.PY3:
            generator_class = MultipartBytesGenerator
        else:
            generator_class = email_generator.Generator
        g = generator_class(fp, mangle_from_=False)
        g.flatten(msg_root, unixfrom=False)
        http_request.body = fp.getvalue()

        multipart_boundary = msg_root.get_boundary()
        http_request.headers['content-type'] = (
            'multipart/related; boundary=%r' % multipart_boundary)
        if isinstance(multipart_boundary, six.text_type):
            multipart_boundary = multipart_boundary.encode('ascii')

        body_components = http_request.body.split(multipart_boundary)
        headers, _, _ = body_components[-2].partition(b'\n\n')
        body_components[-2] = b'\n\n'.join([headers, b'<media body>\n\n--'])
        http_request.loggable_body = multipart_boundary.join(body_components)
def __ConfigureMultipartRequest ( self , http_request ) : msg_root = mime_multipart . MIMEMultipart ( 'related' ) setattr ( msg_root , '_write_headers' , lambda self : None ) msg = mime_nonmultipart . MIMENonMultipart ( * http_request . headers [ 'content-type' ] . split ( '/' ) ) msg . set_payload ( http_request . body ) msg_root . attach ( msg ) msg = mime_nonmultipart . MIMENonMultipart ( * self . mime_type . split ( '/' ) ) msg [ 'Content-Transfer-Encoding' ] = 'binary' msg . set_payload ( self . stream . read ( ) ) msg_root . attach ( msg ) fp = six . BytesIO ( ) if six . PY3 : generator_class = MultipartBytesGenerator else : generator_class = email_generator . Generator g = generator_class ( fp , mangle_from_ = False ) g . flatten ( msg_root , unixfrom = False ) http_request . body = fp . getvalue ( ) multipart_boundary = msg_root . get_boundary ( ) http_request . headers [ 'content-type' ] = ( 'multipart/related; boundary=%r' % multipart_boundary ) if isinstance ( multipart_boundary , six . text_type ) : multipart_boundary = multipart_boundary . encode ( 'ascii' ) body_components = http_request . body . split ( multipart_boundary ) headers , _ , _ = body_components [ - 2 ] . partition ( b'\n\n' ) body_components [ - 2 ] = b'\n\n' . join ( [ headers , b'<media body>\n\n--' ] ) http_request . loggable_body = multipart_boundary . join ( body_components )
Configure http_request as a multipart request for this upload.