Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass context when calling Postmark API #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ _testmain.go
*.exe
*.test
*.prof

.idea
59 changes: 45 additions & 14 deletions bounce.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postmark

import (
"context"
"encoding/json"
"fmt"
"net/url"
Expand Down Expand Up @@ -29,11 +30,16 @@ type DeliveryStats struct {
Bounces []BounceType
}

// GetDeliveryStats returns delivery stats for the server
// GetDeliveryStats calls GetDeliveryStatsWithContext with empty context
func (client *Client) GetDeliveryStats() (DeliveryStats, error) {
return client.GetDeliveryStatsWithContext(context.Background())
}

// GetDeliveryStatsWithContext returns delivery stats for the server
func (client *Client) GetDeliveryStatsWithContext(ctx context.Context) (DeliveryStats, error) {
res := DeliveryStats{}
path := "deliverystats"
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: path,
TokenType: server_token,
Expand Down Expand Up @@ -81,10 +87,15 @@ type bouncesResponse struct {
Bounces []Bounce
}

// GetBounces returns bounces for the server
// GetBounces calls GetBouncesWithContext with empty context
func (client *Client) GetBounces(count int64, offset int64, options map[string]interface{}) ([]Bounce, int64, error) {
return client.GetBouncesWithContext(context.Background(), count, offset, options)
}

// GetBouncesWithContext returns bounces for the server
// It returns a Bounce slice, the total bounce count, and any error that occurred
// Available options: http://developer.postmarkapp.com/developer-api-bounce.html#bounces
func (client *Client) GetBounces(count int64, offset int64, options map[string]interface{}) ([]Bounce, int64, error) {
func (client *Client) GetBouncesWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]Bounce, int64, error) {
res := bouncesResponse{}

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

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

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

// GetBounce fetches a single bounce with bounceID
// GetBounce calls GetBounceWithContext with empty context
func (client *Client) GetBounce(bounceID int64) (Bounce, error) {
return client.GetBounceWithContext(context.Background(), bounceID)
}

// GetBounceWithContext fetches a single bounce with bounceID
func (client *Client) GetBounceWithContext(ctx context.Context, bounceID int64) (Bounce, error) {
res := Bounce{}
path := fmt.Sprintf("bounces/%v", bounceID)
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: path,
TokenType: server_token,
Expand All @@ -127,11 +143,16 @@ type dumpResponse struct {
Body string
}

// GetBounceDump fetches a SMTP data dump for a single bounce
// GetBounceDump calls GetBounceDumpWithContext with empty context
func (client *Client) GetBounceDump(bounceID int64) (string, error) {
return client.GetBounceDumpWithContext(context.Background(), bounceID)
}

// GetBounceDumpWithContext fetches a SMTP data dump for a single bounce
func (client *Client) GetBounceDumpWithContext(ctx context.Context, bounceID int64) (string, error) {
res := dumpResponse{}
path := fmt.Sprintf("bounces/%v/dump", bounceID)
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: path,
TokenType: server_token,
Expand All @@ -147,13 +168,18 @@ type activateBounceResponse struct {
Bounce Bounce
}

// ActivateBounce reactivates a bounce for resending. Returns the bounce, a
// ActivateBounce calls ActivateBounceWithContext with empty context
func (client *Client) ActivateBounce(bounceID int64) (Bounce, string, error) {
return client.ActivateBounceWithContext(context.Background(), bounceID)
}

// ActivateBounceWithContext reactivates a bounce for resending. Returns the bounce, a
// message, and any error that occurs
// TODO: clarify this with Postmark
func (client *Client) ActivateBounce(bounceID int64) (Bounce, string, error) {
func (client *Client) ActivateBounceWithContext(ctx context.Context, bounceID int64) (Bounce, string, error) {
res := activateBounceResponse{}
path := fmt.Sprintf("bounces/%v/activate", bounceID)
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "PUT",
Path: path,
TokenType: server_token,
Expand All @@ -168,11 +194,16 @@ type bouncedTagsResponse struct {
Tags []string `json:"tags"`
}

// GetBouncedTags retrieves a list of tags that have generated bounced emails
// GetBouncedTags calls GetBouncedTagsWithContext with empty context
func (client *Client) GetBouncedTags() ([]string, error) {
return client.GetBouncedTagsWithContext(context.Background())
}

// GetBouncedTagsWithContext retrieves a list of tags that have generated bounced emails
func (client *Client) GetBouncedTagsWithContext(ctx context.Context) ([]string, error) {
var raw json.RawMessage
path := "bounces/tags"
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: path,
TokenType: server_token,
Expand Down
21 changes: 16 additions & 5 deletions email.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postmark

import (
"context"
"fmt"
"time"
)
Expand Down Expand Up @@ -70,10 +71,15 @@ type EmailResponse struct {
Message string
}

// SendEmail sends, well, an email.
// SendEmail calls SendEmailWithContext with empty context
func (client *Client) SendEmail(email Email) (EmailResponse, error) {
return client.SendEmailWithContext(context.Background(), email)
}

// SendEmailWithContext sends, well, an email.
func (client *Client) SendEmailWithContext(ctx context.Context, email Email) (EmailResponse, error) {
res := EmailResponse{}
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "POST",
Path: "email",
Payload: email,
Expand All @@ -87,12 +93,17 @@ func (client *Client) SendEmail(email Email) (EmailResponse, error) {
return res, err
}

// SendEmailBatch sends multiple emails together
// SendEmailBatch calls SendEmailBatchWithContext with empty context
func (client *Client) SendEmailBatch(emails []Email) ([]EmailResponse, error) {
return client.SendEmailBatchWithContext(context.Background(), emails)
}

// SendEmailBatchWithContxt sends multiple emails together
// Note, individual emails in the batch can error, so it would be wise to
// range over the responses and sniff for errors
func (client *Client) SendEmailBatch(emails []Email) ([]EmailResponse, error) {
func (client *Client) SendEmailBatchWithContext(ctx context.Context, emails []Email) ([]EmailResponse, error) {
res := []EmailResponse{}
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "POST",
Path: "email/batch",
Payload: emails,
Expand Down
39 changes: 30 additions & 9 deletions messages_inbound.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postmark

import (
"context"
"fmt"
"net/url"
"time"
Expand Down Expand Up @@ -59,10 +60,15 @@ func (x InboundMessage) Time() (time.Time, error) {
///////////////////////////////////////
///////////////////////////////////////

// GetInboundMessage fetches a specific inbound message via serverID
// GetInboundMessage calls GetInboundMessageWithContext with empty context
func (client *Client) GetInboundMessage(messageID string) (InboundMessage, error) {
return client.GetInboundMessageWithContext(context.Background(), messageID)
}

// GetInboundMessageWithContext fetches a specific inbound message via serverID
func (client *Client) GetInboundMessageWithContext(ctx context.Context, messageID string) (InboundMessage, error) {
res := InboundMessage{}
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: fmt.Sprintf("messages/inbound/%s/details", messageID),
TokenType: server_token,
Expand All @@ -78,10 +84,15 @@ type inboundMessagesResponse struct {
Messages []InboundMessage
}

// GetInboundMessages fetches a list of inbound message on the server
// GetInboundMessages calls GetInboundMessagesWithContext with empty context
func (client *Client) GetInboundMessages(count int64, offset int64, options map[string]interface{}) ([]InboundMessage, int64, error) {
return client.GetInboundMessagesWithContext(context.Background(), count, offset, options)
}

// GetInboundMessagesWithContext fetches a list of inbound message on the server
// It returns a InboundMessage slice, the total message count, and any error that occurred
// http://developer.postmarkapp.com/developer-api-messages.html#inbound-message-search
func (client *Client) GetInboundMessages(count int64, offset int64, options map[string]interface{}) ([]InboundMessage, int64, error) {
func (client *Client) GetInboundMessagesWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]InboundMessage, int64, error) {
res := inboundMessagesResponse{}

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

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

// BypassInboundMessage - Bypass rules for a blocked inbound message
// BypassInboundMessage calls BypassInboundMessageWithContext with empty context
func (client *Client) BypassInboundMessage(messageID string) error {
return client.BypassInboundMessageWithContext(context.Background(), messageID)
}

// BypassInboundMessageWithContext bypasses rules for a blocked inbound message
func (client *Client) BypassInboundMessageWithContext(ctx context.Context, messageID string) error {
res := APIError{}
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "PUT",
Path: fmt.Sprintf("messages/inbound/%s/bypass", messageID),
TokenType: server_token,
Expand All @@ -123,10 +139,15 @@ func (client *Client) BypassInboundMessage(messageID string) error {
///////////////////////////////////////
///////////////////////////////////////

// RetryInboundMessage - Retry a failed inbound message for processing
// RetryInboundMessage calls RetryInboundMessageWithContext with empty context
func (client *Client) RetryInboundMessage(messageID string) error {
return client.RetryInboundMessageWithContext(context.Background(), messageID)
}

// RetryInboundMessageWithContext retries a failed inbound message for processing
func (client *Client) RetryInboundMessageWithContext(ctx context.Context, messageID string) error {
res := APIError{}
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "PUT",
Path: fmt.Sprintf("messages/inbound/%s/retry", messageID),
TokenType: server_token,
Expand Down
50 changes: 38 additions & 12 deletions messages_outbound.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postmark

import (
"context"
"fmt"
"net/url"
"time"
Expand Down Expand Up @@ -64,10 +65,15 @@ type MessageEvent struct {
///////////////////////////////////////
///////////////////////////////////////

// GetOutboundMessage fetches a specific outbound message via serverID
// GetOutboundMessage calls GetOutboundMessageWithContext with empty context
func (client *Client) GetOutboundMessage(messageID string) (OutboundMessage, error) {
return client.GetOutboundMessageWithContext(context.Background(), messageID)
}

// GetOutboundMessageWithContext fetches a specific outbound message via serverID
func (client *Client) GetOutboundMessageWithContext(ctx context.Context, messageID string) (OutboundMessage, error) {
res := OutboundMessage{}
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: fmt.Sprintf("messages/outbound/%s/details", messageID),
TokenType: server_token,
Expand All @@ -78,10 +84,15 @@ func (client *Client) GetOutboundMessage(messageID string) (OutboundMessage, err
///////////////////////////////////////
///////////////////////////////////////

// GetOutboundMessageDump fetches the raw source of message. If no dump is available this will return an empty string.
// GetOutboundMessageDump calls GetOutboundMessageDumpWithContext with empty context
func (client *Client) GetOutboundMessageDump(messageID string) (string, error) {
return client.GetOutboundMessageDumpWithContext(context.Background(), messageID)
}

// GetOutboundMessageDumpWithContext fetches the raw source of message. If no dump is available this will return an empty string.
func (client *Client) GetOutboundMessageDumpWithContext(ctx context.Context, messageID string) (string, error) {
res := dumpResponse{}
err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: fmt.Sprintf("messages/outbound/%s/dump", messageID),
TokenType: server_token,
Expand All @@ -97,11 +108,16 @@ type outboundMessagesResponse struct {
Messages []OutboundMessage
}

// GetOutboundMessages fetches a list of outbound message on the server
// GetOutboundMessages calls GetOutboundMessagesWithContext with empty context
func (client *Client) GetOutboundMessages(count int64, offset int64, options map[string]interface{}) ([]OutboundMessage, int64, error) {
return client.GetOutboundMessagesWithContext(context.Background(), count, offset, options)
}

// GetOutboundMessagesWithContext fetches a list of outbound message on the server
// It returns a OutboundMessage slice, the total message count, and any error that occurred
// 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.
// Available options: http://developer.postmarkapp.com/developer-api-messages.html#outbound-message-search
func (client *Client) GetOutboundMessages(count int64, offset int64, options map[string]interface{}) ([]OutboundMessage, int64, error) {
func (client *Client) GetOutboundMessagesWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]OutboundMessage, int64, error) {
res := outboundMessagesResponse{}

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

err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: fmt.Sprintf("messages/outbound?%s", values.Encode()),
TokenType: server_token,
Expand Down Expand Up @@ -148,11 +164,16 @@ type outboundMessageOpensResponse struct {
Opens []Open
}

// GetOutboundMessagesOpens calls GetOutboundMessagesOpensWithContext with empty context
func (client *Client) GetOutboundMessagesOpens(count int64, offset int64, options map[string]interface{}) ([]Open, int64, error) {
return client.GetOutboundMessagesOpensWithContext(context.Background(), count, offset, options)
}

// GetOutboundMessagesOpens fetches a list of opens on the server
// It returns a Open slice, the total opens count, and any error that occurred
// To get opens for a specific message, use GetOutboundMessageOpens()
// Available options: http://developer.postmarkapp.com/developer-api-messages.html#message-opens
func (client *Client) GetOutboundMessagesOpens(count int64, offset int64, options map[string]interface{}) ([]Open, int64, error) {
func (client *Client) GetOutboundMessagesOpensWithContext(ctx context.Context, count int64, offset int64, options map[string]interface{}) ([]Open, int64, error) {
res := outboundMessageOpensResponse{}

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

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

// GetOutboundMessageOpens fetches a list of opens for a specific message
// It returns a Open slice, the total opens count, and any error that occurred
// GetOutboundMessageOpens calls GetOutboundMessageOpensWithContext with empty context
func (client *Client) GetOutboundMessageOpens(messageID string, count int64, offset int64) ([]Open, int64, error) {
return client.GetOutboundMessageOpensWithContext(context.Background(), messageID, count, offset)
}

// GetOutboundMessageOpensWithContext fetches a list of opens for a specific message
// It returns a Open slice, the total opens count, and any error that occurred
func (client *Client) GetOutboundMessageOpensWithContext(ctx context.Context, messageID string, count int64, offset int64) ([]Open, int64, error) {
res := outboundMessageOpensResponse{}

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

err := client.doRequest(parameters{
err := client.doRequest(ctx, parameters{
Method: "GET",
Path: fmt.Sprintf("messages/outbound/opens/%s?%s", messageID, values.Encode()),
TokenType: server_token,
Expand Down
Loading