-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtopic.go
58 lines (51 loc) · 1.47 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
package sns
import (
SDK "github.com/aws/aws-sdk-go/service/sns"
"github.com/evalphobia/aws-sdk-go-wrapper/private/pointers"
)
// Topic is struct for Topic.
type Topic struct {
svc *SNS
name string
nameWithPrefix string
arn string
sound string
}
// NewTopic returns initialized *Topic.
func NewTopic(svc *SNS, arn, name string) *Topic {
topicName := svc.prefix + name
return &Topic{
svc: svc,
arn: arn,
name: name,
nameWithPrefix: topicName,
sound: "default",
}
}
// Subscribe operates `Subscribe` and returns `SubscriptionArn`.
func (t *Topic) Subscribe(endpointARN, protocol string) (subscriptionARN string, err error) {
resp, err := t.svc.client.Subscribe(&SDK.SubscribeInput{
Endpoint: pointers.String(endpointARN),
Protocol: pointers.String(protocol),
TopicArn: pointers.String(t.arn),
})
if err != nil {
t.svc.Errorf("error on `Subscribe` operation; name=%s; error=%s;", t.nameWithPrefix, err.Error())
return "", err
}
return *resp.SubscriptionArn, nil
}
// Publish sends notification to the topic.
func (t *Topic) Publish(msg string) error {
return t.svc.Publish(t.arn, msg, nil)
}
// Delete deltes the topic.
func (t *Topic) Delete() error {
_, err := t.svc.client.DeleteTopic(&SDK.DeleteTopicInput{
TopicArn: pointers.String(t.arn),
})
if err != nil {
t.svc.Errorf("error on `DeleteTopic` operation; name=%s; error=%s;", t.nameWithPrefix, err.Error())
}
return err
}