forked from VojtechVitek/go-trello
-
Notifications
You must be signed in to change notification settings - Fork 3
/
webhook.go
62 lines (50 loc) · 1.29 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
package trello
import (
"encoding/json"
"fmt"
"net/url"
)
type Webhook struct {
ID string `json:"id"`
Description string `json:"description"`
IDModel string `json:"idModel"`
CallbackURL string `json:"callbackURL"`
Active bool `json:"active"`
}
func (c *Client) Webhooks(token string) (webhooks []Webhook, err error) {
body, err := c.Get(webhookURL(token))
if err != nil {
return []Webhook{}, err
}
err = json.Unmarshal(body, &webhooks)
return
}
func (c *Client) CreateWebhook(hook Webhook) (webhook Webhook, err error) {
payload := url.Values{}
payload.Set("description", hook.Description)
payload.Set("callbackURL", hook.CallbackURL)
payload.Set("idModel", hook.IDModel)
body, err := c.Post("/webhooks/", payload)
if err != nil {
return Webhook{}, err
}
err = json.Unmarshal(body, &webhook)
return
}
func (c *Client) Webhook(webhookID string) (webhook Webhook, err error) {
url := fmt.Sprintf("/webhooks/%s/", webhookID)
body, err := c.Get(url)
if err != nil {
return
}
err = json.Unmarshal(body, &webhook)
return
}
func (c *Client) DeleteWebhook(webhookID string) (err error) {
url := fmt.Sprintf("/webhooks/%s/", webhookID)
_, err = c.Delete(url)
return
}
func webhookURL(token string) (url string) {
return fmt.Sprintf("/tokens/%s/webhooks/", token)
}