import base64
import bz2


def encode(payload):
    """Encode a string for tranmission over the network using base64 encoding of the bz2 compressed string.

    We bz2 compress because we can and also to counteract the inefficiency of the base64 encoding.

    Args:
        payload(str): string we want to transmit over the wire

    Returns:
        str: base64 encoded, bz2 compressed string

    """
    return base64.urlsafe_b64encode(bz2.compress(payload.encode('utf-8'))).decode('utf-8')