Skip to content

Commit 7653e04

Browse files
authored
Merge pull request #26 from fawkesley/get-suppressions
add client.GetSuppressions
2 parents 5c602f6 + c598f28 commit 7653e04

2 files changed

Lines changed: 173 additions & 0 deletions

File tree

suppressions.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package postmark
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
"time"
8+
)
9+
10+
// SuppressionReasonType - The reason type of suppression
11+
type SuppressionReasonType string
12+
13+
// OriginType - The reason type of origin
14+
type OriginType string
15+
16+
const (
17+
// HardBounceReason means an email sent to the address returned a hard bounce.
18+
HardBounceReason SuppressionReasonType = "HardBounce"
19+
20+
// SpamComplaintReason means the recipient marked an email as spam.
21+
SpamComplaintReason SuppressionReasonType = "SpamComplaint"
22+
23+
// ManualSuppressionReason means the recipient followed an unsubscribe link.
24+
ManualSuppressionReason SuppressionReasonType = "ManualSuppression"
25+
26+
// RecipientOrigin means the email was added to the suppression list
27+
// as a result of the recipient's own action, e.g. by following an unsubscribe link.
28+
RecipientOrigin OriginType = "Recipient"
29+
30+
// CustomerOrigin means the email was added to the suppression list as
31+
// the result of action by the Postmark account holder (e.g. Postmark's
32+
// customer).
33+
CustomerOrigin OriginType = "Customer"
34+
35+
// AdminOrigin means the email was added to the suppression list as
36+
// the result of action by Postmark staff.
37+
AdminOrigin OriginType = "Admin"
38+
)
39+
40+
// Suppression contains a suppressed email address for a particular message stream.
41+
type Suppression struct {
42+
// EmailAddress is the address that is suppressed (can't be emailed any more)
43+
EmailAddress string
44+
45+
// SuppressionReason is why the email address was added to the suppression list.
46+
// Possible options: HardBounce, SpamComplaint, ManualSuppression
47+
SuppressionReason SuppressionReasonType
48+
49+
// Origin describes who added the email address to the suppression list.
50+
// Possible options: Recipient, Customer, Admin.
51+
Origin OriginType
52+
53+
// CreatedAt is when the email address was added to the suppression list.
54+
CreatedAt time.Time
55+
}
56+
57+
// suppressionsResponse - A message received from the Postmark server
58+
type suppressionsResponse struct {
59+
// Suppressions - The slice of suppression email address.
60+
Suppressions []Suppression
61+
}
62+
63+
// GetSuppressions fetches email addresses in the list of suppression dump on the server
64+
// It returns a Suppressions slice, and any error that occurred
65+
// https://postmarkapp.com/developer/api/suppressions-api#suppression-dump
66+
func (client *Client) GetSuppressions(
67+
ctx context.Context,
68+
streamID string,
69+
options map[string]interface{},
70+
) ([]Suppression, error) {
71+
72+
values := &url.Values{}
73+
for k, v := range options {
74+
values.Add(k, fmt.Sprintf("%v", v))
75+
}
76+
77+
path := fmt.Sprintf("message-streams/%s/suppressions/dump", streamID)
78+
if len(options) != 0 {
79+
path = fmt.Sprintf("%s?%s", path, values.Encode())
80+
}
81+
82+
res := suppressionsResponse{}
83+
err := client.doRequest(ctx, parameters{
84+
Method: "GET",
85+
Path: path,
86+
TokenType: serverToken,
87+
}, &res)
88+
return res.Suppressions, err
89+
}

suppressions_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package postmark
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"testing"
7+
8+
"goji.io/pat"
9+
)
10+
11+
func TestGetSuppressions(t *testing.T) {
12+
responseJSON := `{
13+
"Suppressions":[
14+
{
15+
"EmailAddress":"address@wildbit.com",
16+
"SuppressionReason":"ManualSuppression",
17+
"Origin": "Recipient",
18+
"CreatedAt":"2019-12-10T08:58:33-05:00"
19+
},
20+
{
21+
"EmailAddress":"bounce.address@wildbit.com",
22+
"SuppressionReason":"HardBounce",
23+
"Origin": "Recipient",
24+
"CreatedAt":"2019-12-11T08:58:33-05:00"
25+
},
26+
{
27+
"EmailAddress":"spam.complaint.address@wildbit.com",
28+
"SuppressionReason":"SpamComplaint",
29+
"Origin": "Recipient",
30+
"CreatedAt":"2019-12-12T08:58:33-05:00"
31+
}
32+
]
33+
}`
34+
35+
tMux.HandleFunc(pat.Get("/message-streams/:StreamID/suppressions/dump"), func(w http.ResponseWriter, req *http.Request) {
36+
w.Write([]byte(responseJSON))
37+
})
38+
39+
res, err := client.GetSuppressions(context.Background(), "outbound", nil)
40+
41+
if err != nil {
42+
t.Fatalf("GetSuppressions: %s", err.Error())
43+
}
44+
45+
if len(res) != 3 {
46+
t.Fatalf("GetSuppressions: wrong number of suppression (%d)", len(res))
47+
}
48+
49+
if res[0].EmailAddress != "address@wildbit.com" {
50+
t.Fatalf("GetSuppressions: wrong suppression email address: %s", res[0].EmailAddress)
51+
}
52+
53+
responseJSON = `{
54+
"Suppressions":[
55+
{
56+
"EmailAddress":"address@wildbit.com",
57+
"SuppressionReason":"ManualSuppression",
58+
"Origin": "Recipient",
59+
"CreatedAt":"2019-12-10T08:58:33-05:00"
60+
}
61+
]
62+
}`
63+
64+
tMux.HandleFunc(pat.Get("/message-streams/:StreamID/suppressions/dump"), func(w http.ResponseWriter, req *http.Request) {
65+
w.Write([]byte(responseJSON))
66+
})
67+
68+
res, err = client.GetSuppressions(context.Background(), "outbound", map[string]interface{}{
69+
"emailaddress": "address@wildbit.com",
70+
"fromdate": "2019-12-10",
71+
"todate": "2019-12-11",
72+
"suppressionreason": HardBounceReason,
73+
"origin": RecipientOrigin,
74+
})
75+
76+
if len(res) != 1 {
77+
t.Fatalf("GetSuppressions: wrong number of suppression (%d)", len(res))
78+
}
79+
80+
if res[0].EmailAddress != "address@wildbit.com" {
81+
t.Fatalf("GetSuppressions: wrong suppression email address: %s", res[0].EmailAddress)
82+
}
83+
84+
}

0 commit comments

Comments
 (0)