import base64


def encode(s, urlsafe: bool = False):
    """
    Base64 encode.

    :param s:
    :param urlsafe: Whether use URL Safe Mode
    :return: str
    """
    if isinstance(s, str):
        s = s.encode('utf-8')

    if urlsafe:
        return base64.urlsafe_b64encode(s).decode('utf-8').rstrip('=')

    return base64.b64encode(s).decode('utf-8')