-
Notifications
You must be signed in to change notification settings - Fork 1
/
batches.go
151 lines (128 loc) · 3.86 KB
/
batches.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
package xesende
import (
"encoding/xml"
"time"
)
// BatchesResponse is a list of returned message batches along with the paging
// information.
type BatchesResponse struct {
Paging
Batches []BatchResponse
}
// BatchResponse is a single sent batch.
type BatchResponse struct {
ID string
URI string
CreatedAt time.Time
BatchSize int
PersistedBatchSize int
Status map[string]int
AccountReference string
CreatedBy string
Name string
}
// Batches returns a list of batches sent by the authenticated user.
func (c *Client) Batches(opts ...Option) (*BatchesResponse, error) {
req, err := c.newRequest("GET", "/v1.1/messagebatches", nil)
if err != nil {
return nil, err
}
for _, opt := range opts {
opt(req)
}
var v messageBatchesResponse
if _, err := c.do(req, &v); err != nil {
return nil, err
}
response := &BatchesResponse{
Paging: Paging{
StartIndex: v.StartIndex,
Count: v.Count,
TotalCount: v.TotalCount,
},
Batches: make([]BatchResponse, len(v.Batches)),
}
for i, batch := range v.Batches {
status := map[string]int{}
for _, s := range batch.Status.List {
if s.Value > 0 {
status[s.XMLName.Local] = s.Value
}
}
response.Batches[i] = BatchResponse{
ID: batch.ID,
URI: batch.URI,
CreatedAt: batch.CreatedAt,
BatchSize: batch.BatchSize,
PersistedBatchSize: batch.PersistedBatchSize,
Status: status,
AccountReference: batch.AccountReference,
CreatedBy: batch.CreatedBy,
Name: batch.Name,
}
}
return response, nil
}
// Batch returns the batch with the given id.
func (c *Client) Batch(id string) (*BatchResponse, error) {
req, err := c.newRequest("GET", "/v1.1/messagebatches/"+id, nil)
if err != nil {
return nil, err
}
var v messageBatchResponse
if _, err = c.do(req, &v); err != nil {
return nil, err
}
status := map[string]int{}
for _, s := range v.Status.List {
if s.Value > 0 {
status[s.XMLName.Local] = s.Value
}
}
response := &BatchResponse{
ID: v.ID,
URI: v.URI,
CreatedAt: v.CreatedAt,
BatchSize: v.BatchSize,
PersistedBatchSize: v.PersistedBatchSize,
Status: status,
AccountReference: v.AccountReference,
CreatedBy: v.CreatedBy,
Name: v.Name,
}
return response, nil
}
// CancelBatch prevents the messagebatch from being sent if it is scheduled and
// due to be sent at a point that allows it to be cancelled.
func (c *Client) CancelBatch(id string) error {
req, err := c.newRequest("DELETE", "/v1.1/messagebatches/"+id+"/schedule", nil)
if err != nil {
return err
}
_, err = c.do(req, nil)
return err
}
type messageBatchesResponse struct {
StartIndex int `xml:"startindex,attr"`
Count int `xml:"count,attr"`
TotalCount int `xml:"totalcount,attr"`
Batches []messageBatchResponse `xml:"messagebatch"`
}
type messageBatchResponse struct {
ID string `xml:"id,attr"`
URI string `xml:"uri,attr"`
CreatedAt time.Time `xml:"createdat"`
BatchSize int `xml:"batchsize"`
PersistedBatchSize int `xml:"persistedbatchsize"`
Status messageBatchResponseStatuses `xml:"status"`
AccountReference string `xml:"accountreference"`
CreatedBy string `xml:"createdby"`
Name string `xml:"name"`
}
type messageBatchResponseStatuses struct {
List []messageBatchResponseStatus `xml:",any"`
}
type messageBatchResponseStatus struct {
XMLName xml.Name `xml:""`
Value int `xml:",chardata"`
}