forked from frain-dev/convoy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
207 lines (167 loc) · 5.11 KB
/
endpoint.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package convoy_go
import (
"context"
"errors"
"fmt"
"time"
)
var (
ErrNotListEndpointResponse = errors.New("invalid list endpoint response")
ErrNotEndpointResponse = errors.New("invalid endpoint response")
)
type Endpoint struct {
client *Client
}
type CreateEndpointRequest struct {
Name string `json:"name"`
SupportEmail string `json:"support_email"`
OwnerID string `json:"owner_id"`
SlackWebhookUrl string `json:"slack_webhook_url"`
URL string `json:"url"`
Secret string `json:"secret,omitempty"`
Description string `json:"description,omitempty"`
AdvancedSignatures *bool `json:"advanced_signatures"`
IsDisabled bool `json:"is_disabled"`
Authentication *EndpointAuth `json:"authentication"`
HttpTimeout string `json:"http_timeout,omitempty"`
RateLimit int `json:"rate_limit,omitempty"`
RateLimitDuration string `json:"rate_limit_duration,omitempty"`
}
type EndpointResponse struct {
UID string `json:"uid"`
GroupID string `json:"group_id"`
OwnerID string `json:"owner_id"`
TargetUrl string `json:"target_url"`
Title string `json:"title"`
Description string `json:"description"`
Status string `json:"status"`
Secrets []Secret `json:"secrets"`
AdvancedSignatures bool `json:"advanced_signatures"`
SlackWebhookUrl string `json:"slack_webhook_url"`
SupportEmail string `json:"support_email"`
IsDisabled bool `json:"is_disabled"`
HttpTimeout string `json:"http_timeout"`
RateLimit int `json:"rate_limit"`
RateLimitDuration string `json:"rate_limit_duration"`
Authentication *EndpointAuth `json:"authentication"`
Events int64 `json:"events"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type EndpointAuth struct {
Type string `json:"type"`
ApiKey *ApiKeyAuth `json:"api_key"`
}
type ApiKeyAuth struct {
HeaderValue string `json:"header_value"`
HeaderName string `json:"header_name"`
}
type Secret struct {
UID string `json:"uid" bson:"uid"`
Value string `json:"value" bson:"value"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
}
type ListEndpointResponse struct {
Content []EndpointResponse `json:"content"`
Pagination Pagination `json:"pagination"`
}
type EndpointParams struct {
ListParams
Query string `url:"query"`
OwnerID string `url:"ownerId"`
}
type RollSecretRequest struct {
Expiration int `json:"expiration"`
Secret string `json:"secret"`
}
func newEndpoint(client *Client) *Endpoint {
return &Endpoint{
client: client,
}
}
func (e *Endpoint) All(ctx context.Context, query *EndpointParams) (*ListEndpointResponse, error) {
url, err := addOptions(e.generateUrl(), query)
if err != nil {
return nil, err
}
respPtr := &ListEndpointResponse{}
err = getResource(ctx, e.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *Endpoint) Create(ctx context.Context, body *CreateEndpointRequest, query *EndpointParams) (*EndpointResponse, error) {
url, err := addOptions(e.generateUrl(), query)
if err != nil {
return nil, err
}
respPtr := &EndpointResponse{}
err = postJSON(ctx, e.client, url, body, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *Endpoint) Find(ctx context.Context, endpointID string, query *EndpointParams) (*EndpointResponse, error) {
url, err := addOptions(e.generateUrl()+"/"+endpointID, query)
if err != nil {
return nil, err
}
respPtr := &EndpointResponse{}
err = getResource(ctx, e.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *Endpoint) Update(ctx context.Context, endpointID string, body *CreateEndpointRequest, query *EndpointParams) (*EndpointResponse, error) {
url, err := addOptions(e.generateUrl()+"/"+endpointID, query)
if err != nil {
return nil, err
}
respPtr := &EndpointResponse{}
err = putResource(ctx, e.client, url, body, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e *Endpoint) Delete(ctx context.Context, endpointID string, query *EndpointParams) error {
url, err := addOptions(e.generateUrl()+"/"+endpointID, query)
if err != nil {
return err
}
err = deleteResource(ctx, e.client, url, nil)
if err != nil {
return err
}
return nil
}
func (e *Endpoint) Pause(ctx context.Context, Id string) (*EndpointResponse, error) {
url, err := addOptions(e.generateUrl()+"/"+Id+"/pause", nil)
if err != nil {
return nil, err
}
respPtr := &EndpointResponse{}
err = putResource(ctx, e.client, url, nil, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (e Endpoint) RollSecret(ctx context.Context, Id string, body *RollSecretRequest) error {
url, err := addOptions(e.generateUrl()+"/"+Id+"/expire_secret", nil)
if err != nil {
return err
}
respPtr := &EndpointResponse{}
err = putResource(ctx, e.client, url, body, respPtr)
if err != nil {
return err
}
return nil
}
func (e *Endpoint) generateUrl() string {
return fmt.Sprintf("%s/projects/%s/endpoints", e.client.baseURL, e.client.projectID)
}