-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessages.go
397 lines (346 loc) · 10.4 KB
/
messages.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package esendex
import (
"encoding/xml"
"net/url"
"time"
)
// Paging gives the details of the page that was accessed.
type Paging struct {
StartIndex int
Count int
TotalCount int
}
// FailureReason gives detailed information for why a message failed to send.
type FailureReason struct {
Code int
Description string
Permanent bool
}
type messageWithBody interface {
getBodyURI() string
}
// SentMessagesResponse is a list of returned messages along with the paging
// information.
type SentMessagesResponse struct {
Paging
Messages []SentMessageResponse
}
// SentMessageResponse is a single sent message. It implements messageWithBody.
type SentMessageResponse struct {
ID string
URI string
Reference string
Status string
LastStatusAt time.Time
SubmittedAt time.Time
Type MessageType
To string
From string
Summary string
Direction string
Parts int
Username string
FailureReason *FailureReason
BatchID string
bodyURI string
}
func (r SentMessageResponse) getBodyURI() string { return r.bodyURI }
// MessageResponse is a single message. It implements messageWithBody.
//
// BatchID might be nil, as inbound messages do not have a batch ID.
type MessageResponse struct {
ID string
URI string
Reference string
Status string
LastStatusAt time.Time
SubmittedAt time.Time
ReceivedAt time.Time
Type MessageType
To string
From string
Summary string
Direction string
ReadAt time.Time
SentAt time.Time
DeliveredAt time.Time
ReadBy string
Parts int
Username string
FailureReason *FailureReason
BatchID *string
bodyURI string
}
func (r MessageResponse) getBodyURI() string { return r.bodyURI }
// ReceivedMessagesResponse is a list of received messages along with the paging
// information.
type ReceivedMessagesResponse struct {
Paging
Messages []ReceivedMessageResponse
}
// ReceivedMessageResponse is a single received message. It implements messageWithBody.
type ReceivedMessageResponse struct {
ID string
URI string
Reference string
Status string
ReceivedAt time.Time
Type MessageType
To string
From string
Summary string
Direction string
Parts int
ReadAt time.Time
ReadBy string
bodyURI string
}
func (r ReceivedMessageResponse) getBodyURI() string { return r.bodyURI }
// MessageBody is the body of a message.
type MessageBody struct {
Text string
CharacterSet string
}
// Sent returns a list of messages sent by the user.
func (c *Client) Sent(opts ...Option) (*SentMessagesResponse, error) {
req, err := c.newRequest("GET", "/v1.0/messageheaders", nil)
if err != nil {
return nil, err
}
for _, opt := range opts {
opt(req)
}
var v messageHeadersResponse
if _, err := c.do(req, &v); err != nil {
return nil, err
}
response := &SentMessagesResponse{
Paging: Paging{
StartIndex: v.StartIndex,
Count: v.Count,
TotalCount: v.TotalCount,
},
Messages: make([]SentMessageResponse, len(v.Messages)),
}
for i, message := range v.Messages {
response.Messages[i] = SentMessageResponse{
ID: message.ID,
URI: message.URI,
Reference: message.Reference,
Status: message.Status,
LastStatusAt: message.LastStatusAt.Time,
SubmittedAt: message.SubmittedAt.Time,
Type: MessageType(message.Type),
To: message.To,
From: message.From,
Summary: message.Summary,
bodyURI: message.Body.URI,
Direction: message.Direction,
Parts: message.Parts,
Username: message.Username,
BatchID: message.Batch.ID,
}
if message.FailureReason != nil {
response.Messages[i].FailureReason = &FailureReason{
Code: message.FailureReason.Code,
Description: message.FailureReason.Description,
Permanent: message.FailureReason.Permanent,
}
}
}
return response, nil
}
// Received returns the messages sent to the user.
func (c *Client) Received(opts ...Option) (*ReceivedMessagesResponse, error) {
req, err := c.newRequest("GET", "/v1.0/inbox/messages", nil)
if err != nil {
return nil, err
}
for _, opt := range opts {
opt(req)
}
var v inboxResponse
if _, err = c.do(req, &v); err != nil {
return nil, err
}
response := &ReceivedMessagesResponse{
Paging: Paging{
StartIndex: v.StartIndex,
Count: v.Count,
TotalCount: v.TotalCount,
},
Messages: make([]ReceivedMessageResponse, len(v.Messages)),
}
for i, message := range v.Messages {
response.Messages[i] = ReceivedMessageResponse{
ID: message.ID,
URI: message.URI,
Reference: message.Reference,
Status: message.Status,
ReceivedAt: message.ReceivedAt.Time,
Type: MessageType(message.Type),
To: message.To,
From: message.From,
Summary: message.Summary,
bodyURI: message.Body.URI,
Direction: message.Direction,
Parts: message.Parts,
ReadAt: message.ReadAt.Time,
ReadBy: message.ReadBy,
}
}
return response, nil
}
// Message returns the message with the given id.
func (c *Client) Message(id string) (*MessageResponse, error) {
req, err := c.newRequest("GET", "/v1.0/messageheaders/"+id, nil)
if err != nil {
return nil, err
}
var v messageHeadersResponseMessageHeader
if _, err = c.do(req, &v); err != nil {
return nil, err
}
response := &MessageResponse{
ID: v.ID,
URI: v.URI,
Reference: v.Reference,
Status: v.Status,
LastStatusAt: v.LastStatusAt.Time,
SubmittedAt: v.SubmittedAt.Time,
ReceivedAt: v.ReceivedAt.Time,
Type: MessageType(v.Type),
To: v.To,
From: v.From,
Summary: v.Summary,
bodyURI: v.Body.URI,
Direction: v.Direction,
ReadAt: v.ReadAt.Time,
SentAt: v.SentAt.Time,
DeliveredAt: v.DeliveredAt.Time,
ReadBy: v.ReadBy,
Parts: v.Parts,
Username: v.Username,
BatchID: nil,
}
if v.Batch != nil {
response.BatchID = &v.Batch.ID
}
if v.FailureReason != nil {
response.FailureReason = &FailureReason{
Code: v.FailureReason.Code,
Description: v.FailureReason.Description,
Permanent: v.FailureReason.Permanent,
}
}
return response, nil
}
// Body returns the full body of a single message.
func (c *Client) Body(message messageWithBody) (*MessageBody, error) {
u, err := url.Parse(message.getBodyURI())
if err != nil {
return nil, err
}
req, err := c.newRequest("GET", u.Path, nil)
if err != nil {
return nil, err
}
var v messageBodyResponse
if _, err = c.do(req, &v); err != nil {
return nil, err
}
return &MessageBody{
Text: v.BodyText,
CharacterSet: v.CharacterSet,
}, nil
}
type messageBodyResponse struct {
XMLName xml.Name `xml:"http://api.esendex.com/ns/ messagebody"`
BodyText string `xml:"bodytext"`
CharacterSet string `xml:"characterset"`
}
type messageFailureReason struct {
Code int `xml:"code"`
Description string `xml:"description"`
Permanent bool `xml:"permanentfailure"`
}
type messageHeadersResponse struct {
XMLName xml.Name `xml:"http://api.esendex.com/ns/ messageheaders"`
StartIndex int `xml:"startindex,attr"`
Count int `xml:"count,attr"`
TotalCount int `xml:"totalcount,attr"`
Messages []messageHeadersResponseMessageHeader `xml:"messageheader"`
}
type messageHeadersResponseMessageHeader struct {
ID string `xml:"id,attr"`
URI string `xml:"uri,attr"`
Reference string `xml:"reference"`
Status string `xml:"status"`
LastStatusAt messageHeaderTime `xml:"laststatusat"`
SubmittedAt messageHeaderTime `xml:"submittedat"`
ReceivedAt messageHeaderTime `xml:"receivedat"`
Type string `xml:"type"`
To string `xml:"to>phonenumber"`
From string `xml:"from>phonenumber"`
Summary string `xml:"summary"`
Body struct {
URI string `xml:"uri,attr"`
} `xml:"body"`
Direction string `xml:"direction"`
ReadAt messageHeaderTime `xml:"readat"`
SentAt messageHeaderTime `xml:"sentat"`
DeliveredAt messageHeaderTime `xml:"deliveredat"`
ReadBy string `xml:"readby"`
Parts int `xml:"parts"`
Username string `xml:"username"`
FailureReason *messageFailureReason `xml:"failurereason"`
Batch *resourceLink `xml:"batch"`
}
type resourceLink struct {
ID string `xml:"id,attr"`
Link string `xml:"link,attr"`
}
type inboxResponse struct {
XMLName xml.Name `xml:"http://api.esendex.com/ns/ messageheaders"`
StartIndex int `xml:"startindex,attr"`
Count int `xml:"count,attr"`
TotalCount int `xml:"totalcount,attr"`
Messages []inboxResponseMessageHeader `xml:"messageheader"`
}
type inboxResponseMessageHeader struct {
ID string `xml:"id,attr"`
URI string `xml:"uri,attr"`
Reference string `xml:"reference"`
Status string `xml:"status"`
ReceivedAt messageHeaderTime `xml:"receivedat"`
Type string `xml:"type"`
To string `xml:"to>phonenumber"`
From string `xml:"from>phonenumber"`
Summary string `xml:"summary"`
Body struct {
URI string `xml:"uri,attr"`
} `xml:"body"`
Direction string `xml:"direction"`
Parts int `xml:"parts"`
ReadAt messageHeaderTime `xml:"readat"`
ReadBy string `xml:"readby"`
}
const messageHeaderTimeFormat = "2006-01-02T15:04:05.999999999"
const messageHeaderTimeFormatZ = "2006-01-02T15:04:05.999999999Z"
type messageHeaderTime struct {
time.Time
}
func (t messageHeaderTime) MarshalText() ([]byte, error) {
return []byte(t.Format(messageHeaderTimeFormat)), nil
}
func (t *messageHeaderTime) UnmarshalText(data []byte) error {
g, err := time.ParseInLocation(messageHeaderTimeFormat, string(data), time.UTC)
if err != nil {
g, err = time.ParseInLocation(messageHeaderTimeFormatZ, string(data), time.UTC)
if err != nil {
return err
}
}
*t = messageHeaderTime{g}
return nil
}