Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions fixture/GET/webhooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"links": {
"next": "https://example.zendesk.com/api/v2/webhooks?page[size]=100&page[after]=ijkl",
"prev": "https://example.zendesk.com/api/v2/webhooks?page[size]=100&page[before]=abcd"
},
"meta": {
"after_cursor": "ijkl",
"before_cursor": "abcd",
"has_more": true
},
"webhooks": [
{
"authentication": {
"add_position": "header",
"data": {
"username": "example_username"
},
"type": "basic_auth"
},
"created_at": "2020-10-20T08:16:28Z",
"created_by": "1234567",
"custom_headers": {
"header-one": "value_one",
"header-two": "value_two"
},
"endpoint": "https://example.com/status/200",
"http_method": "POST",
"id": "01EJFTSCC78X5V07NPY2MHR00M",
"name": "Example Webhook",
"request_format": "json",
"status": "active",
"subscriptions": [
"conditional_ticket_events"
],
"updated_at": "2020-10-20T08:16:28Z",
"updated_by": "1234567"
}
]
}
45 changes: 45 additions & 0 deletions zendesk/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,59 @@ type WebhookSigningSecret struct {
Secret string `json:"secret"`
}

// WebhookListOptions is options for ListWebhooks
//
// ref: https://developer.zendesk.com/api-reference/webhooks/webhooks-api/webhooks/#list-webhooks
type WebhookListOptions struct {
PageOptions
FilterNameContains string `url:"filter[name_contains],omitempty"`
FilterStatus string `url:"filter[status],omitempty"`
PageAfter string `url:"page[after],omitempty"`
PageBefore string `url:"page[before],omitempty"`
PageSize string `url:"page[size],omitempty"`
Sort string `url:"sort,omitempty"`
}

type WebhookAPI interface {
ListWebhooks(ctx context.Context, opts *WebhookListOptions) ([]Webhook, Page, error)
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)
}

// ListWebhooks lists webhooks.
//
// https://developer.zendesk.com/api-reference/webhooks/webhooks-api/webhooks/#list-webhooks
func (z *Client) ListWebhooks(ctx context.Context, opts *WebhookListOptions) ([]Webhook, Page, error) {
var data struct {
Webhooks []Webhook `json:"webhooks"`
Page
}

tmp := opts
if tmp == nil {
tmp = &WebhookListOptions{}
}

u, err := addOptions("/webhooks", tmp)
if err != nil {
return nil, Page{}, err
}

body, err := z.get(ctx, u)
if err != nil {
return nil, Page{}, err
}

err = json.Unmarshal(body, &data)
if err != nil {
return nil, Page{}, err
}
return data.Webhooks, data.Page, nil
}

// CreateWebhook creates new webhook.
//
// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#create-or-clone-webhook
Expand Down
15 changes: 15 additions & 0 deletions zendesk/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"testing"
)

func TestListWebhooks(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "webhooks.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

webhooks, _, err := client.ListWebhooks(ctx, &WebhookListOptions{})
if err != nil {
t.Fatalf("Failed to get webhooks: %s", err)
}

if len(webhooks) != 1 {
t.Fatalf("expected length of groups is 1, but got %d", len(webhooks))
}
}

func TestCreateWebhook(t *testing.T) {
mockAPI := newMockAPI(http.MethodPost, "webhooks.json")
client := newTestClient(mockAPI)
Expand Down