Skip to content

Commit f338051

Browse files
Pass context when calling Postmark API
1 parent 28358b1 commit f338051

12 files changed

+298
-90
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ _testmain.go
2727
*.exe
2828
*.test
2929
*.prof
30+
31+
.idea

bounce.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postmark
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"net/url"
@@ -29,11 +30,16 @@ type DeliveryStats struct {
2930
Bounces []BounceType
3031
}
3132

32-
// GetDeliveryStats returns delivery stats for the server
33+
// GetDeliveryStats calls GetDeliveryStatsWithContext with empty context
3334
func (client *Client) GetDeliveryStats() (DeliveryStats, error) {
35+
return client.GetDeliveryStatsWithContext(context.TODO())
36+
}
37+
38+
// GetDeliveryStatsWithContext returns delivery stats for the server
39+
func (client *Client) GetDeliveryStatsWithContext(ctx context.Context) (DeliveryStats, error) {
3440
res := DeliveryStats{}
3541
path := "deliverystats"
36-
err := client.doRequest(parameters{
42+
err := client.doRequest(ctx, parameters{
3743
Method: "GET",
3844
Path: path,
3945
TokenType: server_token,
@@ -81,10 +87,15 @@ type bouncesResponse struct {
8187
Bounces []Bounce
8288
}
8389

84-
// GetBounces returns bounces for the server
90+
// GetBounces calls GetBouncesWithContext with empty context
91+
func (client *Client) GetBounces(count int64, offset int64, options map[string]interface{}) ([]Bounce, int64, error) {
92+
return client.GetBouncesWithContext(context.TODO(), count, offset, options)
93+
}
94+
95+
// GetBouncesWithContext returns bounces for the server
8596
// It returns a Bounce slice, the total bounce count, and any error that occurred
8697
// Available options: http://developer.postmarkapp.com/developer-api-bounce.html#bounces
87-
func (client *Client) GetBounces(count int64, offset int64, options map[string]interface{}) ([]Bounce, int64, error) {
98+
func (client *Client) GetBouncesWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]Bounce, int64, error) {
8899
res := bouncesResponse{}
89100

90101
values := &url.Values{}
@@ -97,7 +108,7 @@ func (client *Client) GetBounces(count int64, offset int64, options map[string]i
97108

98109
path := fmt.Sprintf("bounces?%s", values.Encode())
99110

100-
err := client.doRequest(parameters{
111+
err := client.doRequest(ctx, parameters{
101112
Method: "GET",
102113
Path: path,
103114
TokenType: server_token,
@@ -108,11 +119,16 @@ func (client *Client) GetBounces(count int64, offset int64, options map[string]i
108119
///////////////////////////////////////
109120
///////////////////////////////////////
110121

111-
// GetBounce fetches a single bounce with bounceID
122+
// GetBounce calls GetBounceWithContext with empty context
112123
func (client *Client) GetBounce(bounceID int64) (Bounce, error) {
124+
return client.GetBounceWithContext(context.TODO(), bounceID)
125+
}
126+
127+
// GetBounceWithContext fetches a single bounce with bounceID
128+
func (client *Client) GetBounceWithContext(ctx context.Context, bounceID int64) (Bounce, error) {
113129
res := Bounce{}
114130
path := fmt.Sprintf("bounces/%v", bounceID)
115-
err := client.doRequest(parameters{
131+
err := client.doRequest(ctx, parameters{
116132
Method: "GET",
117133
Path: path,
118134
TokenType: server_token,
@@ -127,11 +143,16 @@ type dumpResponse struct {
127143
Body string
128144
}
129145

130-
// GetBounceDump fetches a SMTP data dump for a single bounce
146+
// GetBounceDump calls GetBounceDumpWithContext with empty context
131147
func (client *Client) GetBounceDump(bounceID int64) (string, error) {
148+
return client.GetBounceDumpWithContext(context.TODO(), bounceID)
149+
}
150+
151+
// GetBounceDumpWithContext fetches a SMTP data dump for a single bounce
152+
func (client *Client) GetBounceDumpWithContext(ctx context.Context, bounceID int64) (string, error) {
132153
res := dumpResponse{}
133154
path := fmt.Sprintf("bounces/%v/dump", bounceID)
134-
err := client.doRequest(parameters{
155+
err := client.doRequest(ctx, parameters{
135156
Method: "GET",
136157
Path: path,
137158
TokenType: server_token,
@@ -147,13 +168,18 @@ type activateBounceResponse struct {
147168
Bounce Bounce
148169
}
149170

150-
// ActivateBounce reactivates a bounce for resending. Returns the bounce, a
171+
// ActivateBounce calls ActivateBounceWithContext with empty context
172+
func (client *Client) ActivateBounce(bounceID int64) (Bounce, string, error) {
173+
return client.ActivateBounceWithContext(context.TODO(), bounceID)
174+
}
175+
176+
// ActivateBounceWithContext reactivates a bounce for resending. Returns the bounce, a
151177
// message, and any error that occurs
152178
// TODO: clarify this with Postmark
153-
func (client *Client) ActivateBounce(bounceID int64) (Bounce, string, error) {
179+
func (client *Client) ActivateBounceWithContext(ctx context.Context, bounceID int64) (Bounce, string, error) {
154180
res := activateBounceResponse{}
155181
path := fmt.Sprintf("bounces/%v/activate", bounceID)
156-
err := client.doRequest(parameters{
182+
err := client.doRequest(ctx, parameters{
157183
Method: "PUT",
158184
Path: path,
159185
TokenType: server_token,
@@ -168,11 +194,16 @@ type bouncedTagsResponse struct {
168194
Tags []string `json:"tags"`
169195
}
170196

171-
// GetBouncedTags retrieves a list of tags that have generated bounced emails
197+
// GetBouncedTags calls GetBouncedTagsWithContext with empty context
172198
func (client *Client) GetBouncedTags() ([]string, error) {
199+
return client.GetBouncedTagsWithContext(context.TODO())
200+
}
201+
202+
// GetBouncedTagsWithContext retrieves a list of tags that have generated bounced emails
203+
func (client *Client) GetBouncedTagsWithContext(ctx context.Context) ([]string, error) {
173204
var raw json.RawMessage
174205
path := "bounces/tags"
175-
err := client.doRequest(parameters{
206+
err := client.doRequest(ctx, parameters{
176207
Method: "GET",
177208
Path: path,
178209
TokenType: server_token,

email.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postmark
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67
)
@@ -70,10 +71,15 @@ type EmailResponse struct {
7071
Message string
7172
}
7273

73-
// SendEmail sends, well, an email.
74+
// SendEmail calls SendEmailWithContext with empty context
7475
func (client *Client) SendEmail(email Email) (EmailResponse, error) {
76+
return client.SendEmailWithContext(context.TODO(), email)
77+
}
78+
79+
// SendEmailWithContext sends, well, an email.
80+
func (client *Client) SendEmailWithContext(ctx context.Context, email Email) (EmailResponse, error) {
7581
res := EmailResponse{}
76-
err := client.doRequest(parameters{
82+
err := client.doRequest(ctx, parameters{
7783
Method: "POST",
7884
Path: "email",
7985
Payload: email,
@@ -87,12 +93,17 @@ func (client *Client) SendEmail(email Email) (EmailResponse, error) {
8793
return res, err
8894
}
8995

90-
// SendEmailBatch sends multiple emails together
96+
// SendEmailBatch calls SendEmailBatchWithContext with empty context
97+
func (client *Client) SendEmailBatch(emails []Email) ([]EmailResponse, error) {
98+
return client.SendEmailBatchWithContext(context.TODO(), emails)
99+
}
100+
101+
// SendEmailBatchWithContxt sends multiple emails together
91102
// Note, individual emails in the batch can error, so it would be wise to
92103
// range over the responses and sniff for errors
93-
func (client *Client) SendEmailBatch(emails []Email) ([]EmailResponse, error) {
104+
func (client *Client) SendEmailBatchWithContext(ctx context.Context, emails []Email) ([]EmailResponse, error) {
94105
res := []EmailResponse{}
95-
err := client.doRequest(parameters{
106+
err := client.doRequest(ctx, parameters{
96107
Method: "POST",
97108
Path: "email/batch",
98109
Payload: emails,

messages_inbound.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postmark
22

33
import (
4+
"context"
45
"fmt"
56
"net/url"
67
"time"
@@ -59,10 +60,15 @@ func (x InboundMessage) Time() (time.Time, error) {
5960
///////////////////////////////////////
6061
///////////////////////////////////////
6162

62-
// GetInboundMessage fetches a specific inbound message via serverID
63+
// GetInboundMessage calls GetInboundMessageWithContext with empty context
6364
func (client *Client) GetInboundMessage(messageID string) (InboundMessage, error) {
65+
return client.GetInboundMessageWithContext(context.TODO(), messageID)
66+
}
67+
68+
// GetInboundMessageWithContext fetches a specific inbound message via serverID
69+
func (client *Client) GetInboundMessageWithContext(ctx context.Context, messageID string) (InboundMessage, error) {
6470
res := InboundMessage{}
65-
err := client.doRequest(parameters{
71+
err := client.doRequest(ctx, parameters{
6672
Method: "GET",
6773
Path: fmt.Sprintf("messages/inbound/%s/details", messageID),
6874
TokenType: server_token,
@@ -78,10 +84,15 @@ type inboundMessagesResponse struct {
7884
Messages []InboundMessage
7985
}
8086

81-
// GetInboundMessages fetches a list of inbound message on the server
87+
// GetInboundMessages calls GetInboundMessagesWithContext with empty context
88+
func (client *Client) GetInboundMessages(count int64, offset int64, options map[string]interface{}) ([]InboundMessage, int64, error) {
89+
return client.GetInboundMessagesWithContext(context.TODO(), count, offset, options)
90+
}
91+
92+
// GetInboundMessagesWithContext fetches a list of inbound message on the server
8293
// It returns a InboundMessage slice, the total message count, and any error that occurred
8394
// http://developer.postmarkapp.com/developer-api-messages.html#inbound-message-search
84-
func (client *Client) GetInboundMessages(count int64, offset int64, options map[string]interface{}) ([]InboundMessage, int64, error) {
95+
func (client *Client) GetInboundMessagesWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]InboundMessage, int64, error) {
8596
res := inboundMessagesResponse{}
8697

8798
values := &url.Values{}
@@ -92,7 +103,7 @@ func (client *Client) GetInboundMessages(count int64, offset int64, options map[
92103
values.Add(k, fmt.Sprintf("%v", v))
93104
}
94105

95-
err := client.doRequest(parameters{
106+
err := client.doRequest(ctx, parameters{
96107
Method: "GET",
97108
Path: fmt.Sprintf("messages/inbound?%s", values.Encode()),
98109
TokenType: server_token,
@@ -104,10 +115,15 @@ func (client *Client) GetInboundMessages(count int64, offset int64, options map[
104115
///////////////////////////////////////
105116
///////////////////////////////////////
106117

107-
// BypassInboundMessage - Bypass rules for a blocked inbound message
118+
// BypassInboundMessage calls BypassInboundMessageWithContext with empty context
108119
func (client *Client) BypassInboundMessage(messageID string) error {
120+
return client.BypassInboundMessageWithContext(context.TODO(), messageID)
121+
}
122+
123+
// BypassInboundMessageWithContext bypasses rules for a blocked inbound message
124+
func (client *Client) BypassInboundMessageWithContext(ctx context.Context, messageID string) error {
109125
res := APIError{}
110-
err := client.doRequest(parameters{
126+
err := client.doRequest(ctx, parameters{
111127
Method: "PUT",
112128
Path: fmt.Sprintf("messages/inbound/%s/bypass", messageID),
113129
TokenType: server_token,
@@ -123,10 +139,15 @@ func (client *Client) BypassInboundMessage(messageID string) error {
123139
///////////////////////////////////////
124140
///////////////////////////////////////
125141

126-
// RetryInboundMessage - Retry a failed inbound message for processing
142+
// RetryInboundMessage calls RetryInboundMessageWithContext with empty context
127143
func (client *Client) RetryInboundMessage(messageID string) error {
144+
return client.RetryInboundMessageWithContext(context.TODO(), messageID)
145+
}
146+
147+
// RetryInboundMessageWithContext retries a failed inbound message for processing
148+
func (client *Client) RetryInboundMessageWithContext(ctx context.Context, messageID string) error {
128149
res := APIError{}
129-
err := client.doRequest(parameters{
150+
err := client.doRequest(ctx, parameters{
130151
Method: "PUT",
131152
Path: fmt.Sprintf("messages/inbound/%s/retry", messageID),
132153
TokenType: server_token,

messages_outbound.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postmark
22

33
import (
4+
"context"
45
"fmt"
56
"net/url"
67
"time"
@@ -64,10 +65,15 @@ type MessageEvent struct {
6465
///////////////////////////////////////
6566
///////////////////////////////////////
6667

67-
// GetOutboundMessage fetches a specific outbound message via serverID
68+
// GetOutboundMessage calls GetOutboundMessageWithContext with empty context
6869
func (client *Client) GetOutboundMessage(messageID string) (OutboundMessage, error) {
70+
return client.GetOutboundMessageWithContext(context.TODO(), messageID)
71+
}
72+
73+
// GetOutboundMessageWithContext fetches a specific outbound message via serverID
74+
func (client *Client) GetOutboundMessageWithContext(ctx context.Context, messageID string) (OutboundMessage, error) {
6975
res := OutboundMessage{}
70-
err := client.doRequest(parameters{
76+
err := client.doRequest(ctx, parameters{
7177
Method: "GET",
7278
Path: fmt.Sprintf("messages/outbound/%s/details", messageID),
7379
TokenType: server_token,
@@ -78,10 +84,15 @@ func (client *Client) GetOutboundMessage(messageID string) (OutboundMessage, err
7884
///////////////////////////////////////
7985
///////////////////////////////////////
8086

81-
// GetOutboundMessageDump fetches the raw source of message. If no dump is available this will return an empty string.
87+
// GetOutboundMessageDump calls GetOutboundMessageDumpWithContext with empty context
8288
func (client *Client) GetOutboundMessageDump(messageID string) (string, error) {
89+
return client.GetOutboundMessageDumpWithContext(context.TODO(), messageID)
90+
}
91+
92+
// GetOutboundMessageDumpWithContext fetches the raw source of message. If no dump is available this will return an empty string.
93+
func (client *Client) GetOutboundMessageDumpWithContext(ctx context.Context, messageID string) (string, error) {
8394
res := dumpResponse{}
84-
err := client.doRequest(parameters{
95+
err := client.doRequest(ctx, parameters{
8596
Method: "GET",
8697
Path: fmt.Sprintf("messages/outbound/%s/dump", messageID),
8798
TokenType: server_token,
@@ -97,11 +108,16 @@ type outboundMessagesResponse struct {
97108
Messages []OutboundMessage
98109
}
99110

100-
// GetOutboundMessages fetches a list of outbound message on the server
111+
// GetOutboundMessages calls GetOutboundMessagesWithContext with empty context
112+
func (client *Client) GetOutboundMessages(count int64, offset int64, options map[string]interface{}) ([]OutboundMessage, int64, error) {
113+
return client.GetOutboundMessagesWithContext(context.TODO(), count, offset, options)
114+
}
115+
116+
// GetOutboundMessagesWithContext fetches a list of outbound message on the server
101117
// It returns a OutboundMessage slice, the total message count, and any error that occurred
102118
// Note: that a single open is bound to a single recipient, so if the same message was sent to two recipients and both of them opened it, that will be represented by two entries in this array.
103119
// Available options: http://developer.postmarkapp.com/developer-api-messages.html#outbound-message-search
104-
func (client *Client) GetOutboundMessages(count int64, offset int64, options map[string]interface{}) ([]OutboundMessage, int64, error) {
120+
func (client *Client) GetOutboundMessagesWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]OutboundMessage, int64, error) {
105121
res := outboundMessagesResponse{}
106122

107123
values := &url.Values{}
@@ -112,7 +128,7 @@ func (client *Client) GetOutboundMessages(count int64, offset int64, options map
112128
values.Add(k, fmt.Sprintf("%v", v))
113129
}
114130

115-
err := client.doRequest(parameters{
131+
err := client.doRequest(ctx, parameters{
116132
Method: "GET",
117133
Path: fmt.Sprintf("messages/outbound?%s", values.Encode()),
118134
TokenType: server_token,
@@ -148,11 +164,16 @@ type outboundMessageOpensResponse struct {
148164
Opens []Open
149165
}
150166

167+
// GetOutboundMessagesOpens calls GetOutboundMessagesOpensWithContext with empty context
168+
func (client *Client) GetOutboundMessagesOpens(count int64, offset int64, options map[string]interface{}) ([]Open, int64, error) {
169+
return client.GetOutboundMessagesOpensWithContext(context.TODO(), count, offset, options)
170+
}
171+
151172
// GetOutboundMessagesOpens fetches a list of opens on the server
152173
// It returns a Open slice, the total opens count, and any error that occurred
153174
// To get opens for a specific message, use GetOutboundMessageOpens()
154175
// Available options: http://developer.postmarkapp.com/developer-api-messages.html#message-opens
155-
func (client *Client) GetOutboundMessagesOpens(count int64, offset int64, options map[string]interface{}) ([]Open, int64, error) {
176+
func (client *Client) GetOutboundMessagesOpensWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]Open, int64, error) {
156177
res := outboundMessageOpensResponse{}
157178

158179
values := &url.Values{}
@@ -163,7 +184,7 @@ func (client *Client) GetOutboundMessagesOpens(count int64, offset int64, option
163184
values.Add(k, fmt.Sprintf("%v", v))
164185
}
165186

166-
err := client.doRequest(parameters{
187+
err := client.doRequest(ctx, parameters{
167188
Method: "GET",
168189
Path: fmt.Sprintf("messages/outbound/opens?%s", values.Encode()),
169190
TokenType: server_token,
@@ -174,16 +195,21 @@ func (client *Client) GetOutboundMessagesOpens(count int64, offset int64, option
174195
///////////////////////////////////////
175196
///////////////////////////////////////
176197

177-
// GetOutboundMessageOpens fetches a list of opens for a specific message
178-
// It returns a Open slice, the total opens count, and any error that occurred
198+
// GetOutboundMessageOpens calls GetOutboundMessageOpensWithContext with empty context
179199
func (client *Client) GetOutboundMessageOpens(messageID string, count int64, offset int64) ([]Open, int64, error) {
200+
return client.GetOutboundMessageOpensWithContext(context.TODO(), messageID, count, offset)
201+
}
202+
203+
// GetOutboundMessageOpensWithContext fetches a list of opens for a specific message
204+
// It returns a Open slice, the total opens count, and any error that occurred
205+
func (client *Client) GetOutboundMessageOpensWithContext(ctx context.Context, messageID string, count int64, offset int64) ([]Open, int64, error) {
180206
res := outboundMessageOpensResponse{}
181207

182208
values := &url.Values{}
183209
values.Add("count", fmt.Sprintf("%d", count))
184210
values.Add("offset", fmt.Sprintf("%d", offset))
185211

186-
err := client.doRequest(parameters{
212+
err := client.doRequest(ctx, parameters{
187213
Method: "GET",
188214
Path: fmt.Sprintf("messages/outbound/opens/%s?%s", messageID, values.Encode()),
189215
TokenType: server_token,

0 commit comments

Comments
 (0)