From bcc30237571f8f2a1780fb561c842fa69b91ebc0 Mon Sep 17 00:00:00 2001 From: Nicolas Sotgui Date: Thu, 22 Aug 2024 21:06:25 -0400 Subject: [PATCH] Add list webhooks api --- fixture/GET/webhooks.json | 39 +++++++++++++++++++++++++++++++++ zendesk/webhook.go | 45 +++++++++++++++++++++++++++++++++++++++ zendesk/webhook_test.go | 15 +++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 fixture/GET/webhooks.json diff --git a/fixture/GET/webhooks.json b/fixture/GET/webhooks.json new file mode 100644 index 00000000..eff7187d --- /dev/null +++ b/fixture/GET/webhooks.json @@ -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" + } + ] +} diff --git a/zendesk/webhook.go b/zendesk/webhook.go index 8694cc68..fb8a64e5 100644 --- a/zendesk/webhook.go +++ b/zendesk/webhook.go @@ -38,7 +38,21 @@ 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 @@ -46,6 +60,37 @@ type WebhookAPI interface { 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 diff --git a/zendesk/webhook_test.go b/zendesk/webhook_test.go index 162eafa6..282c769e 100644 --- a/zendesk/webhook_test.go +++ b/zendesk/webhook_test.go @@ -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)