-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhooks_controller.go
237 lines (219 loc) · 10.8 KB
/
webhooks_controller.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Package advancedbilling
This file was automatically generated for Maxio by APIMATIC v3.0 ( https://www.apimatic.io ).
*/
package advancedbilling
import (
"context"
"fmt"
"github.com/apimatic/go-core-runtime/https"
"github.com/apimatic/go-core-runtime/utilities"
"github.com/maxio-com/ab-golang-sdk/errors"
"github.com/maxio-com/ab-golang-sdk/models"
)
// WebhooksController represents a controller struct.
type WebhooksController struct {
baseController
}
// NewWebhooksController creates a new instance of WebhooksController.
// It takes a baseController as a parameter and returns a pointer to the WebhooksController.
func NewWebhooksController(baseController baseController) *WebhooksController {
webhooksController := WebhooksController{baseController: baseController}
return &webhooksController
}
// ListWebhooksInput represents the input of the ListWebhooks endpoint.
type ListWebhooksInput struct {
// Webhooks with matching status would be returned.
Status *models.WebhookStatus
// Format YYYY-MM-DD. Returns Webhooks with the created_at date greater than or equal to the one specified.
SinceDate *string
// Format YYYY-MM-DD. Returns Webhooks with the created_at date less than or equal to the one specified.
UntilDate *string
// Result records are organized in pages. By default, the first page of results is displayed. The page parameter specifies a page number of results to fetch. You can start navigating through the pages to consume the results. You do this by passing in a page parameter. Retrieve the next page by adding ?page=2 to the query string. If there are no results to return, then an empty result set will be returned.
// Use in query `page=1`.
Page *int
// This parameter indicates how many records to fetch in each request. Default value is 20. The maximum allowed values is 200; any per_page value over 200 will be changed to 200.
// Use in query `per_page=200`.
PerPage *int
// The order in which the Webhooks are returned.
Order *models.WebhookOrder
// The Advanced Billing id of a subscription you'd like to filter for
Subscription *int
}
// ListWebhooks takes context, status, sinceDate, untilDate, page, perPage, order, subscription as parameters and
// returns an models.ApiResponse with []models.WebhookResponse data and
// an error if there was an issue with the request or response.
// ## Webhooks Intro
// The Webhooks API allows you to view a list of all webhooks and to selectively resend individual or groups of webhooks. Webhooks will be sent on endpoints specified by you. Endpoints can be added via API or Web UI. There is also an option to enable / disable webhooks via API request.
// We recommend that you review Advanced Billing's webhook documentation located in our help site. The following resources will help guide you on how to use webhooks in Advanced Billing, in addition to these webhook endpoints:
// + [Adding/editing new webhooks](https://maxio.zendesk.com/hc/en-us/articles/24286723085197-Webhooks#configure-webhook-url)
// + [Webhooks introduction and delivery information](https://maxio.zendesk.com/hc/en-us/articles/24266143173901-Webhooks-Overview)
// + [Main webhook reference](https://maxio.zendesk.com/hc/en-us/articles/24266136649869-Webhooks-Reference)
// + [Available webhooks and payloads](https://maxio.zendesk.com/hc/en-us/articles/24266136649869-Webhooks-Reference#events)
// ## List Webhooks for a Site
// This method allows you to fetch data about webhooks. You can pass query parameters if you want to filter webhooks.
func (w *WebhooksController) ListWebhooks(
ctx context.Context,
input ListWebhooksInput) (
models.ApiResponse[[]models.WebhookResponse],
error) {
req := w.prepareRequest(ctx, "GET", "/webhooks.json")
req.Authenticate(NewAuth("BasicAuth"))
if input.Status != nil {
req.QueryParam("status", *input.Status)
}
if input.SinceDate != nil {
req.QueryParam("since_date", *input.SinceDate)
}
if input.UntilDate != nil {
req.QueryParam("until_date", *input.UntilDate)
}
if input.Page != nil {
req.QueryParam("page", *input.Page)
}
if input.PerPage != nil {
req.QueryParam("per_page", *input.PerPage)
}
if input.Order != nil {
req.QueryParam("order", *input.Order)
}
if input.Subscription != nil {
req.QueryParam("subscription", *input.Subscription)
}
var result []models.WebhookResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.WebhookResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// EnableWebhooks takes context, body as parameters and
// returns an models.ApiResponse with models.EnableWebhooksResponse data and
// an error if there was an issue with the request or response.
// This method allows you to enable webhooks via API for your site
func (w *WebhooksController) EnableWebhooks(
ctx context.Context,
body *models.EnableWebhooksRequest) (
models.ApiResponse[models.EnableWebhooksResponse],
error) {
req := w.prepareRequest(ctx, "PUT", "/webhooks/settings.json")
req.Authenticate(NewAuth("BasicAuth"))
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.EnableWebhooksResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.EnableWebhooksResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ReplayWebhooks takes context, body as parameters and
// returns an models.ApiResponse with models.ReplayWebhooksResponse data and
// an error if there was an issue with the request or response.
// Posting to the replay endpoint does not immediately resend the webhooks. They are added to a queue and will be sent as soon as possible, depending on available system resources.
// You may submit an array of up to 1000 webhook IDs to replay in the request.
func (w *WebhooksController) ReplayWebhooks(
ctx context.Context,
body *models.ReplayWebhooksRequest) (
models.ApiResponse[models.ReplayWebhooksResponse],
error) {
req := w.prepareRequest(ctx, "POST", "/webhooks/replay.json")
req.Authenticate(NewAuth("BasicAuth"))
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.ReplayWebhooksResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.ReplayWebhooksResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// CreateEndpoint takes context, body as parameters and
// returns an models.ApiResponse with models.EndpointResponse data and
// an error if there was an issue with the request or response.
// The Chargify API allows you to create an endpoint and assign a list of webhooks subscriptions (events) to it.
// You can check available events here.
// [Event keys](https://maxio.zendesk.com/hc/en-us/articles/24266136649869-Webhooks-Reference#events)
func (w *WebhooksController) CreateEndpoint(
ctx context.Context,
body *models.CreateOrUpdateEndpointRequest) (
models.ApiResponse[models.EndpointResponse],
error) {
req := w.prepareRequest(ctx, "POST", "/endpoints.json")
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewErrorListResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.EndpointResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.EndpointResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// ListEndpoints takes context as parameters and
// returns an models.ApiResponse with []models.Endpoint data and
// an error if there was an issue with the request or response.
// This method returns created endpoints for site.
func (w *WebhooksController) ListEndpoints(ctx context.Context) (
models.ApiResponse[[]models.Endpoint],
error) {
req := w.prepareRequest(ctx, "GET", "/endpoints.json")
req.Authenticate(NewAuth("BasicAuth"))
var result []models.Endpoint
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.Endpoint](decoder)
return models.NewApiResponse(result, resp), err
}
// UpdateEndpoint takes context, endpointId, body as parameters and
// returns an models.ApiResponse with models.EndpointResponse data and
// an error if there was an issue with the request or response.
// You can update an Endpoint via the API with a PUT request to the resource endpoint.
// You can change the `url` of your endpoint which consumes webhooks or list of `webhook_subscriptions`.
// Check available [Event keys](https://maxio.zendesk.com/hc/en-us/articles/24266136649869-Webhooks-Reference#events).
// Always send a complete list of events which you want subscribe/watch.
// Sending an PUT request for existing endpoint with empty list of `webhook_subscriptions` will end with unsubscribe from all events.
// If you want unsubscribe from specific event, just send a list of `webhook_subscriptions` without the specific event key.
func (w *WebhooksController) UpdateEndpoint(
ctx context.Context,
endpointId int,
body *models.CreateOrUpdateEndpointRequest) (
models.ApiResponse[models.EndpointResponse],
error) {
req := w.prepareRequest(
ctx,
"PUT",
fmt.Sprintf("/endpoints/%v.json", endpointId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewErrorListResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result models.EndpointResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.EndpointResponse](decoder)
return models.NewApiResponse(result, resp), err
}