-
Notifications
You must be signed in to change notification settings - Fork 9
/
event_delivery.go
137 lines (111 loc) · 3.48 KB
/
event_delivery.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
package convoy_go
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
)
var (
ErrNotListEventDeliveryResponse = errors.New("invalid list event delivery response")
ErrNotEventDeliveryResponse = errors.New("invalid event delivery response")
)
type EventDelivery struct {
client *Client
}
type EventDeliveryResponse struct {
UID string `json:"uid"`
EventMetadata EventMetadata `json:"event_metadata"`
Metadata Metadata `json:"metadata"`
Description string `json:"description,omitempty"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type EventMetadata struct {
UID string `json:"uid"`
Name string `json:"name"`
}
type Metadata struct {
// Data to be sent to endpoint.
Data json.RawMessage `json:"data"`
Strategy string `json:"strategy"`
// NextSendTime denotes the next time a Event will be published in
// case it failed the first time
NextSendTime time.Time `json:"next_send_time"`
// NumTrials: number of times we have tried to deliver this Event to
// an application
NumTrials uint64 `json:"num_trials"`
IntervalSeconds uint64 `json:"interval_seconds"`
RetryLimit uint64 `json:"retry_limit"`
}
type ListEventDeliveryResponse struct {
Content []EventDeliveryResponse `json:"content"`
Pagination Pagination `json:"pagination"`
}
type EventDeliveryParams struct {
ListParams
EventType string `url:"eventType"`
EventID string `url:"eventId"`
Status []string `url:"status"`
EndpointID []string `url:"endpointId"`
SubscriptionID string `url:"subscriptionId"`
StartDate time.Time `url:"startDate" layout:"2006-01-02T15:04:05"`
EndDate time.Time `url:"endDate" layout:"2006-01-02T15:04:05"`
}
func newEventDelivery(client *Client) *EventDelivery {
return &EventDelivery{
client: client,
}
}
func (e *EventDelivery) All(ctx context.Context, query *EventDeliveryParams) (*ListEventDeliveryResponse, error) {
url, err := addOptions(e.generateUrl(), query)
if err != nil {
return nil, err
}
respPtr := &ListEventDeliveryResponse{}
err = getResource(ctx, e.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *EventDelivery) Find(ctx context.Context, eventDeliveryID string, query *EventDeliveryParams) (*EventDeliveryResponse, error) {
url, err := addOptions(e.generateUrl()+"/"+eventDeliveryID, query)
if err != nil {
return nil, err
}
respPtr := &EventDeliveryResponse{}
err = getResource(ctx, e.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *EventDelivery) Resend(ctx context.Context, eventDeliveryID string, query *EventDeliveryParams) (*EventDeliveryResponse, error) {
url, err := addOptions(e.generateUrl()+"/"+eventDeliveryID+"/resend", query)
if err != nil {
return nil, err
}
respPtr := &EventDeliveryResponse{}
err = putResource(ctx, e.client, url, nil, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *EventDelivery) BatchResend(ctx context.Context, query *EventDeliveryParams) error {
url, err := addOptions(e.generateUrl()+"/batchretry", query)
if err != nil {
return err
}
respPtr := &EventDeliveryResponse{}
err = putResource(ctx, e.client, url, nil, respPtr)
if err != nil {
return err
}
return nil
}
func (e *EventDelivery) generateUrl() string {
return fmt.Sprintf("%s/projects/%s/eventdeliveries", e.client.baseURL, e.client.projectID)
}