forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room_webhook.go
112 lines (94 loc) · 2.65 KB
/
room_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
// handling Webhook data
package hipchat
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
// Response Types
type WebhookLinks struct {
Links
}
type Webhook struct {
WebhookLinks WebhookLinks `json:"links"`
Name string `json:"name"`
Event string `json:"event"`
Pattern string `json:"pattern"`
URL string `json:"url"`
ID int `json:"id,omitempty"`
}
type WebhookList struct {
Webhooks []Webhook `json:"items"`
StartIndex int `json:"startIndex"`
MaxResults int `json:"maxResults"`
Links PageLinks `json:"links"`
}
// Request Types
type ListWebhooksRequest struct {
MaxResults int `json:"max-results"`
StartIndex int `json:"start-index"`
}
type CreateWebhookRequest struct {
Name string `json:"name"`
Event string `json:"event"`
Pattern string `json:"pattern"`
URL string `json:"url"`
}
// ListWebhooks returns all the webhooks for a given room.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/get_all_webhooks
func (r *RoomService) ListWebhooks(id interface{}, roomReq *ListWebhooksRequest) (*WebhookList, *http.Response, error) {
u := fmt.Sprintf("room/%v/webhook", id)
if roomReq != nil {
p := url.Values{}
if roomReq.MaxResults != 0 {
p.Add("max-results", strconv.FormatInt(int64(roomReq.MaxResults), 10))
}
if roomReq.StartIndex != 0 {
p.Add("start-index", strconv.FormatInt(int64(roomReq.StartIndex), 10))
}
if len(p) > 0 {
u += "?" + p.Encode()
}
}
req, err := r.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
whList := new(WebhookList)
resp, err := r.client.Do(req, whList)
if err != nil {
return nil, resp, err
}
return whList, resp, nil
}
// DeleteWebhook removes the given webhook.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/delete_webhook
func (r *RoomService) DeleteWebhook(id interface{}, webhookId interface{}) (*http.Response, error) {
req, err := r.client.NewRequest("DELETE", fmt.Sprintf("room/%v/webhook/%v", id, webhookId), nil)
if err != nil {
return nil, err
}
resp, err := r.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// CreateWebhook creates a new webhook.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/create_webhook
func (r *RoomService) CreateWebhook(id interface{}, roomReq *CreateWebhookRequest) (*Webhook, *http.Response, error) {
req, err := r.client.NewRequest("POST", fmt.Sprintf("room/%v/webhook", id), roomReq)
if err != nil {
return nil, nil, err
}
wh := new(Webhook)
resp, err := r.client.Do(req, wh)
if err != nil {
return nil, resp, err
}
return wh, resp, nil
}