-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
83 lines (65 loc) · 2.59 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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package webhook
import (
"fmt"
"time"
)
var (
ErrInvalidInput = fmt.Errorf("invalid input")
)
// DeliveryConfig is a Webhook substructure with data related to event delivery.
type DeliveryConfig struct {
// URL is the HTTP URL to deliver messages to.
ReceiverURL 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"`
}
// Registration 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 Registration 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"`
// now is a function that returns the current time. It is used for testing.
nowFunc func() time.Time `json:"-"`
}
type Option interface {
fmt.Stringer
Validate(*Registration) error
}
// Validate is a method on Registration that validates the registration
// against a list of options.
func (r *Registration) Validate(opts ...Option) error {
for _, opt := range opts {
if opt != nil {
if err := opt.Validate(r); err != nil {
return err
}
}
}
return nil
}