forked from mrz1836/postmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_streams.go
190 lines (171 loc) · 7.51 KB
/
message_streams.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
package postmark
import (
"context"
"fmt"
"net/http"
)
// MessageStreamType is an Enum representing the type of a message stream.
type MessageStreamType string
// MessageStreamUnsubscribeHandlingType is an Enum with the possible values for
// the unsubscribe handling in a message stream.
type MessageStreamUnsubscribeHandlingType string
const (
// InboundMessageStreamType indicates a message stream is for inbound messages.
InboundMessageStreamType MessageStreamType = "Inbound"
// BroadcastMessageStreamType indicates a message stream is for broadcast messages.
BroadcastMessageStreamType MessageStreamType = "Broadcasts"
// TransactionalMessageStreamType indicates a message stream is for transactional messages.
TransactionalMessageStreamType MessageStreamType = "Transactional"
// NoneUnsubscribeHandlingType indicates a message stream unsubscribe
// handling will be performed by the user.
NoneUnsubscribeHandlingType MessageStreamUnsubscribeHandlingType = "None"
// PostmarkUnsubscribeHandlingType indicates a message stream unsubscribe
// handling will be performed by postmark.
PostmarkUnsubscribeHandlingType MessageStreamUnsubscribeHandlingType = "Postmark"
// CustomUnsubscribeHandlingType indicates a message stream unsubscribe
// handling is custom.
CustomUnsubscribeHandlingType MessageStreamUnsubscribeHandlingType = "Custom"
)
// MessageStreamSubscriptionManagementConfiguration is the configuration for
// subscriptions to the message stream.
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"`
}
// MessageStream holes the configuration for a message stream on a server.
// https://postmarkapp.com/developer/api/message-streams-api
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
}