generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebhook.go
94 lines (72 loc) · 3.25 KB
/
webhook.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package ancla
import (
"time"
)
// DeliveryConfig is a Webhook substructure with data related to event delivery.
type DeliveryConfig struct {
// URL is the HTTP URL to deliver messages to.
URL string `json:"url"`
// ContentType is content type value to set WRP messages to (unless already specified in the WRP).
ContentType string `json:"content_type"`
// Secret is the string value for the SHA1 HMAC.
// (Optional, set to "" to disable behavior).
Secret string `json:"secret,omitempty"`
// AlternativeURLs is a list of explicit URLs that should be round robin through on failure cases to the main URL.
AlternativeURLs []string `json:"alt_urls,omitempty"`
}
// MetadataMatcherConfig is Webhook substructure with config to match event metadata.
type MetadataMatcherConfig struct {
// DeviceID is the list of regular expressions to match device id type against.
DeviceID []string `json:"device_id"`
}
// Webhook contains all the information needed to serve events to webhook listeners.
type Webhook struct {
// Address is the subscription request origin HTTP Address.
Address string `json:"registered_from_address"`
// Config contains data to inform how events are delivered.
Config DeliveryConfig `json:"config"`
// FailureURL is the URL used to notify subscribers when they've been cut off due to event overflow.
// Optional, set to "" to disable notifications.
FailureURL string `json:"failure_url"`
// Events is the list of regular expressions to match an event type against.
Events []string `json:"events"`
// Matcher type contains values to match against the metadata.
Matcher MetadataMatcherConfig `json:"matcher,omitempty"`
// Duration describes how long the subscription lasts once added.
Duration time.Duration `json:"duration"`
// Until describes the time this subscription expires.
Until time.Time `json:"until"`
}
// WebhookRegistration is a special struct for unmarshaling a webhook as part of
// a webhook registration request. The only difference between this struct and
// the Webhook struct is the Duration field.
type WebhookRegistration struct {
// Address is the subscription request origin HTTP Address.
Address string `json:"registered_from_address"`
// Config contains data to inform how events are delivered.
Config DeliveryConfig `json:"config"`
// FailureURL is the URL used to notify subscribers when they've been cut off due to event overflow.
// Optional, set to "" to disable notifications.
FailureURL string `json:"failure_url"`
// Events is the list of regular expressions to match an event type against.
Events []string `json:"events"`
// Matcher type contains values to match against the metadata.
Matcher MetadataMatcherConfig `json:"matcher,omitempty"`
// Duration describes how long the subscription lasts once added.
Duration CustomDuration `json:"duration"`
// Until describes the time this subscription expires.
Until time.Time `json:"until"`
}
func (w WebhookRegistration) ToWebhook() Webhook {
return Webhook{
Address: w.Address,
Config: w.Config,
FailureURL: w.FailureURL,
Events: w.Events,
Matcher: w.Matcher,
Duration: time.Duration(w.Duration),
Until: w.Until,
}
}