This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
webhooks.go
85 lines (68 loc) · 1.73 KB
/
webhooks.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
package moneybird
import "encoding/json"
//"time"
// Webhook is a MoneyBird webhook
type Webhook struct {
ID string `json:"id,omitempty"`
AdministrationID string `json:"administration_id,omitempty"`
URL string `json:"url"`
LastHTTPStatus string `json:"last_http_status,omitempty"`
LastHTTPBody string `json:"last_http_body,omitempty"`
}
// WebhookGateway encapsulates all /webhooks related endpoints
type WebhookGateway struct {
*Client
}
// Webhook returns a new gateway instance
func (c *Client) Webhook() *WebhookGateway {
return &WebhookGateway{c}
}
// List returns all webhooks in Moneybird
func (c *WebhookGateway) List() ([]*Webhook, error) {
var webhooks []*Webhook
var err error
res, err := c.execute("GET", "webhooks", nil)
if err != nil {
return nil, err
}
switch res.StatusCode {
case 200:
err = json.NewDecoder(res.Body).Decode(&webhooks)
return webhooks, err
}
return nil, res.error()
}
// Create adds a webhook to MoneyBird
func (c *WebhookGateway) Create(webhook *Webhook) (*Webhook, error) {
data, err := json.Marshal(webhook)
if err != nil {
return nil, err
}
// webhooks do not use an envelope so we can't use c.execute here... bummer
req, err := c.newRequest("POST", "webhooks", data)
if err != nil {
return nil, err
}
httpRes, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
res := &Response{httpRes}
switch res.StatusCode {
case 201:
return res.webhook()
}
return nil, res.error()
}
// Delete the given webhook
func (c *WebhookGateway) Delete(webhook *Webhook) error {
res, err := c.execute("DELETE", "webhooks/"+webhook.ID, nil)
if err != nil {
return err
}
switch res.StatusCode {
case 204:
return nil
}
return res.error()
}