def METHOD_NAME(self, secret: str, algorithm: str) -> str:
    """Encode the token instance into a string.
    Args:
        secret: The secret with which the JWT is encoded.
        algorithm: The algorithm used to encode the JWT.
    Returns:
        An encoded token string.
    Raises:
        ImproperlyConfiguredException: If encoding fails.
    """
    try:
        return jwt.METHOD_NAME(
            claims={k: v for k, v in asdict(self).items() if v is not None}, key=secret, algorithm=algorithm
        )
    except (JWTError, JWSError) as e:
        raise ImproperlyConfiguredException("Failed to encode token") from e