From 9ac2d3ac347b893aaa79acac5c4a747877b59f36 Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Thu, 27 Oct 2022 11:59:28 -0400 Subject: [PATCH] Updated postmark package to use CTX --- postmark.go | 11 ++++++----- postmark_test.go | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/postmark.go b/postmark.go index 07b7987..b230ef2 100644 --- a/postmark.go +++ b/postmark.go @@ -2,9 +2,10 @@ package gomail import ( "bufio" + "context" "encoding/base64" "fmt" - "io/ioutil" + "io" "log" "strings" @@ -13,11 +14,11 @@ import ( // postmarkInterface is an interface for Postmark/mocking type postmarkInterface interface { - SendEmail(email postmark.Email) (postmark.EmailResponse, error) + SendEmail(ctx context.Context, email postmark.Email) (postmark.EmailResponse, error) } // sendViaPostmark sends an email using the Postmark service -func sendViaPostmark(client postmarkInterface, email *Email) (err error) { +func sendViaPostmark(ctx context.Context, client postmarkInterface, email *Email) (err error) { // Create the email struct postmarkEmail := postmark.Email{ @@ -73,7 +74,7 @@ func sendViaPostmark(client postmarkInterface, email *Email) (err error) { // Read all content from the attachment reader := bufio.NewReader(attachment.FileReader) var content []byte - if content, err = ioutil.ReadAll(reader); err != nil { + if content, err = io.ReadAll(reader); err != nil { return } @@ -96,7 +97,7 @@ func sendViaPostmark(client postmarkInterface, email *Email) (err error) { // Send the email var resp postmark.EmailResponse - if resp, err = client.SendEmail(postmarkEmail); err != nil { + if resp, err = client.SendEmail(ctx, postmarkEmail); err != nil { return } diff --git a/postmark_test.go b/postmark_test.go index 3b3ed9e..4dc2408 100644 --- a/postmark_test.go +++ b/postmark_test.go @@ -1,6 +1,7 @@ package gomail import ( + "context" "fmt" "net/http" "os" @@ -13,7 +14,7 @@ import ( type mockPostmarkInterface struct{} // SendEmail is for mocking -func (m *mockPostmarkInterface) SendEmail(email postmark.Email) (postmark.EmailResponse, error) { +func (m *mockPostmarkInterface) SendEmail(_ context.Context, email postmark.Email) (postmark.EmailResponse, error) { // Success if email.To == "test@domain.com" { @@ -96,7 +97,7 @@ func TestSendViaPostmark(t *testing.T) { email.RecipientsCc = []string{test.input} email.RecipientsBcc = []string{test.input} email.ReplyToAddress = test.input - if err = sendViaPostmark(client, email); err != nil && !test.expectedError { + if err = sendViaPostmark(context.Background(), client, email); err != nil && !test.expectedError { t.Fatalf("%s Failed: expected to NOT throw an error, inputted and [%s], error [%s]", t.Name(), test.input, err.Error()) } else if err == nil && test.expectedError { t.Fatalf("%s Failed: expected to throw an error, inputted and [%s]", t.Name(), test.input)