-
Notifications
You must be signed in to change notification settings - Fork 9
/
topic.go
110 lines (102 loc) · 3.74 KB
/
topic.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
package tdmq
import (
"fmt"
"net/url"
"strconv"
"strings"
)
type Topic struct {
Client *Client
Name string
RoutingKey string
Tags []string
}
func (t *Topic) Publish(message string) (ResponseSM, error) {
return t.Client.PublishMessage(t.Name, message, t.RoutingKey, t.Tags)
}
func (t *Topic) BatchPublish(messages ...string) (ResponseSMs, error) {
return t.Client.BatchPublishMessage(t.Name, t.RoutingKey, messages, t.Tags)
}
// PublishMessage
// API: https://cloud.tencent.com/document/product/406/7411
// input: topic string
// input: message string
// input: routingKey string
// input: tags []string
// return: ResponseSM
// return: error
func (c *Client) PublishMessage(topic, message, routingKey string, tags []string) (ResponseSM, error) {
switch {
case !nameReg.MatchString(topic):
return nil, fmt.Errorf("%w topic name(0<len<%d): %s", ErrInvalidParameter, MaxTopicNameSize+1, topic)
case message == `` || len(message) > MaxMessageSize:
return nil, fmt.Errorf("%w message length(0<len<%d): %d", ErrInvalidParameter, MaxMessageSize+1, len(message))
case len(routingKey) > MaxRouteKeyLength:
return nil, fmt.Errorf("%w routing key(0<=len<%d): %s", ErrInvalidParameter, MaxRouteKeyLength+1, routingKey)
case len(tags) > MaxTagCount:
return nil, fmt.Errorf("%w message tags count[0~%d]: %v", ErrInvalidParameter, MaxTagCount, tags)
default:
if strings.Count(routingKey, `.`) > MaxRouteKeyDots {
return nil, fmt.Errorf("%w more than %d dot(.) in routing key: %s", ErrInvalidParameter, MaxRouteKeyDots, routingKey)
}
for _, v := range tags {
if v == `` || len(v) > MaxTagLength {
return nil, fmt.Errorf("%w message tag(0<len<%d): %s", ErrInvalidParameter, MaxTagLength+1, v)
}
}
}
values := url.Values{}
values.Set(`Action`, actionPubMsg)
values.Set(`topicName`, topic)
values.Set(`msgBody`, message)
values.Set(`routingKey`, routingKey)
for i, t := range tags {
values.Set(`msgTag.`+strconv.Itoa(i), t)
}
return c.call(values)
}
// BatchPublishMessage
// API: https://cloud.tencent.com/document/product/406/7412
// input: topic string
// input: routingKey string
// input: messages []string
// input: tags []string
// return: ResponseSMs
// return: error
func (c *Client) BatchPublishMessage(topic, routingKey string, messages, tags []string) (ResponseSMs, error) {
switch {
case !nameReg.MatchString(topic):
return nil, fmt.Errorf("%w topic name(0<len<%d): %s", ErrInvalidParameter, MaxTopicNameSize+1, topic)
case len(messages) == 0 || len(messages) > MaxMessageCount:
return nil, fmt.Errorf("%w messages count(0~%d]: %d", ErrInvalidParameter, MaxMessageCount, len(messages))
case len(routingKey) > MaxRouteKeyLength:
return nil, fmt.Errorf("%w routing key(0<=len<%d): %s", ErrInvalidParameter, MaxRouteKeyLength+1, routingKey)
case len(tags) > MaxTagCount:
return nil, fmt.Errorf("%w message tags count[0~%d]: %v", ErrInvalidParameter, MaxTagCount, tags)
default:
if strings.Count(routingKey, `.`) > MaxRouteKeyDots {
return nil, fmt.Errorf("%w more than %d dot(.) in routing key: %s", ErrInvalidParameter, MaxRouteKeyDots, routingKey)
}
for _, v := range messages {
if v == `` || len(v) > MaxMessageSize {
return nil, fmt.Errorf("%w message length(0<len<%d): %s", ErrInvalidParameter, MaxMessageSize+1, v)
}
}
for _, v := range tags {
if v == `` || len(v) > MaxTagLength {
return nil, fmt.Errorf("%w message tag(0<len<%d): %s", ErrInvalidParameter, MaxTagLength+1, v)
}
}
}
values := url.Values{}
values.Set(`Action`, actionBatchPub)
values.Set(`topicName`, topic)
values.Set(`routingKey`, routingKey)
for i, m := range messages {
values.Set(`msgBody.`+strconv.Itoa(i), m)
}
for i, t := range tags {
values.Set(`msgTag.`+strconv.Itoa(i), t)
}
return c.call(values)
}