-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.go
More file actions
206 lines (166 loc) · 6.09 KB
/
Copy pathwebhooks.go
File metadata and controls
206 lines (166 loc) · 6.09 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2026 AxonFlow
// SPDX-License-Identifier: MIT
package axonflow
import (
"context"
"fmt"
)
// ============================================================================
// Webhook CRUD Types (Feature 7)
// ============================================================================
// CreateWebhookRequest is the request to create a new webhook subscription.
type CreateWebhookRequest struct {
// URL is the endpoint to deliver webhook events to (required)
URL string `json:"url"`
// Events is the list of event types to subscribe to (required)
Events []string `json:"events"`
// Secret is an optional shared secret for HMAC signature verification
Secret string `json:"secret,omitempty"`
// Active indicates whether the webhook is active
Active bool `json:"active"`
}
// WebhookSubscription represents a webhook subscription.
type WebhookSubscription struct {
// ID is the unique identifier for the webhook
ID string `json:"id"`
// URL is the endpoint receiving webhook events
URL string `json:"url"`
// Events is the list of subscribed event types
Events []string `json:"events"`
// Active indicates whether the webhook is active
Active bool `json:"active"`
// TenantID is the tenant that owns this subscription.
TenantID string `json:"tenant_id,omitempty"`
// OrgID is the organization that owns this subscription.
OrgID string `json:"org_id,omitempty"`
// Secret is the HMAC-SHA256 signing key for verifying inbound
// webhook payload signatures (X-AxonFlow-Signature header).
// Returned by CreateWebhook on initial creation; required for
// callers to validate payload authenticity.
Secret string `json:"secret,omitempty"`
// CreatedAt is when the webhook was created
CreatedAt string `json:"created_at"`
// UpdatedAt is when the webhook was last updated
UpdatedAt string `json:"updated_at"`
}
// UpdateWebhookRequest is the request to update an existing webhook subscription.
type UpdateWebhookRequest struct {
// URL is the new endpoint URL (optional)
URL string `json:"url,omitempty"`
// Events is the new list of event types (optional)
Events []string `json:"events,omitempty"`
// Active is the new active status (optional, use pointer to distinguish from zero value)
Active *bool `json:"active,omitempty"`
}
// ListWebhooksResponse is the response from listing webhook subscriptions.
type ListWebhooksResponse struct {
// Webhooks is the list of webhook subscriptions
Webhooks []WebhookSubscription `json:"webhooks"`
// Total is the total count of webhooks
Total int `json:"total"`
}
// ============================================================================
// Webhook CRUD Methods (Feature 7)
// ============================================================================
// CreateWebhook creates a new webhook subscription.
//
// Example:
//
// webhook, err := client.CreateWebhook(CreateWebhookRequest{
// URL: "https://example.com/webhooks",
// Events: []string{"workflow.completed", "step.approval_required"},
// Active: true,
// })
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Webhook created: %s\n", webhook.ID)
func (c *AxonFlowClient) CreateWebhook(req CreateWebhookRequest) (*WebhookSubscription, error) {
fullURL := c.config.Endpoint + "/api/v1/webhooks"
var result WebhookSubscription
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, req, &result); err != nil {
return nil, fmt.Errorf("failed to create webhook: %w", err)
}
return &result, nil
}
// GetWebhook retrieves a webhook subscription by ID.
//
// Example:
//
// webhook, err := client.GetWebhook("wh_123")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Webhook %s: %s (active: %v)\n", webhook.ID, webhook.URL, webhook.Active)
func (c *AxonFlowClient) GetWebhook(webhookID string) (*WebhookSubscription, error) {
if webhookID == "" {
return nil, fmt.Errorf("webhook ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/webhooks/%s", c.config.Endpoint, webhookID)
var result WebhookSubscription
if err := c.makeJSONRequest(context.Background(), "GET", fullURL, nil, &result); err != nil {
return nil, fmt.Errorf("failed to get webhook: %w", err)
}
return &result, nil
}
// UpdateWebhook updates an existing webhook subscription.
//
// Example:
//
// active := false
// webhook, err := client.UpdateWebhook("wh_123", UpdateWebhookRequest{
// Active: &active,
// })
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Webhook updated: %s (active: %v)\n", webhook.ID, webhook.Active)
func (c *AxonFlowClient) UpdateWebhook(webhookID string, req UpdateWebhookRequest) (*WebhookSubscription, error) {
if webhookID == "" {
return nil, fmt.Errorf("webhook ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/webhooks/%s", c.config.Endpoint, webhookID)
var result WebhookSubscription
if err := c.makeJSONRequest(context.Background(), "PUT", fullURL, req, &result); err != nil {
return nil, fmt.Errorf("failed to update webhook: %w", err)
}
return &result, nil
}
// DeleteWebhook deletes a webhook subscription.
//
// Example:
//
// err := client.DeleteWebhook("wh_123")
// if err != nil {
// log.Fatal(err)
// }
func (c *AxonFlowClient) DeleteWebhook(webhookID string) error {
if webhookID == "" {
return fmt.Errorf("webhook ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/webhooks/%s", c.config.Endpoint, webhookID)
if err := c.makeJSONRequest(context.Background(), "DELETE", fullURL, nil, nil); err != nil {
return fmt.Errorf("failed to delete webhook: %w", err)
}
return nil
}
// ListWebhooks lists all webhook subscriptions.
//
// Example:
//
// result, err := client.ListWebhooks()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Found %d webhooks\n", result.Total)
// for _, wh := range result.Webhooks {
// fmt.Printf(" %s: %s (active: %v)\n", wh.ID, wh.URL, wh.Active)
// }
func (c *AxonFlowClient) ListWebhooks() (*ListWebhooksResponse, error) {
fullURL := c.config.Endpoint + "/api/v1/webhooks"
var result ListWebhooksResponse
if err := c.makeJSONRequest(context.Background(), "GET", fullURL, nil, &result); err != nil {
return nil, fmt.Errorf("failed to list webhooks: %w", err)
}
return &result, nil
}