-
Notifications
You must be signed in to change notification settings - Fork 0
/
postage_app.go
342 lines (278 loc) · 8.24 KB
/
postage_app.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package postage_app
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
const (
Url = "https://api.postageapp.com/v.1.0/"
)
type Client struct {
ApiKey string
BaseUrl string
}
type Attachment struct {
FileName string
ContentType string
ContentBytes []byte
}
type Recipient struct {
Email string
Variables map[string]string
}
type Message struct {
Uid string
Template string
Attachments []*Attachment
Recipients []*Recipient
Variables map[string]string
Headers map[string]string
RecipientOverride string
Subject string
From string
ReplyTo string
Text string
Html string
}
type MessageInfo struct {
ProjectId int
Template string
TotalTransmissionsCount int
FailedTransmissionsCount int
CompletedTransmissionsCount int
CreatedAt time.Time
WillPurgeAt time.Time
}
type MessageReceipt struct {
Id int
Url string
}
type MessageTransmission struct {
Status string
ResultCode string
ResultMessage string
CreatedAt time.Time
FailedAt time.Time
OpenedAt time.Time
}
type MessageTransmissions struct {
Id int
Transmissions map[string]*MessageTransmission
}
type TransmissionsStatistic struct {
TodayCount int
ThisMonthCount int
OverallCount int
}
type ProjectInfo struct {
Name string
Url string
Transmissions *TransmissionsStatistic
Users map[string]string
}
type AccountInfo struct {
Name string
Url string
Transmissions *TransmissionsStatistic
Users map[string]string
}
type MetricStatistic struct {
CurrentPercent float64
PreviousPercent float64
DiffPercent float64
CurrentValue int
PreviousValue int
}
type Metric struct {
Delivered *MetricStatistic
Opened *MetricStatistic
Failed *MetricStatistic
Rejected *MetricStatistic
Created *MetricStatistic
Queued *MetricStatistic
Clicked *MetricStatistic
Spammed *MetricStatistic
}
type Metrics struct {
Hour *Metric
Date *Metric
Week *Metric
Month *Metric
}
type Response struct {
Status string
Uid string
Message string
}
type MessagesResponse struct {
Response *Response
Data map[string]*MessageInfo
}
type ProjectResponse struct {
Response *Response
Data *ProjectInfo
}
type AccountResponse struct {
Response *Response
Data *AccountInfo
}
type MetricsResponse struct {
Response *Response
Data *Metrics
}
type MessageResponse struct {
Response *Response
Data *MessageReceipt
}
type MessageTransmissionsResponse struct {
Response *Response
Data *MessageTransmissions
}
type PostageError struct {
Message string
InnerError error
}
type PostageResponseError PostageError
type ResponseParseError PostageError
func (e *PostageError) Error() string {
return e.Message
}
func (e *ResponseParseError) Error() string {
return e.Message
}
func (e *PostageResponseError) Error() string {
return e.Message
}
func (client *Client) post(path string, params string) (map[string]interface{}, error) {
b := bytes.NewBufferString(params)
return client.postBuffer(path, b)
}
func (client *Client) postBuffer(path string, b *bytes.Buffer) (map[string]interface{}, error) {
if client.BaseUrl == "" {
client.BaseUrl = Url
}
url := Url + path
response, err := http.Post(url, "application/json", b)
if err != nil {
return nil, &PostageResponseError{err.Error(), err}
}
bs, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var f interface{}
parseError := json.Unmarshal(bs, &f)
if parseError != nil {
return nil, &PostageResponseError{err.Error(), err}
}
return f.(map[string]interface{}), nil
}
func (client *Client) SendMessage(message *Message) (*MessageResponse, error) {
bts, _ := client.MarshalMessage(message)
b := bytes.NewBuffer(bts)
m, err := client.postBuffer("send_message.json", b)
if err != nil {
return nil, err
}
messageResponse := new(MessageResponse)
messageResponse.Response = client.ParseResponse(m["response"].(map[string]interface{}))
if messageResponse.Response.Status == "ok" {
data := m["data"].(map[string]interface{})
message := data["message"].(map[string]interface{})
messageReceipt := new(MessageReceipt)
messageReceipt.Id = int(message["id"].(float64))
messageReceipt.Url = message["url"].(string)
messageResponse.Data = messageReceipt
} else {
return nil, &ResponseParseError{messageResponse.Response.Status, nil}
}
return messageResponse, nil
}
func (client *Client) GetMessages() (*MessagesResponse, error) {
m, err := client.post("get_messages.json", fmt.Sprintf(`{"api_key":"%s", "uid":"%s"}`, client.ApiKey))
if err != nil {
return nil, err
}
messagesResponse := new(MessagesResponse)
messagesResponse.Response = client.ParseResponse(m["response"].(map[string]interface{}))
if messagesResponse.Response.Status == "ok" {
messagesResponse.Data = client.ParseMessages(m["data"].(map[string]interface{}))
} else {
return nil, &ResponseParseError{messagesResponse.Response.Status, nil}
}
return messagesResponse, nil
}
func (client *Client) GetProjectInfo() (*ProjectResponse, error) {
m, err := client.post("get_project_info.json", fmt.Sprintf(`{"api_key":"%s", "uid":"%s"}`, client.ApiKey))
if err != nil {
return nil, err
}
projectResponse := new(ProjectResponse)
projectResponse.Response = client.ParseResponse(m["response"].(map[string]interface{}))
if projectResponse.Response.Status == "ok" {
projectResponse.Data = client.ParseProjectInfo(m["data"].(map[string]interface{}))
} else {
return nil, &ResponseParseError{projectResponse.Response.Status, nil}
}
return projectResponse, nil
}
func (client *Client) GetAccountInfo() (*AccountResponse, error) {
m, err := client.post("get_account_info.json", fmt.Sprintf(`{"api_key":"%s", "uid":"%s"}`, client.ApiKey))
if err != nil {
return nil, err
}
accountResponse := new(AccountResponse)
accountResponse.Response = client.ParseResponse(m["response"].(map[string]interface{}))
if accountResponse.Response.Status == "ok" {
accountResponse.Data = client.ParseAccountInfo(m["data"].(map[string]interface{}))
} else {
return nil, &ResponseParseError{accountResponse.Response.Status, nil}
}
return accountResponse, nil
}
func (client *Client) GetMetrics() (*MetricsResponse, error) {
m, err := client.post("get_metrics.json", fmt.Sprintf(`{"api_key":"%s", "uid":"%s"}`, client.ApiKey))
if err != nil {
return nil, err
}
metricsResponse := new(MetricsResponse)
metricsResponse.Response = client.ParseResponse(m["response"].(map[string]interface{}))
if metricsResponse.Response.Status == "ok" {
metricsResponse.Data = client.ParseMetrics(m["data"].(map[string]interface{}))
} else {
return nil, &ResponseParseError{metricsResponse.Response.Status, nil}
}
return metricsResponse, nil
}
func (client *Client) GetMessageReceipt(uid string) (*MessageResponse, error) {
m, err := client.post("get_message_receipt.json", fmt.Sprintf(`{"api_key":"%s", "uid":"%s"}`, client.ApiKey, uid))
if err != nil {
return nil, err
}
messageReceiptResponse := new(MessageResponse)
messageReceiptResponse.Response = client.ParseResponse(m["response"].(map[string]interface{}))
if messageReceiptResponse.Response.Status == "ok" {
messageReceiptResponse.Data = client.ParseMessageReceipt(m["data"].(map[string]interface{}))
} else {
return nil, &ResponseParseError{messageReceiptResponse.Response.Status, nil}
}
return messageReceiptResponse, nil
}
func (client *Client) GetMessageTransmissions(uid string) (*MessageTransmissionsResponse, error) {
m, err := client.post("get_message_transmissions.json", fmt.Sprintf(`{"api_key":"%s", "uid":"%s"}`, client.ApiKey, uid))
if err != nil {
return nil, err
}
messageTransmissionsResponse := new(MessageTransmissionsResponse)
messageTransmissionsResponse.Response = client.ParseResponse(m["response"].(map[string]interface{}))
if messageTransmissionsResponse.Response.Status == "ok" {
messageTransmissionsResponse.Data = client.ParseMessageTransmissions(m["data"].(map[string]interface{}))
} else {
return nil, &ResponseParseError{messageTransmissionsResponse.Response.Status, nil}
}
return messageTransmissionsResponse, nil
}