forked from keighl/postmark
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef255e2
commit 3ab5c12
Showing
3 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package postmark | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type MessageStreamType string | ||
type MessageStreamUnsubscribeHandlingType string | ||
|
||
const ( | ||
InboundMessageStreamType MessageStreamType = "Inbound" | ||
BroadcastMessageStreamType MessageStreamType = "Broadcast" | ||
TransactionalMessageStreamType MessageStreamType = "Transactional" | ||
|
||
NoneUnsubscribeHandlingType MessageStreamUnsubscribeHandlingType = "none" | ||
PostmarkUnsubscribeHandlingType MessageStreamUnsubscribeHandlingType = "Postmark" | ||
CustomUnsubscribeHandlingType MessageStreamUnsubscribeHandlingType = "Custom" | ||
) | ||
|
||
type MessageStreamSubscriptionManagementConfiguration struct { | ||
// The unsubscribe management option used for the Stream. Broadcast Message | ||
// Streams require unsubscribe management, Postmark is default. For Inbound | ||
// and Transactional Streams default is none. | ||
UnsubscribeHandlingType MessageStreamUnsubscribeHandlingType `json:"UnsubscribeHandlingType"` | ||
} | ||
|
||
type MessageStream struct { | ||
// ID of message stream. | ||
ID string `json:"ID"` | ||
// ID of server the message stream is associated with. | ||
ServerID int `json:"ServerID"` | ||
// Name of message stream. | ||
Name string `json:"Name"` | ||
// Description of message stream. This value can be null. | ||
Description *string `json:"Description,omitempty"` | ||
// Type of message stream. | ||
MessageStreamType MessageStreamType `json:"MessageStreamType"` | ||
// Timestamp when message stream was created. | ||
CreatedAt string `json:"CreatedAt"` | ||
// Timestamp when message stream was last updated. This value can be null. | ||
UpdatedAt *string `json:"UpdatedAt,omitempty"` | ||
// Timestamp when message stream was archived. This value can be null. | ||
ArchivedAt *string `json:"ArchivedAt,omitempty"` | ||
// Archived streams are deleted 45 days after archiving date. Until this | ||
// date, it can be restored. This value is null if the stream is not | ||
// archived. | ||
ExpectedPurgeDate *string `json:"ExpectedPurgeDate,omitempty"` | ||
// Subscription management options for the Stream | ||
SubscriptionManagementConfiguration MessageStreamSubscriptionManagementConfiguration `json:"SubscriptionManagementConfiguration"` | ||
} | ||
|
||
// ListMessageStreams returns all message streams for a server. | ||
// messageStreamType must be one of "All", "Inbound", "Transactional", | ||
// "Broadcasts" and defaults to "All". | ||
func (client *Client) ListMessageStreams(ctx context.Context, messageStreamType string, includeArchived bool) ([]MessageStream, error) { | ||
switch messageStreamType { | ||
case "Inbound", "Transactional", "Broadcasts": | ||
break | ||
default: | ||
messageStreamType = "All" | ||
} | ||
|
||
var res struct { | ||
MessageStreams []MessageStream | ||
} | ||
|
||
err := client.doRequest(ctx, parameters{ | ||
Method: http.MethodGet, | ||
Path: fmt.Sprintf("message-streams?MessageStreamType=%s&IncludeArchivedStreams=%t", messageStreamType, includeArchived), | ||
TokenType: serverToken, | ||
}, &res) | ||
|
||
return res.MessageStreams, err | ||
} | ||
|
||
// GetMessageStream retrieves a specific message stream by the message stream's ID. | ||
func (client *Client) GetMessageStream(ctx context.Context, id string) (MessageStream, error) { | ||
var res MessageStream | ||
err := client.doRequest(ctx, parameters{ | ||
Method: http.MethodGet, | ||
Path: fmt.Sprintf("message-streams/%s", id), | ||
TokenType: serverToken, | ||
}, &res) | ||
return res, err | ||
} | ||
|
||
// EditMessageStreamRequest is the request body for EditMessageStream. It | ||
// contains only a subset of the fields of MessageStream. | ||
type EditMessageStreamRequest struct { | ||
// Name of message stream. | ||
Name string `json:"Name"` | ||
// Description of message stream. This value can be null. | ||
Description *string `json:"Description,omitempty"` | ||
// Subscription management options for the Stream | ||
SubscriptionManagementConfiguration MessageStreamSubscriptionManagementConfiguration `json:"SubscriptionManagementConfiguration"` | ||
} | ||
|
||
// EditMessageStream updates a message stream. | ||
func (client *Client) EditMessageStream(ctx context.Context, id string, req EditMessageStreamRequest) (MessageStream, error) { | ||
var res MessageStream | ||
err := client.doRequest(ctx, parameters{ | ||
Method: http.MethodPatch, | ||
Path: fmt.Sprintf("message-streams/%s", id), | ||
TokenType: serverToken, | ||
Payload: req, | ||
}, &res) | ||
return res, err | ||
} | ||
|
||
// CreateMessageStreamRequest is the request body for CreateMessageStream. It | ||
// contains only a subset of the fields of MessageStream. | ||
type CreateMessageStreamRequest struct { | ||
// ID of message stream. | ||
ID string `json:"ID"` | ||
// Name of message stream. | ||
Name string `json:"Name"` | ||
// Description of message stream. This value can be null. | ||
Description *string `json:"Description,omitempty"` | ||
// Type of message stream. | ||
MessageStreamType MessageStreamType `json:"MessageStreamType"` | ||
// Subscription management options for the Stream | ||
SubscriptionManagementConfiguration MessageStreamSubscriptionManagementConfiguration `json:"SubscriptionManagementConfiguration"` | ||
} | ||
|
||
// CreateMessageStream makes a new message stream. It will be created on the | ||
// server of the token used by this Client. | ||
func (client *Client) CreateMessageStream(ctx context.Context, req CreateMessageStreamRequest) (MessageStream, error) { | ||
var res MessageStream | ||
err := client.doRequest(ctx, parameters{ | ||
Method: http.MethodPost, | ||
Path: "message-streams", | ||
TokenType: serverToken, | ||
Payload: req, | ||
}, &res) | ||
return res, err | ||
} | ||
|
||
// ArchiveMessageStreamResponse is the response body for ArchiveMessageStream. | ||
type ArchiveMessageStreamResponse struct { | ||
// ID of message stream. | ||
ID string `json:"ID"` | ||
// Server ID of message stream. | ||
ServerID int `json:"ServerID"` | ||
// Expected purge date of message stream. Stream is deleted 45 days after | ||
// archiving date. Until this date, it can be restored. | ||
ExpectedPurgeDate string `json:"ExpectedPurgeDate"` | ||
} | ||
|
||
// ArchiveMessageStream archives a message stream. Archived streams are deleted | ||
// after 45 days, but they can be restored until that point. | ||
func (client *Client) ArchiveMessageStream(ctx context.Context, id string) (ArchiveMessageStreamResponse, error) { | ||
var res ArchiveMessageStreamResponse | ||
err := client.doRequest(ctx, parameters{ | ||
Method: http.MethodPost, | ||
Path: fmt.Sprintf("message-streams/%s/archive", id), | ||
TokenType: serverToken, | ||
}, &res) | ||
return res, err | ||
} | ||
|
||
// UnarchiveMessageStream unarchives a message stream if it has not been deleted yet. | ||
// The ArchivedAt value will be null after calling this method. | ||
func (client *Client) UnarchiveMessageStream(ctx context.Context, id string) (MessageStream, error) { | ||
var res MessageStream | ||
err := client.doRequest(ctx, parameters{ | ||
Method: http.MethodPost, | ||
Path: fmt.Sprintf("message-streams/%s/unarchive", id), | ||
TokenType: serverToken, | ||
}, &res) | ||
return res, err | ||
} |
Oops, something went wrong.