Skip to content

Commit

Permalink
Merge pull request #290 from hownowstephen/batch_bounces_complaints
Browse files Browse the repository at this point in the history
Add support for batch endpoints for adding bounces and complaints
  • Loading branch information
thrawn01 committed Feb 10, 2023
2 parents 5bc06f1 + 727aa67 commit 58b1f48
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 13 deletions.
12 changes: 12 additions & 0 deletions bounces.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ func (mg *MailgunImpl) AddBounce(ctx context.Context, address, code, error strin
return err
}

// Add Bounces adds a list of bounces to the bounce list
func (mg *MailgunImpl) AddBounces(ctx context.Context, bounces []Bounce) error {
r := newHTTPRequest(generateApiUrl(mg, bouncesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())

payload := newJSONEncodedPayload(bounces)

_, err := makePostRequest(ctx, r, payload)
return err
}

// DeleteBounce removes all bounces associted with the provided e-mail address.
func (mg *MailgunImpl) DeleteBounce(ctx context.Context, address string) error {
r := newHTTPRequest(generateApiUrl(mg, bouncesEndpoint) + "/" + address)
Expand Down
49 changes: 36 additions & 13 deletions bounces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestAddDelBounces(t *testing.T) {
ensure.NotNil(t, err)
}

func TestDelBounceList(t *testing.T) {
func TestAddDelBounceList(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())

Expand All @@ -127,25 +127,48 @@ func TestDelBounceList(t *testing.T) {
return false
}

for i := 0; i < 3; i++ {
// Compute an e-mail address for our Bounce.
exampleEmail := fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain)
createdAt, err := mailgun.NewRFC2822Time("Thu, 13 Oct 2011 18:02:00 UTC")
if err != nil {
t.Fatalf("invalid time")
}

// Add the bounce for our address.
err := mg.AddBounce(ctx, exampleEmail, "550", "TestAddDelBounces-generated error")
ensure.Nil(t, err)
// Generate a list of bounces
bounces := []mailgun.Bounce{
{
Code: "550",
Address: fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain),
Error: "TestAddDelBounces-generated error",
},
{
Code: "550",
Address: fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain),
Error: "TestAddDelBounces-generated error",
CreatedAt: createdAt,
},
}

// Give API some time to refresh cache
time.Sleep(time.Second)
// Add the bounce for our address.
err = mg.AddBounces(ctx, bounces)
ensure.Nil(t, err)

// We should now have one bounce listed when we query the API.
if !findBounce(exampleEmail) {
t.Fatalf("Expected bounce for address %s in list of bounces", exampleEmail)
for _, expect := range bounces {
if !findBounce(expect.Address) {
t.Fatalf("Expected bounce for address %s in list of bounces", expect.Address)
}

bounce, err := mg.GetBounce(ctx, expect.Address)
ensure.Nil(t, err)
if bounce.Address != expect.Address {
t.Fatalf("Expected at least one bounce for %s", expect.Address)
}
t.Logf("Bounce Created At: %s", bounce.CreatedAt)
if !expect.CreatedAt.IsZero() && bounce.CreatedAt != expect.CreatedAt {
t.Fatalf("Expected bounce createdAt to be %s, got %s", expect.CreatedAt, bounce.CreatedAt)
}
}

// Delete the bounce list. This should put us back the way we were.
err := mg.DeleteBounceList(ctx)
err = mg.DeleteBounceList(ctx)
ensure.Nil(t, err)

it := mg.ListBounces(nil)
Expand Down
16 changes: 16 additions & 0 deletions spam_complaints.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ func (mg *MailgunImpl) CreateComplaint(ctx context.Context, address string) erro
return err
}

func (mg *MailgunImpl) CreateComplaints(ctx context.Context, addresses []string) error {
r := newHTTPRequest(generateApiUrl(mg, complaintsEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())

body := make([]map[string]string, len(addresses))
for i, addr := range addresses {
body[i] = map[string]string{"address": addr}
}

payload := newJSONEncodedPayload(body)

_, err := makePostRequest(ctx, r, payload)
return err
}

// DeleteComplaint removes a previously registered e-mail address from the list of people who complained
// of receiving spam from your domain.
func (mg *MailgunImpl) DeleteComplaint(ctx context.Context, address string) error {
Expand Down
38 changes: 38 additions & 0 deletions spam_complaints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,41 @@ func TestCreateDeleteComplaint(t *testing.T) {
ensure.Nil(t, mg.DeleteComplaint(ctx, randomMail))
ensure.False(t, hasComplaint(randomMail))
}

func TestCreateDeleteComplaintList(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()

var hasComplaint = func(email string) bool {
t.Logf("hasComplaint: %s\n", email)
it := mg.ListComplaints(nil)
ensure.Nil(t, it.Err())

var page []mailgun.Complaint
for it.Next(ctx, &page) {
for _, complaint := range page {
t.Logf("Complaint Address: %s\n", complaint.Address)
if complaint.Address == email {
return true
}
}
}
return false
}

addresses := []string{
strings.ToLower(randomString(64, "")) + "@example1.com",
strings.ToLower(randomString(64, "")) + "@example2.com",
strings.ToLower(randomString(64, "")) + "@example3.com",
}

ensure.Nil(t, mg.CreateComplaints(ctx, addresses))

for _, address := range addresses {
ensure.True(t, hasComplaint(address))
ensure.Nil(t, mg.DeleteComplaint(ctx, address))
ensure.False(t, hasComplaint(address))
}

}

0 comments on commit 58b1f48

Please sign in to comment.