-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebhook.go
110 lines (86 loc) · 1.96 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package webhook
import (
"time"
"github.com/code-payments/code-server/pkg/pointer"
"github.com/pkg/errors"
)
type State uint8
const (
StateUnknown State = iota // Not actionable (ie. the event to trigger hasn't occurred yet)
StatePending // Actively sending to third party
StateConfirmed // Third party successfully processed webhook
StateFailed // Third party failed to process webhook after sufficient retries
)
type Type uint8
const (
TypeUnknown Type = iota
TypeIntentSubmitted
TypeTest
)
type Record struct {
Id uint64
WebhookId string
Url string
Type Type
Attempts uint8
State State
CreatedAt time.Time
NextAttemptAt *time.Time
}
func (r *Record) Validate() error {
if len(r.WebhookId) == 0 {
return errors.New("webhook id is required")
}
if len(r.Url) == 0 {
return errors.New("url is required")
}
if r.Type == TypeUnknown {
return errors.New("type is required")
}
switch r.State {
case StatePending:
if r.NextAttemptAt == nil || r.NextAttemptAt.IsZero() {
return errors.New("next attempt timestamp is required")
}
default:
if r.NextAttemptAt != nil {
return errors.New("next attempt timestamp cannot be set")
}
}
return nil
}
func (r *Record) Clone() Record {
return Record{
Id: r.Id,
WebhookId: r.WebhookId,
Url: r.Url,
Type: r.Type,
Attempts: r.Attempts,
State: r.State,
CreatedAt: r.CreatedAt,
NextAttemptAt: pointer.TimeCopy(r.NextAttemptAt),
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.WebhookId = r.WebhookId
dst.Url = r.Url
dst.Type = r.Type
dst.Attempts = r.Attempts
dst.State = r.State
dst.CreatedAt = r.CreatedAt
dst.NextAttemptAt = pointer.TimeCopy(r.NextAttemptAt)
}
func (s State) String() string {
switch s {
case StateUnknown:
return "unknown"
case StatePending:
return "pending"
case StateConfirmed:
return "confirmed"
case StateFailed:
return "failed"
}
return "unknown"
}