forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.go
51 lines (43 loc) · 1.28 KB
/
notifications.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
package main
import (
"github.com/franela/goreq"
"time"
)
// NotificationsManager handles sending notifications to OAuth endpoints to notify the provider of key changes.
// TODO: Make this more generic
type NotificationsManager struct {
SharedSecret string `bson:"shared_secret" json:"shared_secret"`
OAuthKeyChangeURL string `bson:"oauth_on_keychange_url" json:"oauth_on_keychange_url"`
}
// SendRequest sends the requested package (as a POST) to the defined
func (n NotificationsManager) SendRequest(wait bool, count int, notification interface{}) {
if wait {
if count < 3 {
time.Sleep(10 * time.Second)
} else {
log.Error("Too many notification attempts, aborting.")
return
}
}
req := goreq.Request{
Method: "POST",
Uri: n.OAuthKeyChangeURL,
UserAgent: "Tyk-Gatewy-Notifications",
ContentType: "application/json",
Body: notification,
}
req.AddHeader("X-Tyk-Shared-Secret", n.SharedSecret)
resp, reqErr := req.Do()
if reqErr != nil {
log.Error("Request failed, trying again in 10s. Error was: ", reqErr)
count++
go n.SendRequest(true, count, notification)
return
}
if resp.StatusCode != 200 {
log.Error("Request returned non-200 status, trying again in 10s.")
count++
go n.SendRequest(true, count, notification)
return
}
}