-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbatch.go
51 lines (40 loc) · 1.34 KB
/
batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
}