Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Svix Webhooks API #236

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
8 changes: 5 additions & 3 deletions clerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,11 @@ func (b *defaultBackend) do(req *http.Request, params Params, setter ResponseRea
}

setter.Read(apiResponse)
err = json.Unmarshal(resBody, setter)
if err != nil {
return err
if len(resBody) > 0 {
err := json.Unmarshal(resBody, setter)
if err != nil {
return err
}
}

return nil
Expand Down
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)
}
Loading