-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent_patterns.go
More file actions
123 lines (103 loc) · 3.39 KB
/
Copy pathevent_patterns.go
File metadata and controls
123 lines (103 loc) · 3.39 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
package loops
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// EventPatternSummary is the list-item representation of an event pattern
// returned by [Client.ListEventPatterns].
type EventPatternSummary struct {
ID string `json:"id"`
EventName string `json:"eventName"`
// IncomingWebhookPlatform names the platform that sent this event
// pattern when it originates from an incoming webhook (one of "clerk",
// "polar", "stripe", or "supabase"). It is nil for custom events.
IncomingWebhookPlatform *string `json:"incomingWebhookPlatform"`
}
// EventPattern is the full representation of an event pattern, including the
// event properties usable in emails.
type EventPattern struct {
ID string `json:"id"`
EventName string `json:"eventName"`
EventProperties []WorkflowEventProperty `json:"eventProperties"`
// IncomingWebhookPlatform names the platform that sent this event
// pattern when it originates from an incoming webhook (one of "clerk",
// "polar", "stripe", or "supabase"). It is nil for custom events.
IncomingWebhookPlatform *string `json:"incomingWebhookPlatform"`
}
// ListEventPatterns returns a single page of event patterns along with
// pagination information. To iterate every page, use [Paginate].
func (c *Client) ListEventPatterns(params PaginationParams) ([]EventPatternSummary, *Pagination, error) {
q := url.Values{}
if params.PerPage != "" {
q.Set("perPage", params.PerPage)
}
if params.Cursor != "" {
q.Set("cursor", params.Cursor)
}
path := "/event-patterns"
if len(q) > 0 {
path += "?" + q.Encode()
}
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, errorFromResponse(resp)
}
var result struct {
Pagination Pagination `json:"pagination"`
Data []EventPatternSummary `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, nil, fmt.Errorf("failed to decode response: %w", err)
}
return result.Data, &result.Pagination, nil
}
// GetEventPatternByName returns the event pattern identified by eventName.
func (c *Client) GetEventPatternByName(eventName string) (*EventPattern, error) {
req, err := c.newRequest(http.MethodGet, "/event-patterns/by-name/"+url.PathEscape(eventName), nil)
if err != nil {
return nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResponse(resp)
}
var result EventPattern
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}
// GetEventPattern returns the event pattern identified by id.
func (c *Client) GetEventPattern(id string) (*EventPattern, error) {
req, err := c.newRequest(http.MethodGet, "/event-patterns/"+id, nil)
if err != nil {
return nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResponse(resp)
}
var result EventPattern
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}