Skip to content

Commit

Permalink
feat: Blocklist Identifiers API (#222)
Browse files Browse the repository at this point in the history
Added support for the blocklist identifiers API. Supported operations
are Create, Delete and List.
  • Loading branch information
gkats authored Feb 7, 2024
1 parent e15c401 commit f597eec
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 55 deletions.
2 changes: 1 addition & 1 deletion actortoken/api.go

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

79 changes: 29 additions & 50 deletions allowlistidentifier/allowlist_identifier_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package allowlistidentifier

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

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

Expand All @@ -19,10 +17,12 @@ func TestAllowlistIdentifierCreate(t *testing.T) {
id := "alid_123"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &mockRoundTripper{
T: t,
in: json.RawMessage(fmt.Sprintf(`{"identifier":"%s"}`, identifier)),
out: json.RawMessage(fmt.Sprintf(`{"id":"%s","identifier":"%s"}`, id, identifier)),
Transport: &clerktest.RoundTripper{
T: t,
In: json.RawMessage(fmt.Sprintf(`{"identifier":"%s"}`, identifier)),
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","identifier":"%s"}`, id, identifier)),
Method: http.MethodPost,
Path: "/v1/allowlist_identifiers",
},
},
}))
Expand All @@ -31,17 +31,17 @@ func TestAllowlistIdentifierCreate(t *testing.T) {
Identifier: clerk.String(identifier),
})
require.NoError(t, err)
assert.Equal(t, id, allowlistIdentifier.ID)
assert.Equal(t, identifier, allowlistIdentifier.Identifier)
require.Equal(t, id, allowlistIdentifier.ID)
require.Equal(t, identifier, allowlistIdentifier.Identifier)
}

func TestAllowlistIdentifierCreate_Error(t *testing.T) {
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &mockRoundTripper{
Transport: &clerktest.RoundTripper{
T: t,
status: http.StatusBadRequest,
out: json.RawMessage(`{
Status: http.StatusBadRequest,
Out: json.RawMessage(`{
"errors":[{
"code":"create-error-code"
}],
Expand All @@ -55,70 +55,49 @@ func TestAllowlistIdentifierCreate_Error(t *testing.T) {
require.Error(t, err)
apiErr, ok := err.(*clerk.APIErrorResponse)
require.True(t, ok)
assert.Equal(t, "create-trace-id", apiErr.TraceID)
require.Equal(t, "create-trace-id", apiErr.TraceID)
require.Equal(t, 1, len(apiErr.Errors))
assert.Equal(t, "create-error-code", apiErr.Errors[0].Code)
require.Equal(t, "create-error-code", apiErr.Errors[0].Code)
}

func TestAllowlistIdentifierDelete(t *testing.T) {
id := "alid_456"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &mockRoundTripper{
T: t,
out: json.RawMessage(fmt.Sprintf(`{"id":"%s","deleted":true}`, id)),
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","deleted":true}`, id)),
Method: http.MethodDelete,
Path: "/v1/allowlist_identifiers/" + id,
},
},
}))

allowlistIdentifier, err := Delete(context.Background(), id)
require.NoError(t, err)
assert.Equal(t, id, allowlistIdentifier.ID)
assert.True(t, allowlistIdentifier.Deleted)
require.Equal(t, id, allowlistIdentifier.ID)
require.True(t, allowlistIdentifier.Deleted)
}

func TestAllowlistIdentifierList(t *testing.T) {
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &mockRoundTripper{
Transport: &clerktest.RoundTripper{
T: t,
out: json.RawMessage(`{
Out: json.RawMessage(`{
"data": [{"id":"alid_123","identifier":"[email protected]"}],
"total_count": 1
}`),
Method: http.MethodGet,
Path: "/v1/allowlist_identifiers",
},
},
}))

list, err := List(context.Background(), &ListParams{})
require.NoError(t, err)
assert.Equal(t, int64(1), list.TotalCount)
assert.Equal(t, 1, len(list.AllowlistIdentifiers))
assert.Equal(t, "alid_123", list.AllowlistIdentifiers[0].ID)
assert.Equal(t, "[email protected]", list.AllowlistIdentifiers[0].Identifier)
}

type mockRoundTripper struct {
T *testing.T
status int
in json.RawMessage
out json.RawMessage
}

func (rt *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
if rt.status == 0 {
rt.status = http.StatusOK
}
if rt.in != nil {
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
defer r.Body.Close()
assert.JSONEq(rt.T, string(rt.in), string(body))
}
return &http.Response{
StatusCode: rt.status,
Body: io.NopCloser(bytes.NewReader(rt.out)),
}, nil
require.Equal(t, int64(1), list.TotalCount)
require.Equal(t, 1, len(list.AllowlistIdentifiers))
require.Equal(t, "alid_123", list.AllowlistIdentifiers[0].ID)
require.Equal(t, "[email protected]", list.AllowlistIdentifiers[0].Identifier)
}
2 changes: 1 addition & 1 deletion allowlistidentifier/api.go

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

3 changes: 1 addition & 2 deletions allowlistidentifier/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/clerk/clerk-sdk-go/v2"
)
Expand Down Expand Up @@ -46,7 +45,7 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Allow

// Delete removes an identifier from the allowlist.
func (c *Client) Delete(ctx context.Context, id string) (*clerk.DeletedResource, error) {
path, err := url.JoinPath(path, id)
path, err := clerk.JoinPath(path, id)
if err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions blocklist_identifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package clerk

type BlocklistIdentifier struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
Identifier string `json:"identifier"`
IdentifierType string `json:"identifier_type"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type BlocklistIdentifierList struct {
APIResource
BlocklistIdentifiers []*BlocklistIdentifier `json:"data"`
TotalCount int64 `json:"total_count"`
}
31 changes: 31 additions & 0 deletions blocklistidentifier/api.go

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

67 changes: 67 additions & 0 deletions blocklistidentifier/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Package blocklistidentifier provides the Blocklist Identifiers API.
package blocklistidentifier

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

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

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

const path = "/blocklist_identifiers"

// Client is used to invoke the Blocklist Identifiers 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
Identifier *string `json:"identifier,omitempty"`
}

// Create adds a new identifier to the blocklist.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.BlocklistIdentifier, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
identifier := &clerk.BlocklistIdentifier{}
err := c.Backend.Call(ctx, req, identifier)
return identifier, err
}

// Delete removes an identifier from the blocklist.
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)
identifier := &clerk.DeletedResource{}
err = c.Backend.Call(ctx, req, identifier)
return identifier, err
}

type ListParams struct {
clerk.APIParams
}

// List returns all the identifiers in the blocklist.
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.BlocklistIdentifierList, error) {
req := clerk.NewAPIRequest(http.MethodGet, fmt.Sprintf("%s?paginated=true", path))
list := &clerk.BlocklistIdentifierList{}
err := c.Backend.Call(ctx, req, list)
return list, err
}
Loading

0 comments on commit f597eec

Please sign in to comment.