This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
webhook.go
117 lines (93 loc) · 2.8 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
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)
// WebhooksService service
type WebhooksService service
// Webhook model
type Webhook struct {
Sys *Sys `json:"sys,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Topics []string `json:"topics,omitempty"`
HTTPBasicUsername string `json:"httpBasicUsername,omitempty"`
HTTPBasicPassword string `json:"httpBasicPassword,omitempty"`
Headers []*WebhookHeader `json:"headers,omitempty"`
}
// WebhookHeader model
type WebhookHeader struct {
Key string `json:"key"`
Value string `json:"value"`
}
// GetVersion returns entity version
func (webhook *Webhook) GetVersion() int {
version := 1
if webhook.Sys != nil {
version = webhook.Sys.Version
}
return version
}
// List returns webhooks collection
func (service *WebhooksService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/webhook_definitions", spaceID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return &Collection{}
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single webhook entity
func (service *WebhooksService) Get(spaceID, webhookID string) (*Webhook, error) {
path := fmt.Sprintf("/spaces/%s/webhook_definitions/%s", spaceID, webhookID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil, err
}
var webhook Webhook
if err := service.c.do(req, &webhook); err != nil {
return nil, err
}
return &webhook, nil
}
// Upsert updates or creates a new entity
func (service *WebhooksService) Upsert(spaceID string, webhook *Webhook) error {
bytesArray, err := json.Marshal(webhook)
if err != nil {
return err
}
var path string
var method string
if webhook.Sys != nil && webhook.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s/webhook_definitions/%s", spaceID, webhook.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/webhook_definitions", spaceID)
method = "POST"
}
req, err := service.c.newRequest(method, path, nil, bytes.NewReader(bytesArray))
if err != nil {
return err
}
req.Header.Set("X-Contentful-Version", strconv.Itoa(webhook.GetVersion()))
return service.c.do(req, webhook)
}
// Delete the webhook
func (service *WebhooksService) Delete(spaceID string, webhook *Webhook) error {
path := fmt.Sprintf("/spaces/%s/webhook_definitions/%s", spaceID, webhook.Sys.ID)
method := "DELETE"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
version := strconv.Itoa(webhook.Sys.Version)
req.Header.Set("X-Contentful-Version", version)
return service.c.do(req, nil)
}