-
Notifications
You must be signed in to change notification settings - Fork 7
/
webhook.go
188 lines (156 loc) · 5.58 KB
/
webhook.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
package aapi
import (
"fmt"
"net/http"
)
// WebhookService handles requests to outgoing webhook endpoint
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
type WebhookService struct {
client *Client
url string
}
// NewWebhookService creates WebhookService with defined url
func NewWebhookService(client *Client) *WebhookService {
WebhookService := WebhookService{}
WebhookService.client = client
WebhookService.url = "webhooks"
return &WebhookService
}
type PaginatedWebhooksResponse struct {
PaginatedResponse
Webhooks []*Webhook `json:"results"`
}
type Webhook struct {
ID string `json:"id"`
Name string `json:"name"`
Team string `json:"team"`
Url string `json:"url"`
TriggerType string `json:"trigger_type"`
HttpMethod string `json:"http_method"`
Data *string `json:"data"`
Username *string `json:"username"`
Password *string `json:"password"`
AuthorizationHeader *string `json:"authorization_header"`
TriggerTemplate *string `json:"trigger_template"`
Headers *string `json:"headers"`
ForwardAll bool `json:"forward_all"`
IntegrationFilter *[]string `json:"integration_filter"`
IsWebhookEnabled bool `json:"is_webhook_enabled"`
}
type ListWebhookOptions struct {
ListOptions
Name string `url:"name,omitempty" json:"name,omitempty"`
}
// ListWebhooks fetches all Webhooks for authorized organization
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/#list-actions
func (service *WebhookService) ListWebhooks(opt *ListWebhookOptions) (*PaginatedWebhooksResponse, *http.Response, error) {
u := fmt.Sprintf("%s", service.url)
req, err := service.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}
var Webhooks *PaginatedWebhooksResponse
resp, err := service.client.Do(req, &Webhooks)
if err != nil {
return nil, resp, err
}
return Webhooks, resp, err
}
type GetWebhookOptions struct {
}
// GetWebhook fetches webhook by given id.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) GetWebhook(id string, opt *GetWebhookOptions) (*Webhook, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("GET", u, opt)
if err != nil {
return nil, nil, err
}
Webhook := new(Webhook)
resp, err := service.client.Do(req, Webhook)
if err != nil {
return nil, resp, err
}
return Webhook, resp, err
}
type CreateWebhookOptions struct {
Name string `json:"name"`
Team string `json:"team"`
Url string `json:"url"`
TriggerType string `json:"trigger_type"`
HttpMethod string `json:"http_method"`
Data *string `json:"data"`
Username *string `json:"username"`
Password *string `json:"password"`
AuthorizationHeader *string `json:"authorization_header"`
TriggerTemplate *string `json:"trigger_template"`
Headers *string `json:"headers"`
ForwardAll bool `json:"forward_all"`
IntegrationFilter *[]string `json:"integration_filter"`
IsWebhookEnabled bool `json:"is_webhook_enabled"`
}
// CreateWebhook creates webhook
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) CreateWebhook(opt *CreateWebhookOptions) (*Webhook, *http.Response, error) {
u := fmt.Sprintf("%s/", service.url)
req, err := service.client.NewRequest("POST", u, opt)
if err != nil {
return nil, nil, err
}
Webhook := new(Webhook)
resp, err := service.client.Do(req, Webhook)
if err != nil {
return nil, resp, err
}
return Webhook, resp, err
}
type UpdateWebhookOptions struct {
Name string `json:"name"`
Team string `json:"team"`
Url string `json:"url"`
TriggerType string `json:"trigger_type"`
HttpMethod string `json:"http_method"`
Data *string `json:"data"`
Username *string `json:"username"`
Password *string `json:"password"`
AuthorizationHeader *string `json:"authorization_header"`
TriggerTemplate *string `json:"trigger_template"`
Headers *string `json:"headers"`
ForwardAll bool `json:"forward_all"`
IntegrationFilter *[]string `json:"integration_filter"`
IsWebhookEnabled bool `json:"is_webhook_enabled"`
}
// UpdateWebhook updates webhook
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) UpdateWebhook(id string, opt *UpdateWebhookOptions) (*Webhook, *http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, nil, err
}
Webhook := new(Webhook)
resp, err := service.client.Do(req, Webhook)
if err != nil {
return nil, resp, err
}
return Webhook, resp, err
}
type DeleteWebhookOptions struct {
}
// DeleteWebhook deletes webhook.
//
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/
func (service *WebhookService) DeleteWebhook(id string, opt *DeleteWebhookOptions) (*http.Response, error) {
u := fmt.Sprintf("%s/%s/", service.url, id)
req, err := service.client.NewRequest("DELETE", u, opt)
if err != nil {
return nil, err
}
resp, err := service.client.Do(req, nil)
return resp, err
}