forked from mrz1836/postmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suppressions.go
158 lines (130 loc) · 5.24 KB
/
suppressions.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
package postmark
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// SuppressionReasonType - The reason type of suppression
type SuppressionReasonType string
// OriginType - The reason type of origin
type OriginType string
// SuppressionUpdateStatus - The status of suppression update
type SuppressionUpdateStatus string
const (
// HardBounceReason means an email sent to the address returned a hard bounce.
HardBounceReason SuppressionReasonType = "HardBounce"
// SpamComplaintReason means the recipient marked an email as spam.
SpamComplaintReason SuppressionReasonType = "SpamComplaint"
// ManualSuppressionReason means the recipient followed an unsubscribe link.
ManualSuppressionReason SuppressionReasonType = "ManualSuppression"
// RecipientOrigin means the email was added to the suppression list
// as a result of the recipient's own action, e.g. by following an unsubscribe link.
RecipientOrigin OriginType = "Recipient"
// CustomerOrigin means the email was added to the suppression list as
// the result of action by the Postmark account holder (e.g. Postmark's
// customer).
CustomerOrigin OriginType = "Customer"
// AdminOrigin means the email was added to the suppression list as
// the result of action by Postmark staff.
AdminOrigin OriginType = "Admin"
// SuppressionUpdateStatusSuppressed means the server successfully suppressed the email address.
SuppressionUpdateStatusSuppressed SuppressionUpdateStatus = "Suppressed"
// SuppressionUpdateStatusDeleted means the server successfully deleted the suppression.
SuppressionUpdateStatusDeleted SuppressionUpdateStatus = "Deleted"
// SuppressionUpdateStatusFailed means the server failed to update the suppression.
SuppressionUpdateStatusFailed SuppressionUpdateStatus = "Failed"
)
// Suppression contains a suppressed email address for a particular message stream.
type Suppression struct {
// EmailAddress is the address that is suppressed (can't be emailed any more)
EmailAddress string
// SuppressionReason is why the email address was added to the suppression list.
// Possible options: HardBounce, SpamComplaint, ManualSuppression
SuppressionReason SuppressionReasonType
// Origin describes who added the email address to the suppression list.
// Possible options: Recipient, Customer, Admin.
Origin OriginType
// CreatedAt is when the email address was added to the suppression list.
CreatedAt time.Time
}
// SuppressionResponse contains a status of suppression creation or deletion.
type SuppressionResponse struct {
// EmailAddress is the address that is suppressed (can't be emailed any more)
EmailAddress string
// Status of suppression creation or deletion.
Status SuppressionUpdateStatus
// If address cannot be suppressed or deleted (Status: Failed), the cause for failure is listed.
// Otherwise, this field is null.
Message string
}
// suppressionsResponse - A message received from the Postmark server
type suppressionsResponse struct {
// Suppressions - The slice of suppression email address.
Suppressions []Suppression
}
// updateSuppressionsResponse
type updateSuppressionsResponse struct {
// Suppressions - The slice of suppression email address.
Suppressions []SuppressionResponse
}
// suppressionsRequest - A message to send to the Postmark server for creating or deleting suppressions
type suppressionsRequest struct {
Suppressions []Suppression
}
// GetSuppressions fetches email addresses in the list of suppression dump on the server
// It returns a Suppressions slice, and any error that occurred
// https://postmarkapp.com/developer/api/suppressions-api#suppression-dump
func (client *Client) GetSuppressions(
ctx context.Context,
streamID string,
options map[string]interface{},
) ([]Suppression, error) {
values := &url.Values{}
for k, v := range options {
values.Add(k, fmt.Sprintf("%v", v))
}
path := fmt.Sprintf("message-streams/%s/suppressions/dump", streamID)
if len(options) != 0 {
path = fmt.Sprintf("%s?%s", path, values.Encode())
}
res := suppressionsResponse{}
err := client.doRequest(ctx, parameters{
Method: http.MethodGet,
Path: path,
TokenType: serverToken,
}, &res)
return res.Suppressions, err
}
// CreateSuppressions creates email addresses in the suppression list on the server.
func (client *Client) CreateSuppressions(
ctx context.Context,
streamID string,
suppressions []Suppression,
) ([]SuppressionResponse, error) {
res := updateSuppressionsResponse{}
err := client.doRequest(ctx, parameters{
Method: http.MethodPost,
Path: fmt.Sprintf("message-streams/%s/suppressions", streamID),
Payload: suppressionsRequest{Suppressions: suppressions},
TokenType: serverToken,
}, &res)
return res.Suppressions, err
}
// DeleteSuppressions deletes email addresses from the suppression list on the server.
// SpamComplaint suppressions cannot be deleted.
func (client *Client) DeleteSuppressions(
ctx context.Context,
streamID string,
suppressions []Suppression,
) ([]SuppressionResponse, error) {
res := updateSuppressionsResponse{}
err := client.doRequest(ctx, parameters{
Method: http.MethodPost,
Path: fmt.Sprintf("message-streams/%s/suppressions/delete", streamID),
Payload: suppressionsRequest{Suppressions: suppressions},
TokenType: serverToken,
}, &res)
return res.Suppressions, err
}