|  | 
|  | 1 | +package zendesk | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"encoding/json" | 
|  | 6 | +	"fmt" | 
|  | 7 | +	"time" | 
|  | 8 | +) | 
|  | 9 | + | 
|  | 10 | +// Webhook is struct for webhook payload. | 
|  | 11 | +// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/ | 
|  | 12 | +type Webhook struct { | 
|  | 13 | +	Authentication *WebhookAuthentication `json:"authentication,omitempty"` | 
|  | 14 | +	CreatedAt      time.Time              `json:"created_at,omitempty"` | 
|  | 15 | +	CreatedBy      string                 `json:"created_by,omitempty"` | 
|  | 16 | +	Description    string                 `json:"description,omitempty"` | 
|  | 17 | +	Endpoint       string                 `json:"endpoint"` | 
|  | 18 | +	ExternalSource interface{}            `json:"external_source,omitempty"` | 
|  | 19 | +	HTTPMethod     string                 `json:"http_method"` | 
|  | 20 | +	ID             string                 `json:"id,omitempty"` | 
|  | 21 | +	Name           string                 `json:"name"` | 
|  | 22 | +	RequestFormat  string                 `json:"request_format"` | 
|  | 23 | +	SigningSecret  *WebhookSigningSecret  `json:"signing_secret,omitempty"` | 
|  | 24 | +	Status         string                 `json:"status"` | 
|  | 25 | +	Subscriptions  []string               `json:"subscriptions,omitempty"` | 
|  | 26 | +	UpdatedAt      time.Time              `json:"updated_at,omitempty"` | 
|  | 27 | +	UpdatedBy      string                 `json:"updated_by,omitempty"` | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +type WebhookAuthentication struct { | 
|  | 31 | +	Type        string      `json:"type"` | 
|  | 32 | +	Data        interface{} `json:"data"` | 
|  | 33 | +	AddPosition string      `json:"add_position"` | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +type WebhookSigningSecret struct { | 
|  | 37 | +	Algorithm string `json:"algorithm"` | 
|  | 38 | +	Secret    string `json:"secret"` | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +type WebhookAPI interface { | 
|  | 42 | +	CreateWebhook(ctx context.Context, hook *Webhook) (*Webhook, error) | 
|  | 43 | +	GetWebhook(ctx context.Context, webhookID string) (*Webhook, error) | 
|  | 44 | +	UpdateWebhook(ctx context.Context, webhookID string, hook *Webhook) error | 
|  | 45 | +	DeleteWebhook(ctx context.Context, webhookID string) error | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +// CreateWebhook creates new webhook. | 
|  | 49 | +// | 
|  | 50 | +// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#create-or-clone-webhook | 
|  | 51 | +func (z *Client) CreateWebhook(ctx context.Context, hook *Webhook) (*Webhook, error) { | 
|  | 52 | +	var data, result struct { | 
|  | 53 | +		Webhook *Webhook `json:"webhook"` | 
|  | 54 | +	} | 
|  | 55 | +	data.Webhook = hook | 
|  | 56 | + | 
|  | 57 | +	body, err := z.post(ctx, "/webhooks", data) | 
|  | 58 | +	if err != nil { | 
|  | 59 | +		return nil, err | 
|  | 60 | +	} | 
|  | 61 | + | 
|  | 62 | +	err = json.Unmarshal(body, &result) | 
|  | 63 | +	if err != nil { | 
|  | 64 | +		return nil, err | 
|  | 65 | +	} | 
|  | 66 | +	return result.Webhook, nil | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +// GetWebhook gets a specified webhook. | 
|  | 70 | +// | 
|  | 71 | +// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#show-webhook | 
|  | 72 | +func (z *Client) GetWebhook(ctx context.Context, webhookID string) (*Webhook, error) { | 
|  | 73 | +	var result struct { | 
|  | 74 | +		Webhook *Webhook `json:"webhook"` | 
|  | 75 | +	} | 
|  | 76 | + | 
|  | 77 | +	body, err := z.get(ctx, fmt.Sprintf("/webhooks/%s", webhookID)) | 
|  | 78 | +	if err != nil { | 
|  | 79 | +		return nil, err | 
|  | 80 | +	} | 
|  | 81 | + | 
|  | 82 | +	err = json.Unmarshal(body, &result) | 
|  | 83 | +	if err != nil { | 
|  | 84 | +		return nil, err | 
|  | 85 | +	} | 
|  | 86 | + | 
|  | 87 | +	return result.Webhook, nil | 
|  | 88 | +} | 
|  | 89 | + | 
|  | 90 | +// UpdateWebhook updates a webhook with the specified webhook. | 
|  | 91 | +// | 
|  | 92 | +// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#update-webhook | 
|  | 93 | +func (z *Client) UpdateWebhook(ctx context.Context, webhookID string, hook *Webhook) error { | 
|  | 94 | +	var data struct { | 
|  | 95 | +		Webhook *Webhook `json:"webhook"` | 
|  | 96 | +	} | 
|  | 97 | +	data.Webhook = hook | 
|  | 98 | + | 
|  | 99 | +	_, err := z.put(ctx, fmt.Sprintf("/webhooks/%s", webhookID), data) | 
|  | 100 | +	if err != nil { | 
|  | 101 | +		return err | 
|  | 102 | +	} | 
|  | 103 | + | 
|  | 104 | +	return nil | 
|  | 105 | +} | 
|  | 106 | + | 
|  | 107 | +// DeleteWebhook deletes the specified webhook. | 
|  | 108 | +// | 
|  | 109 | +// https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#delete-webhook | 
|  | 110 | +func (z *Client) DeleteWebhook(ctx context.Context, webhookID string) error { | 
|  | 111 | +	err := z.delete(ctx, fmt.Sprintf("/webhooks/%s", webhookID)) | 
|  | 112 | +	if err != nil { | 
|  | 113 | +		return err | 
|  | 114 | +	} | 
|  | 115 | + | 
|  | 116 | +	return nil | 
|  | 117 | +} | 
0 commit comments