-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbroadcasts.go
243 lines (187 loc) · 7.04 KB
/
broadcasts.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package resend
import (
"context"
"errors"
"net/http"
)
type SendBroadcastRequest struct {
BroadcastId string `json:"broadcast_id"`
//Schedule email to be sent later. The date should be in language natural (e.g.: in 1 min)
// or ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z).
ScheduledAt string `json:"scheduled_at"`
}
type CreateBroadcastRequest struct {
AudienceId string `json:"audience_id"`
From string `json:"from"`
Subject string `json:"subject"`
ReplyTo []string `json:"reply_to"`
Html string `json:"html"`
Text string `json:"text"`
Name string `json:"name"`
}
type CreateBroadcastResponse struct {
Id string `json:"id"`
}
type SendBroadcastResponse struct {
Id string `json:"id"`
}
type RemoveBroadcastResponse struct {
Object string `json:"object"`
Id string `json:"id"`
Deleted bool `json:"deleted"`
}
type ListBroadcastsResponse struct {
Object string `json:"object"`
Data []Broadcast `json:"data"`
}
type Broadcast struct {
Object string `json:"object"`
Id string `json:"id"`
Name string `json:"name"`
AudienceId string `json:"audience_id"`
From string `json:"from"`
Subject string `json:"subject"`
ReplyTo []string `json:"reply_to"`
PreviewText string `json:"preview_text"`
Status string `json:"status"`
CreatedAt string `json:"created_at"`
ScheduledAt string `json:"scheduled_at"`
SentAt string `json:"sent_at"`
}
type BroadcastsSvc interface {
CreateWithContext(ctx context.Context, params *CreateBroadcastRequest) (CreateBroadcastResponse, error)
Create(params *CreateBroadcastRequest) (CreateBroadcastResponse, error)
ListWithContext(ctx context.Context) (ListBroadcastsResponse, error)
List() (ListBroadcastsResponse, error)
GetWithContext(ctx context.Context, broadcastId string) (Broadcast, error)
Get(broadcastId string) (Broadcast, error)
SendWithContext(ctx context.Context, params *SendBroadcastRequest) (SendBroadcastResponse, error)
Send(params *SendBroadcastRequest) (SendBroadcastResponse, error)
RemoveWithContext(ctx context.Context, broadcastId string) (RemoveBroadcastResponse, error)
Remove(broadcastId string) (RemoveBroadcastResponse, error)
}
type BroadcastsSvcImpl struct {
client *Client
}
// CreateWithContext creates a new Broadcast based on the given params
// https://resend.com/docs/api-reference/broadcasts/create-broadcast
func (s *BroadcastsSvcImpl) CreateWithContext(ctx context.Context, params *CreateBroadcastRequest) (CreateBroadcastResponse, error) {
path := "/broadcasts"
if params.AudienceId == "" {
return CreateBroadcastResponse{}, errors.New("[ERROR]: AudienceId cannot be empty")
}
if params.From == "" {
return CreateBroadcastResponse{}, errors.New("[ERROR]: From cannot be empty")
}
if params.Subject == "" {
return CreateBroadcastResponse{}, errors.New("[ERROR]: Subject cannot be empty")
}
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return CreateBroadcastResponse{}, ErrFailedToCreateBroadcastCreateRequest
}
// Build response recipient obj
broadcastResp := new(CreateBroadcastResponse)
// Send Request
_, err = s.client.Perform(req, broadcastResp)
if err != nil {
return CreateBroadcastResponse{}, err
}
return *broadcastResp, nil
}
// Create creates a new Broadcast based on the given params
func (s *BroadcastsSvcImpl) Create(params *CreateBroadcastRequest) (CreateBroadcastResponse, error) {
return s.CreateWithContext(context.Background(), params)
}
// GetWithContext Retrieve a single broadcast.
// https://resend.com/docs/api-reference/broadcasts/get-broadcast
func (s *BroadcastsSvcImpl) GetWithContext(ctx context.Context, broadcastId string) (Broadcast, error) {
if broadcastId == "" {
return Broadcast{}, errors.New("[ERROR]: broadcastId cannot be empty")
}
path := "broadcasts/" + broadcastId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return Broadcast{}, errors.New("[ERROR]: Failed to create Broadcast.Get request")
}
broadcast := new(Broadcast)
// Send Request
_, err = s.client.Perform(req, broadcast)
if err != nil {
return Broadcast{}, err
}
return *broadcast, nil
}
// Get retrieves a single broadcast.
func (s *BroadcastsSvcImpl) Get(broadcastId string) (Broadcast, error) {
return s.GetWithContext(context.Background(), broadcastId)
}
// SendWithContext Sends broadcasts to your audience.
// https://resend.com/docs/api-reference/broadcasts/send-broadcast
func (s *BroadcastsSvcImpl) SendWithContext(ctx context.Context, params *SendBroadcastRequest) (SendBroadcastResponse, error) {
if params.BroadcastId == "" {
return SendBroadcastResponse{}, errors.New("[ERROR]: BroadcastId cannot be empty")
}
path := "/broadcasts/" + params.BroadcastId + "/send"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return SendBroadcastResponse{}, ErrFailedToCreateBroadcastSendRequest
}
// Build response recipient obj
broadcastResp := new(SendBroadcastResponse)
// Send Request
_, err = s.client.Perform(req, broadcastResp)
if err != nil {
return SendBroadcastResponse{}, err
}
return *broadcastResp, nil
}
// Send sends broadcasts to your audience.
func (s *BroadcastsSvcImpl) Send(params *SendBroadcastRequest) (SendBroadcastResponse, error) {
return s.SendWithContext(context.Background(), params)
}
// RemoveWithContext removes a given broadcast by id
// https://resend.com/docs/api-reference/broadcasts/delete-broadcast
func (s *BroadcastsSvcImpl) RemoveWithContext(ctx context.Context, broadcastId string) (RemoveBroadcastResponse, error) {
path := "broadcasts/" + broadcastId
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return RemoveBroadcastResponse{}, errors.New("[ERROR]: Failed to create Broadcast.Remove request")
}
resp := new(RemoveBroadcastResponse)
// Send Request
_, err = s.client.Perform(req, resp)
if err != nil {
return RemoveBroadcastResponse{}, err
}
return *resp, nil
}
// Remove removes a given broadcast entry by id
func (s *BroadcastsSvcImpl) Remove(broadcastId string) (RemoveBroadcastResponse, error) {
return s.RemoveWithContext(context.Background(), broadcastId)
}
// ListWithContext returns the list of all broadcasts
// https://resend.com/docs/api-reference/broadcasts/list-broadcasts
func (s *BroadcastsSvcImpl) ListWithContext(ctx context.Context) (ListBroadcastsResponse, error) {
path := "broadcasts"
// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return ListBroadcastsResponse{}, errors.New("[ERROR]: Failed to create Broadcasts.List request")
}
broadcasts := new(ListBroadcastsResponse)
// Send Request
_, err = s.client.Perform(req, broadcasts)
if err != nil {
return ListBroadcastsResponse{}, err
}
return *broadcasts, nil
}
// List returns the list of all broadcasts
func (s *BroadcastsSvcImpl) List() (ListBroadcastsResponse, error) {
return s.ListWithContext(context.Background())
}