From e5f4cf57566a0be61367414169c01c408554865d Mon Sep 17 00:00:00 2001 From: suprithIUB Date: Sat, 28 Oct 2017 03:13:03 -0700 Subject: [PATCH] modularize lengthy method --- examples/helpers/mail/mail_example.py | 208 +++++++++++++++++--------- 1 file changed, 137 insertions(+), 71 deletions(-) diff --git a/examples/helpers/mail/mail_example.py b/examples/helpers/mail/mail_example.py index 9d5133d5e..4b1af0c88 100644 --- a/examples/helpers/mail/mail_example.py +++ b/examples/helpers/mail/mail_example.py @@ -4,7 +4,9 @@ from sendgrid.helpers.mail import * from sendgrid import * -# NOTE: you will need move this file to the root directory of this project to execute properly. +# NOTE: you will need move this file to the root +# directory of this project to execute properly. + def build_hello_email(): """Minimum required to send an email""" @@ -17,66 +19,139 @@ def build_hello_email(): return mail.get() -def build_kitchen_sink(): - """All settings set""" - mail = Mail() - mail.from_email = Email("test@example.com", "Example User") +def build_personalization(personalization): + """Build personalization mock instance from a mock dict""" + mock_personalization = Personalization() - mail.subject = "Hello World from the SendGrid Python Library" + for to_addr in personalization['to_list']: + personalization.add_to(to_addr) - personalization = Personalization() - personalization.add_to(Email("test1@example.com", "Example User")) - personalization.add_to(Email("test2@example.com", "Example User")) - personalization.add_cc(Email("test3@example.com", "Example User")) - personalization.add_cc(Email("test4@example.com", "Example User")) - personalization.add_bcc(Email("test5@example.com")) - personalization.add_bcc(Email("test6@example.com")) - personalization.subject = "Hello World from the Personalized SendGrid Python Library" - personalization.add_header(Header("X-Test", "test")) - personalization.add_header(Header("X-Mock", "true")) - personalization.add_substitution(Substitution("%name%", "Example User")) - personalization.add_substitution(Substitution("%city%", "Denver")) - personalization.add_custom_arg(CustomArg("user_id", "343")) - personalization.add_custom_arg(CustomArg("type", "marketing")) - personalization.send_at = 1443636843 - mail.add_personalization(personalization) - - personalization2 = Personalization() - personalization2.add_to(Email("test1@example.com", "Example User")) - personalization2.add_to(Email("test2@example.com", "Example User")) - personalization2.add_cc(Email("test3@example.com", "Example User")) - personalization2.add_cc(Email("test4@example.com", "Eric Shallock")) - personalization2.add_bcc(Email("test5@example.com")) - personalization2.add_bcc(Email("test6@example.com")) - personalization2.subject = "Hello World from the Personalized SendGrid Python Library" - personalization2.add_header(Header("X-Test", "test")) - personalization2.add_header(Header("X-Mock", "true")) - personalization2.add_substitution(Substitution("%name%", "Example User")) - personalization2.add_substitution(Substitution("%city%", "Denver")) - personalization2.add_custom_arg(CustomArg("user_id", "343")) - personalization2.add_custom_arg(CustomArg("type", "marketing")) - personalization2.send_at = 1443636843 - mail.add_personalization(personalization2) + for cc_addr in personalization['cc_list']: + personalization.add_to(cc_addr) - mail.add_content(Content("text/plain", "some text here")) - mail.add_content(Content("text/html", "some text here")) + for bcc_addr in personalization['bcc_list']: + personalization.add_bc(bcc_addr) + + personalization.subject = personalization['subject'] + + for header in personalization['headers']: + personalization.add_header(header) + + for substitution in personalization['substitutions']: + personalization.add_substitution(substitution) + + for arg in personalization['custom_args']: + personalization.add_custom_arg(arg) + + personalization.send_at = personalization['send_at'] + return mock_personalization + + +def get_mock_personalization_dict(): + """Get a dict of personalization mock.""" + mock_personalization = dict() + + mock_personalization['to_list'] = [Email("test1@example.com", "Example User"), + Email("test2@example.com", "Example User")] + + mock_personalization['cc_list'] = [Email("test3@example.com", "Example User"), + Email("test4@example.com", "Example User")] + mock_personalization['bcc_list'] = [Email("test5@example.com"), + Email("test6@example.com")] + + mock_personalization['subject'] = ("Hello World from the Personalized " + "SendGrid Python Library") + + mock_personalization['headers'] = [Header("X-Test", "test"), + Header("X-Mock", "true")] + + mock_personalization['substitutions'] = [Substitution("%name%", "Example User"), + Substitution("%city%", "Denver")] + + mock_personalization['custom_args'] = [CustomArg("user_id", "343"), + CustomArg("type", "marketing")] + + mock_personalization['send_at'] = 1443636843 + return mock_personalization + + +def build_attachment1(): + """Build attachment mock.""" attachment = Attachment() - attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12" + attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQs" + "IGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") attachment.type = "application/pdf" attachment.filename = "balance_001.pdf" attachment.disposition = "attachment" attachment.content_id = "Balance Sheet" - mail.add_attachment(attachment) + return attachment + + +def build_attachment2(): + """Build attachment mock.""" + attachment = Attachment() + attachment.content = "BwdW" + attachment.type = "image/png" + attachment.filename = "banner.png" + attachment.disposition = "inline" + attachment.content_id = "Banner" + return attachment + + +def build_mail_settings(): + """Build mail settings mock.""" + mail_settings = MailSettings() + mail_settings.bcc_settings = BCCSettings(True, Email("test@example.com")) + mail_settings.bypass_list_management = BypassListManagement(True) + mail_settings.footer_settings = FooterSettings(True, "Footer Text", + "Footer Text") + mail_settings.sandbox_mode = SandBoxMode(True) + mail_settings.spam_check = SpamCheck(True, 1, "https://spamcatcher.sendgrid.com") + return mail_settings + + +def build_tracking_settings(): + """Build tracking settings mock.""" + tracking_settings = TrackingSettings() + tracking_settings.click_tracking = ClickTracking(True, True) + tracking_settings.open_tracking = OpenTracking(True, + ("Optional tag to replace with the" + "open image in the body of the message")) + + tracking_settings.subscription_tracking = SubscriptionTracking(True, + ("text to insert into the " + "text/plain portion of the" + " message"), + ("html to insert " + "into the text/html portion of " + "the message"), + ("Optional tag to replace with " + "the open image in the body of " + "the message")) + + tracking_settings.ganalytics = Ganalytics(True, "some source", "some medium", + "some term", "some_content", "some_campaign") + return tracking_settings + + +def build_kitchen_sink(): + """All settings set""" + mail = Mail() + + mail.from_email = Email("test@example.com", "Example User") + mail.subject = "Hello World from the SendGrid Python Library" + + personalization = get_mock_personalization_dict() + mail.add_personalization(build_personalization(personalization)) + mail.add_personalization(build_personalization(personalization)) + + mail.add_content(Content("text/plain", "some text here")) + mail.add_content(Content("text/html", "some text here")) - attachment2 = Attachment() - attachment2.content = "BwdW" - attachment2.type = "image/png" - attachment2.filename = "banner.png" - attachment2.disposition = "inline" - attachment2.content_id = "Banner" - mail.add_attachment(attachment2) + mail.add_attachment(build_attachment1()) + mail.add_attachment(build_attachment2()) mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932" @@ -94,32 +169,18 @@ def build_kitchen_sink(): mail.send_at = 1443636842 - # This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work + # This must be a valid [batch ID] + # (https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work # mail.set_batch_id("N2VkYjBjYWItMGU4OC0xMWU2LWJhMzYtZjQ1Yzg5OTBkNzkxLWM5ZTUyZjNhOA") - mail.asm = ASM(99, [4, 5, 6, 7, 8]) - mail.ip_pool_name = "24" - - mail_settings = MailSettings() - mail_settings.bcc_settings = BCCSettings(True, Email("test@example.com")) - mail_settings.bypass_list_management = BypassListManagement(True) - mail_settings.footer_settings = FooterSettings(True, "Footer Text", "Footer Text") - mail_settings.sandbox_mode = SandBoxMode(True) - mail_settings.spam_check = SpamCheck(True, 1, "https://spamcatcher.sendgrid.com") - mail.mail_settings = mail_settings - - tracking_settings = TrackingSettings() - tracking_settings.click_tracking = ClickTracking(True, True) - tracking_settings.open_tracking = OpenTracking(True, "Optional tag to replace with the open image in the body of the message") - tracking_settings.subscription_tracking = SubscriptionTracking(True, "text to insert into the text/plain portion of the message", "html to insert into the text/html portion of the message", "Optional tag to replace with the open image in the body of the message") - tracking_settings.ganalytics = Ganalytics(True, "some source", "some medium", "some term", "some_content", "some_campaign") - mail.tracking_settings = tracking_settings - + mail.mail_settings = build_mail_settings() + mail.tracking_settings = build_tracking_settings() mail.reply_to = Email("test@example.com") return mail.get() + def send_hello_email(): # Assumes you set your environment variable: # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key @@ -130,6 +191,7 @@ def send_hello_email(): print(response.headers) print(response.body) + def send_kitchen_sink(): # Assumes you set your environment variable: # https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key @@ -140,5 +202,9 @@ def send_kitchen_sink(): print(response.headers) print(response.body) -send_hello_email() # this will actually send an email -send_kitchen_sink() # this will only send an email if you set SandBox Mode to False + +# this will actually send an email +send_hello_email() + +# this will only send an email if you set SandBox Mode to False +send_kitchen_sink()