-
Notifications
You must be signed in to change notification settings - Fork 9
/
event.go
189 lines (154 loc) · 4.49 KB
/
event.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
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
package convoy_go
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
)
var (
ErrNotListEventResponse = errors.New("invalid list event response")
ErrNotEventResponse = errors.New("invalid event response")
)
type Event struct {
client *Client
}
type CreateEventRequest struct {
EndpointID string `json:"endpoint_id"`
EventType string `json:"event_type"`
IdempotencyKey string `json:"idempotency_key"`
CustomHeaders map[string]string `json:"custom_headers"`
Data json.RawMessage `json:"data"`
}
type CreateFanoutEventRequest struct {
OwnerID string `json:"owner_id"`
EventType string `json:"event_type"`
IdempotencyKey string `json:"idempotency_key"`
CustomHeaders map[string]string `json:"custom_headers"`
Data json.RawMessage `json:"data"`
}
type CreateBroadcastEventRequest struct {
EventType string `json:"event_type"`
CustomHeaders map[string]string `json:"custom_headers"`
IdempotencyKey string `json:"idempotency_key"`
Data json.RawMessage `json:"data"`
}
type CreateDynamicEventRequest struct {
Endpoint string `json:"endpoint"`
Subscription string `json:"subscription"`
Event string `json:"event"`
}
type EventResponse struct {
UID string `json:"uid"`
EventType string `json:"event_type"`
MatchedEndpoints int `json:"matched_endpoints"`
ProviderID string `json:"provider_id"`
Data json.RawMessage `json:"data"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type ListEventResponse struct {
Content []EventResponse `json:"content"`
Pagination Pagination `json:"pagination"`
}
type EventParams struct {
ListParams
Query string `url:"query"`
IdempotencyKey string `url:"idempotencyKey"`
SourceID string `url:"sourceId"`
EndpointID []string `url:"endpointId"`
StartDate time.Time `url:"startDate" layout:"2006-01-02T15:04:05"`
EndDate time.Time `url:"endDate" layout:"2006-01-02T15:04:05"`
}
type BatchReplayOptions struct {
SourceID string `url:"sourceId"`
StartDate time.Time `url:"startDate" layout:"2006-01-02T15:04:05"`
EndDate time.Time `url:"endDate" layout:"2006-01-02T15:04:05"`
}
func newEvent(client *Client) *Event {
return &Event{
client: client,
}
}
func (e *Event) All(ctx context.Context, query *EventParams) (*ListEventResponse, error) {
url, err := addOptions(e.generateUrl(), query)
if err != nil {
return nil, err
}
respPtr := &ListEventResponse{}
err = getResource(ctx, e.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *Event) Create(ctx context.Context, body *CreateEventRequest) error {
url, err := addOptions(e.generateUrl(), nil)
if err != nil {
return err
}
err = postJSON(ctx, e.client, url, body, nil)
if err != nil {
return err
}
return nil
}
func (e *Event) FanoutEvent(ctx context.Context, body *CreateFanoutEventRequest) error {
url, err := addOptions(e.generateUrl()+"/fanout", nil)
if err != nil {
return err
}
err = postJSON(ctx, e.client, url, body, nil)
if err != nil {
return err
}
return nil
}
func (e *Event) BroadcastEvent(ctx context.Context, body *CreateBroadcastEventRequest) error {
url, err := addOptions(e.generateUrl()+"/broadcast", nil)
if err != nil {
return err
}
err = postJSON(ctx, e.client, url, body, nil)
if err != nil {
return err
}
return nil
}
func (e *Event) Find(ctx context.Context, eventID string) (*EventResponse, error) {
url, err := addOptions(e.generateUrl()+"/"+eventID, nil)
if err != nil {
return nil, err
}
respPtr := &EventResponse{}
err = getResource(ctx, e.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *Event) Replay(ctx context.Context, eventID string) error {
url, err := addOptions(e.generateUrl()+"/"+eventID+"/replay", nil)
if err != nil {
return err
}
err = putResource(ctx, e.client, url, nil, nil)
if err != nil {
return err
}
return nil
}
func (e *Event) BatchReplay(ctx context.Context, query *BatchReplayOptions) error {
url, err := addOptions(e.generateUrl()+"/batchreplay", query)
if err != nil {
return err
}
err = postJSON(ctx, e.client, url, nil, nil)
if err != nil {
return err
}
return nil
}
func (e *Event) generateUrl() string {
return fmt.Sprintf("%s/projects/%s/events", e.client.baseURL, e.client.projectID)
}