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: Redirect URLs API #229

Merged
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
2 changes: 1 addition & 1 deletion clerk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestBackendCall_ParseableError(t *testing.T) {
assert.JSONEq(t, errorJSON, string(apiErr.Response.RawJSON))
}

// TestBackendCall_ParseableError tests responses with a non-successful
// TestBackendCall_NonParseableError tests responses with a non-successful
// status code and a body that can be deserialized to an unexpected
// error response. This might happen when the Clerk API encounters an
// unexpected server error and usually results in 5xx status codes.
Expand Down
16 changes: 16 additions & 0 deletions redirect_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package clerk

type RedirectURL struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
URL string `json:"url"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type RedirectURLList struct {
APIResource
RedirectURLs []*RedirectURL `json:"data"`
TotalCount int64 `json:"total_count"`
}
36 changes: 36 additions & 0 deletions redirecturl/api.go

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

79 changes: 79 additions & 0 deletions redirecturl/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Package redirecturl provides the Redirect URLs API.
package redirecturl

import (
"context"
"fmt"
"net/http"

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

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

const path = "/redirect_urls"

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

type ClientConfig struct {
clerk.BackendConfig
}

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

type CreateParams struct {
clerk.APIParams
URL *string `json:"url,omitempty"`
}

// Create creates a new redirect url.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.RedirectURL, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
redirectURL := &clerk.RedirectURL{}
err := c.Backend.Call(ctx, req, redirectURL)
return redirectURL, err
}

// Get retrieves details for a redirect url by ID.
func (c *Client) Get(ctx context.Context, id string) (*clerk.RedirectURL, error) {
path, err := clerk.JoinPath(path, id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
redirectURL := &clerk.RedirectURL{}
err = c.Backend.Call(ctx, req, redirectURL)
return redirectURL, err
}

// Delete deletes a redirect url.
func (c *Client) Delete(ctx context.Context, id string) (*clerk.DeletedResource, error) {
path, err := clerk.JoinPath(path, id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodDelete, path)
redirectURL := &clerk.DeletedResource{}
err = c.Backend.Call(ctx, req, redirectURL)
return redirectURL, err
}

type ListParams struct {
clerk.APIParams
}

// List returns a list of redirect urls.
func (c *Client) List(ctx context.Context, _ *ListParams) (*clerk.RedirectURLList, error) {
req := clerk.NewAPIRequest(http.MethodGet, fmt.Sprintf("%s?paginated=true", path))
list := &clerk.RedirectURLList{}
err := c.Backend.Call(ctx, req, list)
return list, err
}
123 changes: 123 additions & 0 deletions redirecturl/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package redirecturl

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

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

func TestRedirectURLClientCreate(t *testing.T) {
t.Parallel()
id := "ru_123"
url := "https://example.com"
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
In: json.RawMessage(fmt.Sprintf(`{"url":"%s"}`, url)),
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","url":"%s"}`, id, url)),
Method: http.MethodPost,
Path: "/v1/redirect_urls",
},
}
client := NewClient(config)
redirectURL, err := client.Create(context.Background(), &CreateParams{
URL: clerk.String(url),
})
require.NoError(t, err)
require.Equal(t, id, redirectURL.ID)
require.Equal(t, url, redirectURL.URL)
}

func TestRedirectURLClientCreate_Error(t *testing.T) {
t.Parallel()
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Status: http.StatusBadRequest,
Out: json.RawMessage(`{
"errors":[{
"code":"create-error-code"
}],
"clerk_trace_id":"create-trace-id"
}`),
},
}
client := NewClient(config)
_, err := client.Create(context.Background(), &CreateParams{})
require.Error(t, err)
apiErr, ok := err.(*clerk.APIErrorResponse)
require.True(t, ok)
require.Equal(t, "create-trace-id", apiErr.TraceID)
require.Equal(t, 1, len(apiErr.Errors))
require.Equal(t, "create-error-code", apiErr.Errors[0].Code)
}

func TestRedirectURLClientGet(t *testing.T) {
t.Parallel()
id := "ru_123"
url := "https://example.com"
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","url":"%s"}`, id, url)),
Method: http.MethodGet,
Path: "/v1/redirect_urls/" + id,
},
}
client := NewClient(config)
redirectURL, err := client.Get(context.Background(), id)
require.NoError(t, err)
require.Equal(t, id, redirectURL.ID)
require.Equal(t, url, redirectURL.URL)
}

func TestRedirectURLClientDelete(t *testing.T) {
t.Parallel()
id := "ru_123"
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","deleted":true}`, id)),
Method: http.MethodDelete,
Path: "/v1/redirect_urls/" + id,
},
}
client := NewClient(config)
redirectURL, err := client.Delete(context.Background(), id)
require.NoError(t, err)
require.Equal(t, id, redirectURL.ID)
require.True(t, redirectURL.Deleted)
}

func TestRedirectURLClientList(t *testing.T) {
t.Parallel()
config := &ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(`{
"data": [{"id":"ru_123","url":"https://example.com"}],
"total_count": 1
}`),
Method: http.MethodGet,
Path: "/v1/redirect_urls",
},
}
client := NewClient(config)
list, err := client.List(context.Background(), &ListParams{})
require.NoError(t, err)
require.Equal(t, int64(1), list.TotalCount)
require.Equal(t, 1, len(list.RedirectURLs))
require.Equal(t, "ru_123", list.RedirectURLs[0].ID)
require.Equal(t, "https://example.com", list.RedirectURLs[0].URL)
}
Loading