-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.go
More file actions
134 lines (119 loc) · 5.21 KB
/
Copy pathwebhooks.go
File metadata and controls
134 lines (119 loc) · 5.21 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package vsax
import (
"context"
"fmt"
"iter"
"net/http"
"strconv"
)
// Notification webhook languages.
const (
WebhookLanguageEnglish = "en"
WebhookLanguageGerman = "de"
)
// NotificationWebhook is a webhook registration that receives notification
// events from VSA X. Headers is a plain key/value map sent with each POST.
type NotificationWebhook struct {
ID int64 `json:"Id"`
Name string `json:"Name,omitempty"`
Description string `json:"Description,omitempty"`
Priorities []string `json:"Priorities,omitempty"`
OrganizationIDs []int64 `json:"OrganizationIds,omitempty"`
Headers map[string]string `json:"Headers,omitempty"`
URL string `json:"Url,omitempty"`
Language string `json:"Language,omitempty"`
CreatedAt Time `json:"CreatedAt,omitzero"`
UpdatedAt Time `json:"UpdatedAt,omitzero"`
// SecretKey is only populated on create and on RegenerateSecretKey responses.
SecretKey string `json:"SecretKey,omitempty"`
}
// CreateNotificationWebhookRequest is the body for POST /notifications/webhooks.
type CreateNotificationWebhookRequest struct {
Name string `json:"Name"`
Description string `json:"Description,omitempty"`
Priorities []string `json:"Priorities"`
OrganizationIDs []int64 `json:"OrganizationIds,omitempty"`
Headers map[string]string `json:"Headers,omitempty"`
URL string `json:"Url"`
Language string `json:"Language,omitempty"`
}
// UpdateNotificationWebhookRequest is the body for PUT /notifications/webhooks/{id}.
type UpdateNotificationWebhookRequest struct {
Name string `json:"Name"`
Description string `json:"Description,omitempty"`
Priorities []string `json:"Priorities"`
OrganizationIDs []int64 `json:"OrganizationIds,omitempty"`
Headers map[string]string `json:"Headers,omitempty"`
URL string `json:"Url"`
Language string `json:"Language,omitempty"`
}
// RegenerateSecretKeyResult is the response to POST /notifications/webhooks/{id}/regenerateSecretKey.
type RegenerateSecretKeyResult struct {
SecretKey string `json:"SecretKey"`
}
// NotificationWebhookService provides access to notification-webhook endpoints
// under /notifications/webhooks.
type NotificationWebhookService struct {
client *Client
list listService[NotificationWebhook]
}
func newNotificationWebhookService(c *Client) *NotificationWebhookService {
return &NotificationWebhookService{
client: c,
list: listService[NotificationWebhook]{client: c, path: "/notifications/webhooks"},
}
}
// List retrieves one page of webhooks.
func (s *NotificationWebhookService) List(ctx context.Context, q *Query) (*PagedResult[NotificationWebhook], error) {
return s.list.List(ctx, q)
}
// All iterates all webhooks across pages.
func (s *NotificationWebhookService) All(ctx context.Context, q *Query) iter.Seq2[NotificationWebhook, error] {
return s.list.All(ctx, q)
}
// Get retrieves a single webhook by ID.
func (s *NotificationWebhookService) Get(ctx context.Context, id int64) (*NotificationWebhook, error) {
env, err := s.client.do(ctx, http.MethodGet, "/notifications/webhooks/"+strconv.FormatInt(id, 10), nil)
if err != nil {
return nil, err
}
return decodeOne[NotificationWebhook](env)
}
// Create registers a new webhook. The returned webhook includes the initial
// SecretKey used to sign webhook deliveries; store it, as it is not
// retrievable later without calling RegenerateSecretKey.
func (s *NotificationWebhookService) Create(ctx context.Context, body *CreateNotificationWebhookRequest) (*NotificationWebhook, error) {
if body == nil {
return nil, fmt.Errorf("vsax: CreateNotificationWebhookRequest body is required")
}
env, err := s.client.do(ctx, http.MethodPost, "/notifications/webhooks", body)
if err != nil {
return nil, err
}
return decodeOne[NotificationWebhook](env)
}
// Update modifies an existing webhook.
func (s *NotificationWebhookService) Update(ctx context.Context, id int64, body *UpdateNotificationWebhookRequest) (*NotificationWebhook, error) {
if body == nil {
return nil, fmt.Errorf("vsax: UpdateNotificationWebhookRequest body is required")
}
env, err := s.client.do(ctx, http.MethodPut, "/notifications/webhooks/"+strconv.FormatInt(id, 10), body)
if err != nil {
return nil, err
}
return decodeOne[NotificationWebhook](env)
}
// Delete removes a webhook.
func (s *NotificationWebhookService) Delete(ctx context.Context, id int64) error {
_, err := s.client.do(ctx, http.MethodDelete, "/notifications/webhooks/"+strconv.FormatInt(id, 10), nil)
return err
}
// RegenerateSecretKey rotates the webhook's HMAC signing key. Store the
// returned SecretKey — it is not retrievable later.
func (s *NotificationWebhookService) RegenerateSecretKey(ctx context.Context, id int64) (*RegenerateSecretKeyResult, error) {
env, err := s.client.do(ctx, http.MethodPost, "/notifications/webhooks/"+strconv.FormatInt(id, 10)+"/regenerateSecretKey", nil)
if err != nil {
return nil, err
}
return decodeOne[RegenerateSecretKeyResult](env)
}