Skip to content

Commit

Permalink
feat: Svix Webhooks API (#236)
Browse files Browse the repository at this point in the history
Added support for the Svix Webhooks API with Create, Delete and
RefreshURL operations.
  • Loading branch information
gkats authored Feb 9, 2024
1 parent a7c31c0 commit 32fd2bb
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
6 changes: 6 additions & 0 deletions svix_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package clerk

type SvixWebhook struct {
APIResource
SvixURL string `json:"svix_url"`
}
31 changes: 31 additions & 0 deletions svixwebhook/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions svixwebhook/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Package svixwebhook provides the Svix Webhooks API.
package svixwebhook

import (
"context"
"net/http"

"github.com/clerk/clerk-sdk-go/v2"
)

//go:generate go run ../cmd/gen/main.go

const path = "/webhooks/svix"

// Client is used to invoke the Organizations API.
type Client struct {
Backend clerk.Backend
}

type ClientConfig struct {
clerk.BackendConfig
}

func NewClient(config *ClientConfig) *Client {
return &Client{
Backend: clerk.NewBackend(&config.BackendConfig),
}
}

// Create creates a Svix app.
func (c *Client) Create(ctx context.Context) (*clerk.SvixWebhook, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
resource := &clerk.SvixWebhook{}
err := c.Backend.Call(ctx, req, resource)
return resource, err
}

// Delete deletes the Svix app.
func (c *Client) Delete(ctx context.Context) (*clerk.SvixWebhook, error) {
req := clerk.NewAPIRequest(http.MethodDelete, path)
resource := &clerk.SvixWebhook{}
err := c.Backend.Call(ctx, req, resource)
return resource, err
}

// RefreshURL generates a new URL for accessing Svix's dashboard.
func (c *Client) RefreshURL(ctx context.Context) (*clerk.SvixWebhook, error) {
req := clerk.NewAPIRequest(http.MethodPost, "/webhooks/svix_url")
resource := &clerk.SvixWebhook{}
err := c.Backend.Call(ctx, req, resource)
return resource, err
}
63 changes: 63 additions & 0 deletions svixwebhook/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package svixwebhook

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/clerk/clerk-sdk-go/v2/clerktest"
"github.com/stretchr/testify/require"
)

func TestSvixWebhookClientCreate(t *testing.T) {
t.Parallel()
svixURL := "https://foo.com/webhook"
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"svix_url":"%s"}`, svixURL)),
Method: http.MethodPost,
Path: "/v1/webhooks/svix",
},
}
client := NewClient(config)
webhook, err := client.Create(context.Background())
require.NoError(t, err)
require.Equal(t, svixURL, webhook.SvixURL)
}

func TestSvixWebhookClientDelete(t *testing.T) {
t.Parallel()
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Method: http.MethodDelete,
Path: "/v1/webhooks/svix",
},
}
client := NewClient(config)
_, err := client.Delete(context.Background())
require.NoError(t, err)
}

func TestSvixWebhookClientRefreshURL(t *testing.T) {
t.Parallel()
svixURL := "https://foo.com/webhook"
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"svix_url":"%s"}`, svixURL)),
Method: http.MethodPost,
Path: "/v1/webhooks/svix_url",
},
}
client := NewClient(config)
webhook, err := client.RefreshURL(context.Background())
require.NoError(t, err)
require.Equal(t, svixURL, webhook.SvixURL)
}

0 comments on commit 32fd2bb

Please sign in to comment.