-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the Svix Webhooks API with Create, Delete and RefreshURL operations.
- Loading branch information
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |