forked from liping86/snshttp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscription_confirmation.go
55 lines (48 loc) · 1.43 KB
/
subscription_confirmation.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
package snshttp
import (
"context"
"fmt"
"net/http"
"strings"
)
// SubscriptionConfirmation is an initial event sent by Amazon SNS as part of a
// handshake before any Notification events can be sent. A call to Confirm or a
// request to SubscribeURL will finish the handshake and enable Amazon to send
// Notifications to the webhook.
type SubscriptionConfirmation struct {
BaseMessage
Token string
SubscribeURL string
}
// Confirm finishes the handshake with Amazon, confirming that the subscription
// should start sending notification. A request is made to the SubscribeURL. An
// error will be returned if the subscription has already been confirmed.
func (e *SubscriptionConfirmation) Confirm(ctx context.Context) error {
req, err := http.NewRequest("GET", e.SubscribeURL, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Server is expected to return 200 OK but we can treat any 200 level code as
// success.
if !(200 <= resp.StatusCode && resp.StatusCode < 300) {
return fmt.Errorf("server returned error status=%d", resp.StatusCode)
}
return nil
}
func (e *SubscriptionConfirmation) SigningString() string {
return strings.Join([]string{
"Message", e.Message,
"MessageId", e.MessageID,
"SubscribeURL", e.SubscribeURL,
"Timestamp", e.Timestamp,
"Token", e.Token,
"TopicArn", e.TopicARN,
"Type", e.Type,
"",
}, "\n")
}