Repository: spotify/luigi
File: luigi/notifications.py (Python)

Code before:
    client = boto3_client('ses')

    msg_root = generate_email(sender, subject, message, recipients, image_png)
    response = client.send_raw_email(Source=sender,
                                     Destinations=recipients,
                                     RawMessage={'Data': msg_root.as_string()})

    logger.debug(("Message sent to SES.\nMessageId: {},\nRequestId: {},\n"
                 "HTTPSStatusCode: {}").format(response['MessageId'],
                                               response['ResponseMetadata']['RequestId'],
                                               response['ResponseMetadata']['HTTPStatusCode']))


def send_email_sendgrid(sender, subject, message, recipients, image_png):
    import sendgrid as sendgrid_lib
    client = sendgrid_lib.SendGridAPIClient(sendgrid().apikey)
    if email().format == 'html':
        to_send = sendgrid_lib.Mail(from_email=sender,
            to_emails=recipients,
            subject=subject,
            html_content=message)
    else:
        to_send = sendgrid_lib.Mail(from_email=sender,
            to_emails=recipients,
            subject=subject,
            plain_text_content=message)
    if image_png:
        to_send.add_attachment(image_png)

    response = client.send(to_send)


def _email_disabled_reason():
    if email().format == 'none':
        return "email format is 'none'"
    elif email().force_send:
        return None
    elif sys.stdout.isatty():
        return "running from a tty"
    else:
        return None


def send_email_sns(sender, subject, message, topic_ARN, image_png):
    """
    Sends notification through AWS SNS. Takes Topic ARN from recipients.

    Does not handle access keys.  Use either
      1/ configuration file
      2/ EC2 instance profile


Review comment (refactor):
The `Mail()` generation could be more DRY (similar to how it was before):
```python
sendgrid_mail = sendgrid_lib.Mail()

sendgrid_mail.from_email = sender
sendgrid_mail.to = receipients
sendgrid_mail.subject = subject

if email().format == 'html':
    sendgrid_mail.add_content(message, 'text/html')
else:
    sendgrid_mail.add_content(message, 'text/plain')

if image_png:
    sendgrid_mail.add_attachment(image_png)
```

Code after:
    client = boto3_client('ses')

    msg_root = generate_email(sender, subject, message, recipients, image_png)
    response = client.send_raw_email(Source=sender,
                                     Destinations=recipients,
                                     RawMessage={'Data': msg_root.as_string()})

    logger.debug(("Message sent to SES.\nMessageId: {},\nRequestId: {},\n"
                 "HTTPSStatusCode: {}").format(response['MessageId'],
                                               response['ResponseMetadata']['RequestId'],
                                               response['ResponseMetadata']['HTTPStatusCode']))


def send_email_sendgrid(sender, subject, message, recipients, image_png):
    import sendgrid as sendgrid_lib
    client = sendgrid_lib.SendGridAPIClient(sendgrid().apikey)

    to_send = sendgrid_lib.Mail(
            from_email=sender,
            to_emails=recipients,
            subject=subject)

    if email().format == 'html':
        to_send.add_content(message, 'text/html')
    else:
        to_send.add_content(message, 'text/plain')

    if image_png:
        to_send.add_attachment(image_png)

    client.send(to_send)


def _email_disabled_reason():
    if email().format == 'none':
        return "email format is 'none'"
    elif email().force_send:
        return None
    elif sys.stdout.isatty():
        return "running from a tty"
    else:
        return None


def send_email_sns(sender, subject, message, topic_ARN, image_png):
    """
    Sends notification through AWS SNS. Takes Topic ARN from recipients.

    Does not handle access keys.  Use either
      1/ configuration file
      2/ EC2 instance profile