-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreceive_message.go
106 lines (93 loc) · 2.88 KB
/
receive_message.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
package alimns
import (
"context"
"encoding/xml"
"errors"
"fmt"
"net/http"
)
// ReceiveMessage 收到消息
type ReceiveMessage struct {
XMLName xml.Name `xml:"Message"`
XMLNs string `xml:"xmlns,attr"`
MessageID string `xml:"MessageId"`
ReceiptHandle string `xml:"ReceiptHandle"`
MessageBody string `xml:"MessageBody"`
MessageBodyMD5 string `xml:"MessageBodyMD5"`
EnqueueTime int64 `xml:"EnqueueTime"`
NextVisibleTime int64 `xml:"NextVisibleTime"`
FirstDequeueTime int64 `xml:"FirstDequeueTime"`
DequeueCount int `xml:"DequeueCount"`
Priority int `xml:"Priority"`
}
// ReceiveMessageResponse 收到消息回复
type ReceiveMessageResponse struct {
ReceiveMessage
}
// ReceiveMessageParam 收到消息请求
type ReceiveMessageParam struct {
WaitSeconds *int `url:"waitseconds,omitempty"`
NumOfMessages int `url:"numOfMessages,omitempty"`
}
// DefaultReceiveMessage 默认的收到消息请求参数
func DefaultReceiveMessage() ReceiveMessageParam {
return ReceiveMessageParam{}
}
// ReceiveMessageParamSetter 收到消息请求参数设置函数
type ReceiveMessageParamSetter func(*ReceiveMessageParam) error
// WithReceiveMessageWaitSeconds 设置收到消息的long poll等待时长
func WithReceiveMessageWaitSeconds(s int) ReceiveMessageParamSetter {
return func(rm *ReceiveMessageParam) error {
if s < minPollingWaitSeconds || s > maxPollingWaitSeconds {
return errors.New("polling wait seconds out of range")
}
rm.WaitSeconds = &s
return nil
}
}
const (
defaultReceiveMessage = 16
minReceiveMessage = 1
maxReceiveMessage = 16
)
// WithReceiveMessageNumOfMessages 设置请求消息数量
func WithReceiveMessageNumOfMessages(num int) ReceiveMessageParamSetter {
return func(rm *ReceiveMessageParam) error {
if num < minReceiveMessage || num > maxReceiveMessage {
return errors.New("num of receive message out of range")
}
rm.NumOfMessages = num
return nil
}
}
// ReceiveMessage 接收消息
func (c *Client) ReceiveMessage(name string, setters ...ReceiveMessageParamSetter) (*ReceiveMessageResponse, error) {
var err error
receiveMessage := DefaultReceiveMessage()
for _, setter := range setters {
err = setter(&receiveMessage)
if err != nil {
return nil, err
}
}
requestLine := fmt.Sprintf(mnsReceiveMessage, name)
req := c.ca.NewRequest().Get().WithPath(requestLine).WithQueryParam(&receiveMessage)
resp, err := c.ca.Do(context.TODO(), req)
if err != nil {
return nil, err
}
switch resp.StatusCode() {
case http.StatusOK:
var receiveMessageResponse ReceiveMessageResponse
if err := resp.DecodeFromXML(&receiveMessageResponse); err != nil {
return nil, err
}
return &receiveMessageResponse, nil
default:
var respErr RespErr
if err := resp.DecodeFromXML(&respErr); err != nil {
return nil, err
}
return nil, errors.New(respErr.Code)
}
}