Skip to content

Commit

Permalink
feat: Add Batch Send (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Oct 18, 2023
1 parent 94814b3 commit 9397db6
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
51 changes: 51 additions & 0 deletions batch.go
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
}
46 changes: 46 additions & 0 deletions batch_test.go
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")
}
39 changes: 39 additions & 0 deletions examples/send_batch_email.go
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)
}
4 changes: 2 additions & 2 deletions examples/send_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func sendEmailExample() {

// Send
params := &resend.SendEmailRequest{
To: []string{"to@example.com", "to2@example.com"},
From: "from@example.com",
To: []string{"delivered@resend.dev"},
From: "onboarding@resend.dev",
Text: "hello world",
Subject: "Hello from Golang",
Cc: []string{"cc@example.com"},
Expand Down
2 changes: 2 additions & 0 deletions resend.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Client struct {

// Services
Emails EmailsSvc
Batch BatchSvc
ApiKeys ApiKeysSvc
Domains DomainsSvc
}
Expand All @@ -58,6 +59,7 @@ func NewCustomClient(httpClient *http.Client, apiKey string) *Client {
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}

c.Emails = &EmailsSvcImpl{client: c}
c.Batch = &BatchSvcImpl{client: c}
c.ApiKeys = &ApiKeysSvcImpl{client: c}
c.Domains = &DomainsSvcImpl{client: c}

Expand Down

0 comments on commit 9397db6

Please sign in to comment.