generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
internalWebhook.go
77 lines (65 loc) · 1.61 KB
/
internalWebhook.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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package ancla
import (
"crypto/sha256"
"encoding/json"
"fmt"
"math"
"time"
// nolint:typecheck
"github.com/xmidt-org/argus/model"
)
type InternalWebhook struct {
PartnerIDs []string
Webhook Webhook
}
func InternalWebhookToItem(now func() time.Time, iw InternalWebhook) (model.Item, error) {
encodedWebhook, err := json.Marshal(iw)
if err != nil {
return model.Item{}, err
}
var data map[string]interface{}
err = json.Unmarshal(encodedWebhook, &data)
if err != nil {
return model.Item{}, err
}
SecondsToExpiry := iw.Webhook.Until.Sub(now()).Seconds()
TTLSeconds := int64(math.Max(0, SecondsToExpiry))
checksum := fmt.Sprintf("%x", sha256.Sum256([]byte(iw.Webhook.Config.URL)))
return model.Item{
Data: data,
ID: checksum,
TTL: &TTLSeconds,
}, nil
}
func ItemToInternalWebhook(i model.Item) (InternalWebhook, error) {
encodedWebhook, err := json.Marshal(i.Data)
if err != nil {
return InternalWebhook{}, err
}
var iw InternalWebhook
err = json.Unmarshal(encodedWebhook, &iw)
if err != nil {
return InternalWebhook{}, err
}
return iw, nil
}
func ItemsToInternalWebhooks(items []model.Item) ([]InternalWebhook, error) {
iws := []InternalWebhook{}
for _, item := range items {
iw, err := ItemToInternalWebhook(item)
if err != nil {
return nil, err
}
iws = append(iws, iw)
}
return iws, nil
}
func InternalWebhooksToWebhooks(iws []InternalWebhook) []Webhook {
w := make([]Webhook, 0, len(iws))
for _, iw := range iws {
w = append(w, iw.Webhook)
}
return w
}