-
Notifications
You must be signed in to change notification settings - Fork 5
/
bounce.go
194 lines (158 loc) · 4.87 KB
/
bounce.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package postmark
import (
"errors"
"fmt"
"net/url"
"strconv"
"time"
)
const (
bounceDeliveryStatsAPIPath = "deliverystats"
bounceBouncesAPIPath = "bounces"
)
// BounceService handles communication with the bounce related methods of the
// Postmark API (http://developer.postmarkapp.com/developer-api-bounce.html)
type BounceService service
// BounceType represents the type of bounce with a count.
type BounceType struct {
Type string
Name string
Count int64
}
// BounceDeliveryStats represents a collection of bounce stats.
type BounceDeliveryStats struct {
InactiveMails int64
Bounces []BounceType
}
// Bounces represents a slice of bounces and a given count.
type Bounces struct {
TotalCount int64
Bounces []Bounce
}
// Bounce represents a BounceType in further detail.
type Bounce struct {
ID int64
Type string
TypeCode int64
Name string
Tag string
MessageID string
Description string
Details string
Email string
BouncedAt time.Time
DumpAvailable bool
Inactive bool
CanActivate bool
Subject string
}
// BounceDump represents a the raw source of bounce.
type BounceDump struct {
Body string
}
// BounceActivated represents a bounce that has been reactivated.
type BounceActivated struct {
Message string
Bounce Bounce
}
/*
* GetDeliveryStats --------------------------------------------------------- */
// GetDeliveryStats will return all delivery stats aggregated by bounce type.
func (s *BounceService) GetDeliveryStats() (*BounceDeliveryStats, *Response, error) {
request, err := s.client.NewRequest("GET", bounceDeliveryStatsAPIPath, nil)
if err != nil {
return nil, nil, err
}
deliveryStats := &BounceDeliveryStats{}
response, err := s.client.Do(request, deliveryStats)
if err != nil {
return nil, response, err
}
return deliveryStats, response, nil
}
/*
* GetBounces --------------------------------------------------------- */
// GetBounces will return all bounces.
func (s *BounceService) GetBounces(bounceCount, bounceOffset int, parameters map[string]interface{}) (*Bounces, *Response, error) {
// Ensure our bounce count meets criteria.
if bounceCount > 500 {
return nil, nil, errors.New("The max number of bounces to return per request is 500")
}
// Construct query parameters.
values := &url.Values{}
values.Add("count", strconv.Itoa(bounceCount))
values.Add("offset", strconv.Itoa(bounceOffset))
for key, value := range parameters {
values.Add(key, fmt.Sprintf("%v", value))
}
request, err := s.client.NewRequest("GET", fmt.Sprintf("%s?%s", bounceBouncesAPIPath, values.Encode()), nil)
if err != nil {
return nil, nil, err
}
bounces := Bounces{}
response, err := s.client.Do(request, &bounces)
if err != nil {
return nil, nil, err
}
return &bounces, response, nil
}
/*
* GetSingleBounce --------------------------------------------------------- */
// GetSingleBounce will return a single bounce by ID.
func (s *BounceService) GetSingleBounce(bounceID int64) (*Bounce, *Response, error) {
request, err := s.client.NewRequest("GET", fmt.Sprintf("%s/%v", bounceBouncesAPIPath, bounceID), nil)
if err != nil {
return nil, nil, err
}
bounce := Bounce{}
response, err := s.client.Do(request, &bounce)
if err != nil {
return nil, nil, err
}
return &bounce, response, nil
}
/*
* GetBounceDump --------------------------------------------------------- */
// GetBounceDump will return a single bounce dump by ID.
func (s *BounceService) GetBounceDump(bounceID int64) (*BounceDump, *Response, error) {
request, err := s.client.NewRequest("GET", fmt.Sprintf("%s/%v/dump", bounceBouncesAPIPath, bounceID), nil)
if err != nil {
return nil, nil, err
}
dump := BounceDump{}
response, err := s.client.Do(request, &dump)
if err != nil {
return nil, nil, err
}
return &dump, response, nil
}
/*
* ActivateBounce --------------------------------------------------------- */
// ActivateBounce will attempt to reactivate this email via bounce ID.
func (s *BounceService) ActivateBounce(bounceID int64) (*BounceActivated, *Response, error) {
request, err := s.client.NewRequest("PUT", fmt.Sprintf("%s/%v/activate", bounceBouncesAPIPath, bounceID), nil)
if err != nil {
return nil, nil, err
}
bounce := BounceActivated{}
response, err := s.client.Do(request, &bounce)
if err != nil {
return nil, nil, err
}
return &bounce, response, nil
}
/*
* GetBounceTags --------------------------------------------------------- */
// GetBounceTags will return a slice of tag values that have generated bounces.
func (s *BounceService) GetBounceTags() ([]string, *Response, error) {
request, err := s.client.NewRequest("GET", fmt.Sprintf("%s/tags", bounceBouncesAPIPath), nil)
if err != nil {
return nil, nil, err
}
tags := []string{}
response, err := s.client.Do(request, &tags)
if err != nil {
return nil, nil, err
}
return tags, response, nil
}