-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreceive_message_response.go
85 lines (73 loc) · 1.9 KB
/
receive_message_response.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
package sqs
import (
"encoding/xml"
"errors"
"strconv"
"strings"
)
var (
errBadMD5Sum = errors.New("Body MD5 is invalid")
)
type ReceiveMessageResponse struct {
XMLName xml.Name `xml:"ReceiveMessageResponse"`
Things string
ReceiveMessageResult struct {
Message []struct {
MessageId string
ReceiptHandle string
MD5OfBody string
Body string
Attribute []struct {
Name string
Value string
}
}
}
ResponseMetadata struct {
RequestId string
}
}
func (r *ReceiveMessageResponse) GetMessages(QueueURL string) ([]*Message, error) {
var err error
var messages []*Message
for _, rawmsg := range r.ReceiveMessageResult.Message {
msg := &Message{
MessageID: strings.Trim(rawmsg.MessageId, " \t\n\r"),
ReceiptHandle: strings.Trim(rawmsg.ReceiptHandle, " \t\n\r"),
MD5: strings.Trim(rawmsg.MD5OfBody, " \t\n\r"),
Body: rawmsg.Body,
QueueURL: QueueURL,
}
// That should never be a problem unless some underlying
// implementation or dependency is doing something unhealthy.
if !msg.CheckMD5() {
return nil, errBadMD5Sum
}
for _, attr := range rawmsg.Attribute {
err = nil
switch attr.Name {
case "SenderId":
msg.SenderID = attr.Value
case "SentTimestamp":
msg.SentTimestamp, err = strconv.ParseUint(attr.Value, 10, 64)
case "ApproximateReceiveCount":
msg.ApproximateReceiveCount, err = strconv.ParseUint(attr.Value, 10, 64)
case "ApproximateFirstReceiveTimestamp":
msg.ApproximateFirstReceiveTimestamp, err = strconv.ParseUint(attr.Value, 10, 64)
}
if err != nil {
return nil, err
}
}
messages = append(messages, msg)
}
return messages, nil
}
func NewReceiveMessageResponse(data []byte) (*ReceiveMessageResponse, error) {
r := &ReceiveMessageResponse{}
err := xml.Unmarshal(data, &r)
if err != nil {
return nil, err
}
return r, nil
}