forked from adlio/trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
111 lines (98 loc) · 2.71 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
// Copyright © 2016 Aaron Longwell
//
// Use of this source code is governed by an MIT licese.
// Details in the LICENSE file.
package trello
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// Webhook is the Go representation of a webhook registered in Trello's systems.
// Used when creating, modifying or deleting webhooks.
//
type Webhook struct {
client *Client
ID string `json:"id,omitempty"`
IDModel string `json:"idModel"`
Description string `json:"description"`
CallbackURL string `json:"callbackURL"`
Active bool `json:"active"`
}
// BoardWebhookRequest is the object sent by Trello to a Webhook for Board-triggered
// webhooks.
//
type BoardWebhookRequest struct {
Model *Board
Action *Action
}
// ListWebhookRequest is the object sent by Trello to a Webhook for List-triggered
// webhooks.
//
type ListWebhookRequest struct {
Model *List
Action *Action
}
// CardWebhookRequest is the object sent by Trello to a Webhook for Card-triggered
// webhooks.
//
type CardWebhookRequest struct {
Model *Card
Action *Action
}
func (c *Client) CreateWebhook(webhook *Webhook) error {
path := "webhooks"
args := Arguments{"idModel": webhook.IDModel, "description": webhook.Description, "callbackURL": webhook.CallbackURL}
err := c.Post(path, args, webhook)
if err == nil {
webhook.client = c
}
return err
}
func (c *Client) GetWebhook(webhookID string, args Arguments) (webhook *Webhook, err error) {
path := fmt.Sprintf("webhooks/%s", webhookID)
err = c.Get(path, args, &webhook)
if webhook != nil {
webhook.client = c
}
return
}
func (t *Token) GetWebhooks(args Arguments) (webhooks []*Webhook, err error) {
path := fmt.Sprintf("tokens/%s/webhooks", t.ID)
err = t.client.Get(path, args, &webhooks)
return
}
func GetBoardWebhookRequest(r *http.Request) (whr *BoardWebhookRequest, err error) {
if r.Method == "HEAD" {
return &BoardWebhookRequest{}, nil
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&whr)
if err != nil {
err = errors.Wrapf(err, "GetBoardWebhookRequest() failed to decode '%s'.", r.URL)
}
return
}
func GetListWebhookRequest(r *http.Request) (whr *ListWebhookRequest, err error) {
if r.Method == "HEAD" {
return &ListWebhookRequest{}, nil
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&whr)
if err != nil {
err = errors.Wrapf(err, "GetListWebhookRequest() failed to decode '%s'.", r.URL)
}
return
}
func GetCardWebhookRequest(r *http.Request) (whr *CardWebhookRequest, err error) {
if r.Method == "HEAD" {
return &CardWebhookRequest{}, nil
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&whr)
if err != nil {
err = errors.Wrapf(err, "GetCardWebhookRequest() failed to decode '%s'.", r.URL)
}
return
}