-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebhooks.go
91 lines (78 loc) · 3.15 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
86
87
88
89
90
91
package jwplatform
import (
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// WebhookResource is the resource that is returned for all Webhook resource requests,
// with the exception of the Create action, which extends this struct with upload-related data.
type WebhookResource struct {
V2ResourceResponse
Metadata WebhookMetadata `json:"metadata"`
}
// CreateWebhookResponse is the response structure for Webhook create calls.
//
// The Secret is returned only on Create calls and can be used to authenticate incoming webhooks
// Please see the documentation for additional details:
// https://developer.jwplayer.com/jwplayer/docs/learn-about-webhooks#section-verify-the-authenticity-of-a-webhook
type CreateWebhookResponse struct {
V2ResourceResponse
Metadata WebhookMetadata `json:"metadata"`
Secret string `json:"secret"`
}
// WebhookWriteRequest is the request structure required for Webhook create and update calls.
type WebhookWriteRequest struct {
Metadata WebhookMetadata `json:"metadata"`
}
// WebhookMetadata describes a Webhook resource
type WebhookMetadata struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Events []string `json:"events"`
Sites []string `json:"site_ids"`
WebhookURL string `json:"webhook_url"`
}
// WebhookResourcesResponse is the response structure for Webhook list calls.
type WebhookResourcesResponse struct {
V2ResourcesResponse
Webhooks []WebhookResource `json:"webhooks"`
}
// WebhooksClient for interacting with V2 Webhooks API.
type WebhooksClient struct {
v2Client *V2Client
}
// Get a single Webhook resource by ID.
func (c *WebhooksClient) Get(webhookID string) (*WebhookResource, error) {
webhook := &WebhookResource{}
path := fmt.Sprintf("/v2/webhooks/%s", webhookID)
err := c.v2Client.Request(http.MethodGet, path, webhook, nil, nil)
return webhook, err
}
// Create a Webhook resource.
func (c *WebhooksClient) Create(webhookMetadata *WebhookMetadata) (*CreateWebhookResponse, error) {
createRequestData := &WebhookWriteRequest{Metadata: *webhookMetadata}
webhook := &CreateWebhookResponse{}
err := c.v2Client.Request(http.MethodPost, "/v2/webhooks", webhook, createRequestData, nil)
return webhook, err
}
// List all Webhook resources.
func (c *WebhooksClient) List(queryParams *QueryParams) (*WebhookResourcesResponse, error) {
webhooks := &WebhookResourcesResponse{}
urlValues, _ := query.Values(queryParams)
err := c.v2Client.Request(http.MethodGet, "/v2/webhooks", webhooks, nil, urlValues)
return webhooks, err
}
// Update a Webhook resource by ID.
func (c *WebhooksClient) Update(webhookID string, webhookMetadata *WebhookMetadata) (*WebhookResource, error) {
updateRequestData := &WebhookWriteRequest{Metadata: *webhookMetadata}
webhook := &WebhookResource{}
path := fmt.Sprintf("/v2/webhooks/%s", webhookID)
err := c.v2Client.Request(http.MethodPatch, path, webhook, updateRequestData, nil)
return webhook, err
}
// Delete a Webhook resource by ID.
func (c *WebhooksClient) Delete(webhookID string) error {
path := fmt.Sprintf("/v2/webhooks/%s", webhookID)
err := c.v2Client.Request(http.MethodDelete, path, nil, nil, nil)
return err
}