|
| 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 | +} |
0 commit comments