diff --git a/batch.go b/batch.go new file mode 100644 index 0000000..91d6ad5 --- /dev/null +++ b/batch.go @@ -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 +} diff --git a/batch_test.go b/batch_test.go new file mode 100644 index 0000000..4a55e14 --- /dev/null +++ b/batch_test.go @@ -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") +} diff --git a/examples/send_batch_email.go b/examples/send_batch_email.go new file mode 100644 index 0000000..a982d0c --- /dev/null +++ b/examples/send_batch_email.go @@ -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) +} diff --git a/examples/send_email.go b/examples/send_email.go index 3105119..34cc542 100644 --- a/examples/send_email.go +++ b/examples/send_email.go @@ -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"}, diff --git a/resend.go b/resend.go index 6a80633..3bdbf27 100644 --- a/resend.go +++ b/resend.go @@ -37,6 +37,7 @@ type Client struct { // Services Emails EmailsSvc + Batch BatchSvc ApiKeys ApiKeysSvc Domains DomainsSvc } @@ -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}