-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package resend | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
// BatchEmailResponse is the response from the BatchSendEmail call. | ||
// see https://resend.com/docs/api-reference/emails/send-batch-emails | ||
type BatchEmailResponse struct { | ||
Data []SendEmailResponse `json:"data"` | ||
} | ||
|
||
type BatchSvc interface { | ||
Send([]*SendEmailRequest) (*BatchEmailResponse, error) | ||
SendWithContext(ctx context.Context, params []*SendEmailRequest) (*BatchEmailResponse, error) | ||
} | ||
|
||
type BatchSvcImpl struct { | ||
client *Client | ||
} | ||
|
||
// Send send a batch of emails | ||
// https://resend.com/docs/api-reference/emails/send-batch-emails | ||
func (s *BatchSvcImpl) Send(params []*SendEmailRequest) (*BatchEmailResponse, error) { | ||
return s.SendWithContext(context.Background(), params) | ||
} | ||
|
||
// SendWithContext is the same as Send but accepts a ctx as argument | ||
func (s *BatchSvcImpl) SendWithContext(ctx context.Context, params []*SendEmailRequest) (*BatchEmailResponse, error) { | ||
path := "emails/batch" | ||
|
||
// Prepare request | ||
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params) | ||
if err != nil { | ||
return nil, errors.New("[ERROR]: Failed to create BatchEmail request") | ||
} | ||
|
||
// Build response recipient obj | ||
batchSendEmailResponse := new(BatchEmailResponse) | ||
|
||
// Send Request | ||
_, err = s.client.Perform(req, batchSendEmailResponse) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return batchSendEmailResponse, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package resend | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBatchSendEmail(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/emails/batch", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodPost) | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
ret := &BatchEmailResponse{ | ||
Data: []SendEmailResponse{ | ||
{Id: "1"}, | ||
{Id: "2"}, | ||
}, | ||
} | ||
err := json.NewEncoder(w).Encode(&ret) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
req := []*SendEmailRequest{ | ||
{ | ||
To: []string{"d@e.com"}, | ||
}, | ||
{ | ||
To: []string{"d@e.com"}, | ||
}, | ||
} | ||
resp, err := client.Batch.Send(req) | ||
if err != nil { | ||
t.Errorf("BatchEmail.Send returned error: %v", err) | ||
} | ||
assert.Equal(t, resp.Data[0].Id, "1") | ||
assert.Equal(t, resp.Data[1].Id, "2") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package examples | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/resendlabs/resend-go" | ||
) | ||
|
||
func sendBatchEmails() { | ||
|
||
// ctx := context.TODO() | ||
apiKey := os.Getenv("RESEND_API_KEY") | ||
|
||
client := resend.NewClient(apiKey) | ||
|
||
// Batch Send | ||
var batchEmails = []*resend.SendEmailRequest{ | ||
{ | ||
To: []string{"delivered@resend.dev"}, | ||
From: "onboarding@resend.dev", | ||
Text: "hey", | ||
Subject: "Hello from emails", | ||
}, | ||
{ | ||
To: []string{"delivered@resend.dev"}, | ||
From: "onboarding@resend.dev", | ||
Text: "hellooo", | ||
Subject: "Hello from batch emails 2", | ||
}, | ||
} | ||
|
||
sent, err := client.Batch.Send(batchEmails) | ||
// sent, err := client.Batch.SendWithContext(ctx, batchEmails) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(sent.Data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters