forked from deadmanssnitch/snshttp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunsubscribe_confirmation.go
60 lines (52 loc) · 1.5 KB
/
unsubscribe_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
56
57
58
59
60
package snshttp
import (
"context"
"fmt"
"net/http"
"strings"
)
// UnsubscribeConfirmation events are received when a subscription is canceled
// via the API. No unsubscribe event is fired when deleting a subscription
// through the AWS web console.
type UnsubscribeConfirmation struct {
Type string
MessageID string `json:"MessageId"`
TopicARN string `json:"TopicArn"`
Timestamp string `json:"Timestamp"`
Token string `json:"Token"`
Message string `json:"Message"`
SubscribeURL string `json:"SubscribeURL"`
Signature string `json:"Signature"`
SigningCertURL string `json:"SigningCertURL"`
}
// Resubscribe notifies Amazon to reinstate the subscription. A request is made
// to the SubscribeURL.
func (e *UnsubscribeConfirmation) Resubscribe(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 *UnsubscribeConfirmation) 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")
}