-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Schedule, cancel and update email (#38)
Co-authored-by: Felipe Volpone <[email protected]>
- Loading branch information
1 parent
b9dac9d
commit 37ced1d
Showing
5 changed files
with
193 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,35 @@ func teardown() { | |
server.Close() | ||
} | ||
|
||
func TestScheduleEmail(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/emails", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodPost) | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
ret := &SendEmailResponse{ | ||
Id: "1923781293", | ||
} | ||
err := json.NewEncoder(w).Encode(&ret) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
req := &SendEmailRequest{ | ||
To: []string{"[email protected]"}, | ||
ScheduledAt: "2024-09-05T11:52:01.858Z", | ||
} | ||
resp, err := client.Emails.Send(req) | ||
if err != nil { | ||
t.Errorf("Emails.Send returned error: %v", err) | ||
} | ||
assert.Equal(t, resp.Id, "1923781293") | ||
} | ||
|
||
func TestSendEmail(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
@@ -132,6 +161,31 @@ func TestGetEmail(t *testing.T) { | |
assert.Equal(t, resp.Subject, "Hello World") | ||
} | ||
|
||
func TestCancelScheduledEmail(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/emails/dacf4072-4119-4d88-932f-6202748ac7c8/cancel", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodPost) | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
ret := ` | ||
{ | ||
"id": "dacf4072-4119-4d88-932f-6202748ac7c8", | ||
"object": "email" | ||
}` | ||
fmt.Fprintf(w, ret) | ||
}) | ||
|
||
resp, err := client.Emails.Cancel("dacf4072-4119-4d88-932f-6202748ac7c8") | ||
if err != nil { | ||
t.Errorf("Emails.Cancel returned error: %v", err) | ||
} | ||
assert.Equal(t, resp.Id, "dacf4072-4119-4d88-932f-6202748ac7c8") | ||
assert.Equal(t, resp.Object, "email") | ||
} | ||
|
||
func testMethod(t *testing.T, r *http.Request, expected string) { | ||
if expected != r.Method { | ||
t.Errorf("Request method = %v, expected %v", r.Method, expected) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package examples | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/resend/resend-go/v2" | ||
) | ||
|
||
func scheduleEmail() { | ||
ctx := context.TODO() | ||
apiKey := os.Getenv("RESEND_API_KEY") | ||
|
||
client := resend.NewClient(apiKey) | ||
|
||
// Schedule the email | ||
params := &resend.SendEmailRequest{ | ||
To: []string{"[email protected]"}, | ||
From: "[email protected]", | ||
Text: "hello world", | ||
Subject: "Hello from Golang", | ||
ScheduledAt: "2024-09-05T11:52:01.858Z", | ||
} | ||
|
||
sent, err := client.Emails.SendWithContext(ctx, params) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(sent.Id) | ||
|
||
updateParams := &resend.UpdateEmailRequest{ | ||
Id: sent.Id, | ||
ScheduledAt: "2024-11-05T11:52:01.858Z", | ||
} | ||
|
||
// Update the scheduled email | ||
updatedEmail, err := client.Emails.UpdateWithContext(ctx, updateParams) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("%v\n", updatedEmail) | ||
|
||
canceled, err := client.Emails.CancelWithContext(ctx, "32723fee-8502-4b58-8b5e-bfd98f453ced") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("%v\n", canceled) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters