forked from jbogarin/go-cisco-webex-teams
/
webhooks_api.go
239 lines (189 loc) · 6.67 KB
/
webhooks_api.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
238
239
package webexteams
import (
"fmt"
"strings"
"time"
"github.com/go-resty/resty"
"github.com/google/go-querystring/query"
"github.com/peterhellberg/link"
)
// WebhooksService is the service to communicate with the Webhooks API endpoint
type WebhooksService service
// WebhookCreateRequest is the Webhook Create Request Parameters
type WebhookCreateRequest struct {
Name string `json:"name,omitempty"` // Webhook name.
TargetURL string `json:"targetUrl,omitempty"` // Webhook target URL.
Resource string `json:"resource,omitempty"` // Webhook resource.
Event string `json:"event,omitempty"` // Webhook event.
Filter string `json:"filter,omitempty"` // Webhook filter.
Secret string `json:"secret,omitempty"` // Webhook secret.
}
// WebhookUpdateRequest is the Update Webhook Request Parameters
type WebhookUpdateRequest struct {
Name string `json:"name,omitempty"` // Webhook name.
TargetURL string `json:"targetUrl,omitempty"` // Webhook target URL.
Secret string `json:"secret,omitempty"` // Webhook secret.
Status string `json:"status,omitempty"` // Webhook status.
}
// Webhook is the Webhook definition
type Webhook struct {
ID string `json:"id,omitempty"` // Webhook ID.
Name string `json:"name,omitempty"` // Webhook name.
TargetURL string `json:"targetUrl,omitempty"` // Webhook target URL.
Resource string `json:"resource,omitempty"` // Webhook resource.
Event string `json:"event,omitempty"` // Webhook event.
OrgID string `json:"orgId,omitempty"` // Webhook organization ID.
CreatedBy string `json:"createdBy,omitempty"` // Webhook created by Person ID.
AppID string `json:"appId,omitempty"` // Webhook application ID.
OwnedBy string `json:"ownedBy,omitempty"` // Webhook owner Person ID.
Filter string `json:"filter,omitempty"` // Webhook filter.
Status string `json:"status,omitempty"` // Webhook status.
Secret string `json:"secret,omitempty"` // Webhook secret.
Created time.Time `json:"created,omitempty"` // Webhook creation date/time.
}
// Webhooks is the List of Webhooks
type Webhooks struct {
Items []Webhook `json:"items,omitempty"`
}
// AddWebhook is used to append a webhook to a slice of webhooks
func (webhooks *Webhooks) AddWebhook(item Webhook) []Webhook {
webhooks.Items = append(webhooks.Items, item)
return webhooks.Items
}
func webhooksPagination(linkHeader string, size, max int) *Webhooks {
items := &Webhooks{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := RestyClient.R().
SetResult(&Webhooks{}).
Get(l.URI)
if err != nil {
return nil
}
items = response.Result().(*Webhooks)
if size != 0 {
size = size + len(items.Items)
if size < max {
webhooks := webhooksPagination(response.Header().Get("Link"), size, max)
for _, webhook := range webhooks.Items {
items.AddWebhook(webhook)
}
}
} else {
webhooks := webhooksPagination(response.Header().Get("Link"), size, max)
for _, webhook := range webhooks.Items {
items.AddWebhook(webhook)
}
}
}
}
return items
}
// CreateWebhook Creates a webhook.
/* Creates a webhook.
@param webhookCreateRequest
@return Webhook
*/
func (s *WebhooksService) CreateWebhook(webhookCreateRequest *WebhookCreateRequest) (*Webhook, *resty.Response, error) {
path := "/webhooks/"
response, err := RestyClient.R().
SetBody(webhookCreateRequest).
SetResult(&Webhook{}).
Post(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Webhook)
return result, response, err
}
// DeleteWebhook Deletes a webhook, by ID.
/* Deletes a webhook, by ID.
Specify the webhook ID in the webhookID parameter in the URI.
@param webhookID Webhook ID.
@return
*/
func (s *WebhooksService) DeleteWebhook(webhookID string) (*resty.Response, error) {
path := "/webhooks/{webhookId}"
path = strings.Replace(path, "{"+"webhookId"+"}", fmt.Sprintf("%v", webhookID), -1)
response, err := RestyClient.R().
Delete(path)
if err != nil {
return nil, err
}
return response, err
}
// GetWebhook Shows details for a webhook, by ID.
/* Shows details for a webhook, by ID.
Specify the webhook ID in the webhookID parameter in the URI.
@param webhookID Webhook ID.
@return Webhook
*/
func (s *WebhooksService) GetWebhook(webhookID string) (*Webhook, *resty.Response, error) {
path := "/webhooks/{webhookId}"
path = strings.Replace(path, "{"+"webhookId"+"}", fmt.Sprintf("%v", webhookID), -1)
response, err := RestyClient.R().
SetResult(&Webhook{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Webhook)
return result, response, err
}
// ListWebhooksQueryParams are the query params for the ListWebhooks API Call
type ListWebhooksQueryParams struct {
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response.
Paginate bool // Indicates if pagination is needed
}
// ListWebhooks Lists all of your webhooks.
/* Lists all of your webhooks.
@param "max" (int) Limit the maximum number of items in the response.
@param paginate (bool) indicates if pagination is needed
@return Webhooks
*/
func (s *WebhooksService) ListWebhooks(queryParams *ListWebhooksQueryParams) (*Webhooks, *resty.Response, error) {
path := "/webhooks/"
queryParamsString, _ := query.Values(queryParams)
response, err := RestyClient.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Webhooks{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Webhooks)
if queryParams.Paginate == true {
items := webhooksPagination(response.Header().Get("Link"), 0, 0)
for _, webhook := range items.Items {
result.AddWebhook(webhook)
}
} else {
if len(result.Items) < queryParams.Max {
items := webhooksPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, webhook := range items.Items {
result.AddWebhook(webhook)
}
}
}
return result, response, err
}
// UpdateWebhook Updates a webhook, by ID.
/* Updates a webhook, by ID.
Specify the webhook ID in the webhookID parameter in the URI.
@param webhookID Webhook ID.
@param webhookUpdateRequest
@return Webhook
*/
func (s *WebhooksService) UpdateWebhook(webhookID string, webhookUpdateRequest *WebhookUpdateRequest) (*Webhook, *resty.Response, error) {
path := "/webhooks/{webhookId}"
path = strings.Replace(path, "{"+"webhookId"+"}", fmt.Sprintf("%v", webhookID), -1)
response, err := RestyClient.R().
SetBody(webhookUpdateRequest).
SetResult(&Webhook{}).
Put(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Webhook)
return result, response, err
}