From b4f86e391ef1e40c269286b6ca0d8645192a5ab6 Mon Sep 17 00:00:00 2001 From: Hang Qian Date: Wed, 9 Aug 2023 21:35:47 -0700 Subject: [PATCH 1/3] feat: gzip mail body when content-encoding is set to gzip (#468) [Mail body compression](https://docs.sendgrid.com/api-reference/mail-send/mail-send#mail-body-compression) is supported on V3 api. This patch added the support that when a client sets the content-encoding header to gzip, send compresses the mail body with gzip. --- base_interface.go | 20 +++++++ sendgrid_test.go | 150 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) diff --git a/base_interface.go b/base_interface.go index 60c04bda..b7472a24 100644 --- a/base_interface.go +++ b/base_interface.go @@ -1,6 +1,8 @@ package sendgrid import ( + "bytes" + "compress/gzip" "context" "errors" "net/http" @@ -61,6 +63,24 @@ func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) { // SendWithContext sends an email through Twilio SendGrid with context.Context. func (cl *Client) SendWithContext(ctx context.Context, email *mail.SGMailV3) (*rest.Response, error) { cl.Body = mail.GetRequestBody(email) + // when Content-Encoding header is set to "gzip" + // mail body is compressed using gzip according to + // https://docs.sendgrid.com/api-reference/mail-send/mail-send#mail-body-compression + if cl.Headers["Content-Encoding"] == "gzip" { + var gzipped bytes.Buffer + gz := gzip.NewWriter(&gzipped) + if _, err := gz.Write(cl.Body); err != nil { + return nil, err + } + if err := gz.Flush(); err != nil { + return nil, err + } + if err := gz.Close(); err != nil { + return nil, err + } + + cl.Body = gzipped.Bytes() + } return MakeRequestWithContext(ctx, cl.Request) } diff --git a/sendgrid_test.go b/sendgrid_test.go index f4557bd6..ada4dfe1 100644 --- a/sendgrid_test.go +++ b/sendgrid_test.go @@ -1602,6 +1602,156 @@ func Test_test_mail_batch__batch_id__get(t *testing.T) { assert.Equal(t, 200, response.StatusCode, "Wrong status code returned") } +func Test_test_send_client_with_mail_body_compression_enabled(t *testing.T) { + apiKey := "SENDGRID_API_KEY" + client := NewSendClient(apiKey) + client.Headers["Content-Encoding"] = "gzip" + + emailBytes := []byte(` { + "asm": { + "group_id": 1, + "groups_to_display": [ + 1, + 2, + 3 + ] + }, + "attachments": [ + { + "content": "[BASE64 encoded content block here]", + "content_id": "ii_139db99fdb5c3704", + "disposition": "inline", + "filename": "file1.jpg", + "name": "file1", + "type": "jpg" + } + ], + "batch_id": "[YOUR BATCH ID GOES HERE]", + "categories": [ + "category1", + "category2" + ], + "content": [ + { + "type": "text/html", + "value": "

Hello, world!

" + } + ], + "custom_args": { + "New Argument 1": "New Value 1", + "activationAttempt": "1", + "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]" + }, + "from": { + "email": "sam.smith@example.com", + "name": "Sam Smith" + }, + "headers": {}, + "ip_pool_name": "[YOUR POOL NAME GOES HERE]", + "mail_settings": { + "bcc": { + "email": "ben.doe@example.com", + "enable": true + }, + "bypass_list_management": { + "enable": true + }, + "footer": { + "enable": true, + "html": "

Thanks
The Twilio SendGrid Team

", + "text": "Thanks,/n The Twilio SendGrid Team" + }, + "sandbox_mode": { + "enable": false + }, + "spam_check": { + "enable": true, + "post_to_url": "http://example.com/compliance", + "threshold": 3 + } + }, + "personalizations": [ + { + "bcc": [ + { + "email": "sam.doe@example.com", + "name": "Sam Doe" + } + ], + "cc": [ + { + "email": "jane.doe@example.com", + "name": "Jane Doe" + } + ], + "custom_args": { + "New Argument 1": "New Value 1", + "activationAttempt": "1", + "customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]" + }, + "headers": { + "X-Accept-Language": "en", + "X-Mailer": "MyApp" + }, + "send_at": 1409348513, + "subject": "Hello, World!", + "substitutions": { + "id": "substitutions", + "type": "object" + }, + "to": [ + { + "email": "john.doe@example.com", + "name": "John Doe" + } + ] + } + ], + "reply_to": { + "email": "sam.smith@example.com", + "name": "Sam Smith" + }, + "send_at": 1409348513, + "subject": "Hello, World!", + "template_id": "[YOUR TEMPLATE ID GOES HERE]", + "tracking_settings": { + "click_tracking": { + "enable": true, + "enable_text": true + }, + "ganalytics": { + "enable": true, + "utm_campaign": "[NAME OF YOUR REFERRER SOURCE]", + "utm_content": "[USE THIS SPACE TO DIFFERENTIATE YOUR EMAIL FROM ADS]", + "utm_medium": "[NAME OF YOUR MARKETING MEDIUM e.g. email]", + "utm_name": "[NAME OF YOUR CAMPAIGN]", + "utm_term": "[IDENTIFY PAID KEYWORDS HERE]" + }, + "open_tracking": { + "enable": true, + "substitution_tag": "%opentrack" + }, + "subscription_tracking": { + "enable": true, + "html": "If you would like to unsubscribe and stop receiving these emails <% clickhere %>.", + "substitution_tag": "<%click here%>", + "text": "If you would like to unsubscribe and stop receiving these emails <% click here %>." + } + } + }`) + email := &mail.SGMailV3{} + err := json.Unmarshal(emailBytes, email) + assert.Nil(t, err, fmt.Sprintf("Unmarshal error: %v", err)) + client.Request.Headers["X-Mock"] = "202" + response, err := client.Send(email) + if err != nil { + t.Log(err) + } + t.Log(response) + assert.Equal(t, 202, response.StatusCode, "Wrong status code returned") + +} + func Test_test_send_client(t *testing.T) { apiKey := "SENDGRID_APIKEY" client := NewSendClient(apiKey) From b8d18a6312f78a0247e599c4960a2ad977fc87d3 Mon Sep 17 00:00:00 2001 From: Twilio Date: Thu, 10 Aug 2023 09:52:02 +0000 Subject: [PATCH 2/3] [Librarian] Version Bump --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e28d128e..db8a2199 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Change Log All notable changes to this project will be documented in this file. +[2023-08-10] Version 3.13.0 +--------------------------- +**Library - Feature** +- [PR #468](https://github.com/sendgrid/sendgrid-go/pull/468): gzip mail body when content-encoding is set to gzip. Thanks to [@Bankq](https://github.com/Bankq)! + + [2022-09-21] Version 3.12.0 --------------------------- **Library - Feature** From 72d6eb8df83d5cccf49a5184d99eab2796e6f805 Mon Sep 17 00:00:00 2001 From: Twilio Date: Thu, 10 Aug 2023 09:52:02 +0000 Subject: [PATCH 3/3] Release v3.13.0 --- base_interface.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base_interface.go b/base_interface.go index b7472a24..ae826811 100644 --- a/base_interface.go +++ b/base_interface.go @@ -15,7 +15,7 @@ import ( // Version is this client library's current version const ( - Version = "3.12.0" + Version = "3.13.0" rateLimitRetry = 5 rateLimitSleep = 1100 )