forked from frain-dev/convoy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
153 lines (122 loc) · 3.63 KB
/
subscription.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
package convoy_go
import (
"context"
"errors"
"fmt"
"time"
)
var (
ErrNotListSubscriptionResponse = errors.New("invalid list subscription response")
ErrNotSubscriptionResponse = errors.New("invalid subscription response")
)
type Subscription struct {
client *Client
}
type CreateSubscriptionRequest struct {
Name string `json:"name"`
SourceID string `json:"source_id"`
EndpointID string `json:"endpoint_id"`
AlertConfig *AlertConfiguration `json:"alert_config"`
RetryConfig *RetryConfiguration `json:"retry_config"`
FilterConfig *FilterConfiguration `json:"filter_config"`
}
type AlertConfiguration struct {
Count int `json:"count"`
Threshold string `json:"threshold"`
}
type RetryConfiguration struct {
Type string `json:"type"`
Duration string `json:"duration"`
RetryCount int `json:"retry_count"`
}
type FilterConfiguration struct {
EventTypes []string `json:"event_types" bson:"event_types,omitempty"`
}
type SubscriptionResponse struct {
UID string `json:"uid"`
Name string `json:"name"`
Type string `json:"type"`
Status string `json:"status"`
Source *SourceResponse `json:"source_metadata,omitempty"`
// subscription config
AlertConfig *AlertConfiguration `json:"alert_config,omitempty"`
RetryConfig *RetryConfiguration `json:"retry_config,omitempty"`
FilterConfig *FilterConfiguration `json:"filter_config,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
type SubscriptionParams struct {
ListParams
EndpointID []string `url:"endpointId"`
}
type ListSubscriptionResponse struct {
Content []SubscriptionResponse `json:"content"`
Pagination Pagination `json:"pagination"`
}
func newSubscription(client *Client) *Subscription {
return &Subscription{
client: client,
}
}
func (s *Subscription) All(ctx context.Context, query *SubscriptionParams) (*ListSubscriptionResponse, error) {
url, err := addOptions(s.generateUrl(), query)
if err != nil {
return nil, err
}
respPtr := &ListSubscriptionResponse{}
err = getResource(ctx, s.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Subscription) Create(ctx context.Context, body *CreateSubscriptionRequest) (*SubscriptionResponse, error) {
url, err := addOptions(s.generateUrl(), nil)
if err != nil {
return nil, err
}
respPtr := &SubscriptionResponse{}
err = postJSON(ctx, s.client, url, body, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Subscription) Find(ctx context.Context, subscriptionId string) (*SubscriptionResponse, error) {
url, err := addOptions(s.generateUrl()+"/"+subscriptionId, nil)
if err != nil {
return nil, err
}
respPtr := &SubscriptionResponse{}
err = getResource(ctx, s.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Subscription) Update(ctx context.Context, subscriptionId string, body *CreateSubscriptionRequest) (*SubscriptionResponse, error) {
url, err := addOptions(s.generateUrl()+"/"+subscriptionId, nil)
if err != nil {
return nil, err
}
respPtr := &SubscriptionResponse{}
err = putResource(ctx, s.client, url, body, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (s *Subscription) Delete(ctx context.Context, subscriptionId string) error {
url, err := addOptions(s.generateUrl()+"/"+subscriptionId, nil)
if err != nil {
return err
}
err = deleteResource(ctx, s.client, url, nil)
if err != nil {
return err
}
return nil
}
func (s *Subscription) generateUrl() string {
return fmt.Sprintf("%s/projects/%s/subscriptions", s.client.baseURL, s.client.projectID)
}