-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathwebhook.go
139 lines (118 loc) · 4.2 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package zendesk
import (
"context"
"encoding/json"
"fmt"
"time"
)
// Webhook is struct for webhook payload.
// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/
type Webhook struct {
Authentication *WebhookAuthentication `json:"authentication,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
CreatedBy string `json:"created_by,omitempty"`
Description string `json:"description,omitempty"`
Endpoint string `json:"endpoint"`
ExternalSource interface{} `json:"external_source,omitempty"`
HTTPMethod string `json:"http_method"`
ID string `json:"id,omitempty"`
Name string `json:"name"`
RequestFormat string `json:"request_format"`
SigningSecret *WebhookSigningSecret `json:"signing_secret,omitempty"`
Status string `json:"status"`
Subscriptions []string `json:"subscriptions,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
UpdatedBy string `json:"updated_by,omitempty"`
}
type WebhookAuthentication struct {
Type string `json:"type"`
Data interface{} `json:"data"`
AddPosition string `json:"add_position"`
}
type WebhookSigningSecret struct {
Algorithm string `json:"algorithm"`
Secret string `json:"secret"`
}
type WebhookAPI interface {
CreateWebhook(ctx context.Context, hook *Webhook) (*Webhook, error)
GetWebhook(ctx context.Context, webhookID string) (*Webhook, error)
UpdateWebhook(ctx context.Context, webhookID string, hook *Webhook) error
DeleteWebhook(ctx context.Context, webhookID string) error
GetWebhookSigningSecret(ctx context.Context, webhookID string) (*WebhookSigningSecret, error)
}
// CreateWebhook creates new webhook.
//
// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#create-or-clone-webhook
func (z *Client) CreateWebhook(ctx context.Context, hook *Webhook) (*Webhook, error) {
var data, result struct {
Webhook *Webhook `json:"webhook"`
}
data.Webhook = hook
body, err := z.post(ctx, "/webhooks", data)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result.Webhook, nil
}
// GetWebhook gets a specified webhook.
//
// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#show-webhook
func (z *Client) GetWebhook(ctx context.Context, webhookID string) (*Webhook, error) {
var result struct {
Webhook *Webhook `json:"webhook"`
}
body, err := z.get(ctx, fmt.Sprintf("/webhooks/%s", webhookID))
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result.Webhook, nil
}
// UpdateWebhook updates a webhook with the specified webhook.
//
// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#update-webhook
func (z *Client) UpdateWebhook(ctx context.Context, webhookID string, hook *Webhook) error {
var data struct {
Webhook *Webhook `json:"webhook"`
}
data.Webhook = hook
_, err := z.put(ctx, fmt.Sprintf("/webhooks/%s", webhookID), data)
if err != nil {
return err
}
return nil
}
// DeleteWebhook deletes the specified webhook.
//
// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#delete-webhook
func (z *Client) DeleteWebhook(ctx context.Context, webhookID string) error {
err := z.delete(ctx, fmt.Sprintf("/webhooks/%s", webhookID))
if err != nil {
return err
}
return nil
}
// GetWebhookSigningSecret gets the signing secret of specified webhook.
//
// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#show-webhook-signing-secret
func (z *Client) GetWebhookSigningSecret(ctx context.Context, webhookID string) (*WebhookSigningSecret, error) {
var result struct {
SigningSecret *WebhookSigningSecret `json:"signing_secret"`
}
body, err := z.get(ctx, fmt.Sprintf("/webhooks/%s/signing_secret", webhookID))
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result.SigningSecret, nil
}