Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions broadcasts.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ type SendBroadcastResponse struct {
Id string `json:"id"`
}

type CancelBroadcastResponse struct {
Object string `json:"object"`
Id string `json:"id"`
}

type RemoveBroadcastResponse struct {
Object string `json:"object"`
Id string `json:"id"`
Expand Down Expand Up @@ -104,6 +109,9 @@ type BroadcastsSvc interface {
SendWithContext(ctx context.Context, params *SendBroadcastRequest) (SendBroadcastResponse, error)
Send(params *SendBroadcastRequest) (SendBroadcastResponse, error)

CancelWithContext(ctx context.Context, broadcastId string) (CancelBroadcastResponse, error)
Cancel(broadcastId string) (CancelBroadcastResponse, error)

RemoveWithContext(ctx context.Context, broadcastId string) (RemoveBroadcastResponse, error)
Remove(broadcastId string) (RemoveBroadcastResponse, error)
}
Expand Down Expand Up @@ -251,6 +259,35 @@ func (s *BroadcastsSvcImpl) Send(params *SendBroadcastRequest) (SendBroadcastRes
return s.SendWithContext(context.Background(), params)
}

func (s *BroadcastsSvcImpl) CancelWithContext(ctx context.Context, broadcastId string) (CancelBroadcastResponse, error) {
if broadcastId == "" {
return CancelBroadcastResponse{}, errors.New("[ERROR]: broadcastId cannot be empty")
}

path := "broadcasts/" + broadcastId + "/cancel"

// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return CancelBroadcastResponse{}, ErrFailedToCreateBroadcastCancelRequest
}

resp := new(CancelBroadcastResponse)

// Send Request
_, err = s.client.Perform(req, resp)

if err != nil {
return CancelBroadcastResponse{}, err
}

return *resp, nil
}

func (s *BroadcastsSvcImpl) Cancel(broadcastId string) (CancelBroadcastResponse, error) {
return s.CancelWithContext(context.Background(), broadcastId)
}

// 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) {
Expand Down
26 changes: 26 additions & 0 deletions broadcasts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,32 @@ func TestRemoveBroadcast(t *testing.T) {
assert.Equal(t, deleted.Object, "broadcast")
}

func TestCancelBroadcast(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/broadcasts/b6d24b8e-af0b-4c3c-be0c-359bbd97381e/cancel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusOK)

var ret any
ret = `
{
"object": "broadcast",
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e"
}`

fmt.Fprint(w, ret)
})

canceled, err := client.Broadcasts.Cancel("b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
if err != nil {
t.Errorf("Broadcasts.Cancel returned error: %v", err)
}
assert.Equal(t, canceled.Id, "b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
assert.Equal(t, canceled.Object, "broadcast")
}

func TestListBroadcasts(t *testing.T) {
setup()
defer teardown()
Expand Down
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
ErrFailedToCreateBroadcastUpdateRequest = errors.New("[ERROR]: Failed to create Broadcasts.Update request")
ErrFailedToCreateBroadcastSendRequest = errors.New("[ERROR]: Failed to create Broadcasts.Send request")
ErrFailedToCreateBroadcastCreateRequest = errors.New("[ERROR]: Failed to create Broadcasts.Create request")
ErrFailedToCreateBroadcastCancelRequest = errors.New("[ERROR]: Failed to create Broadcasts.Cancel request")
)

// ApiKeySvc errors
Expand Down
Loading