diff --git a/testing_token.go b/testing_token.go new file mode 100644 index 0000000..c245f0a --- /dev/null +++ b/testing_token.go @@ -0,0 +1,8 @@ +package clerk + +type TestingToken struct { + APIResource + Object string `json:"object"` + Token string `json:"token"` + ExpiresAt int64 `json:"expires_at"` +} diff --git a/testingtoken/api.go b/testingtoken/api.go new file mode 100644 index 0000000..1a162a6 --- /dev/null +++ b/testingtoken/api.go @@ -0,0 +1,20 @@ +// Code generated by "gen"; DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. +package testingtoken + +import ( + "context" + + "github.com/clerk/clerk-sdk-go/v2" +) + +// Create creates a new testing token. +func Create(ctx context.Context) (*clerk.TestingToken, error) { + return getClient().Create(ctx) +} + +func getClient() *Client { + return &Client{ + Backend: clerk.GetBackend(), + } +} diff --git a/testingtoken/client.go b/testingtoken/client.go new file mode 100644 index 0000000..27a976c --- /dev/null +++ b/testingtoken/client.go @@ -0,0 +1,34 @@ +// Package testingtoken provides the Testing Tokens API. +// +// https://clerk.com/docs/reference/backend-api/tag/Testing-Tokens +package testingtoken + +import ( + "context" + "net/http" + + "github.com/clerk/clerk-sdk-go/v2" +) + +//go:generate go run ../cmd/gen/main.go + +const path = "/testing_tokens" + +// Client is used to invoke the Testing Tokens API. +type Client struct { + Backend clerk.Backend +} + +func NewClient(config *clerk.ClientConfig) *Client { + return &Client{ + Backend: clerk.NewBackend(&config.BackendConfig), + } +} + +// Create creates a new testing token. +func (c *Client) Create(ctx context.Context) (*clerk.TestingToken, error) { + req := clerk.NewAPIRequest(http.MethodPost, path) + token := &clerk.TestingToken{} + err := c.Backend.Call(ctx, req, token) + return token, err +} diff --git a/testingtoken/client_test.go b/testingtoken/client_test.go new file mode 100644 index 0000000..38c0941 --- /dev/null +++ b/testingtoken/client_test.go @@ -0,0 +1,35 @@ +package testingtoken + +import ( + "context" + "encoding/json" + "net/http" + "testing" + + "github.com/clerk/clerk-sdk-go/v2" + "github.com/clerk/clerk-sdk-go/v2/clerktest" + "github.com/stretchr/testify/require" +) + +func TestTestingTokenClientCreate(t *testing.T) { + t.Parallel() + + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + In: nil, + Out: json.RawMessage( + `{"object":"testing_token","token":"1713877200-c_3G2MvPu9PnXcuhbPZNao0LOXqK9A7YrnBn0HmIWxt","expires_at":1713880800}`, + ), + Method: http.MethodPost, + Path: "/v1/testing_tokens", + }, + } + client := NewClient(config) + token, err := client.Create(context.Background()) + require.NoError(t, err) + require.Equal(t, "testing_token", token.Object) + require.Equal(t, "1713877200-c_3G2MvPu9PnXcuhbPZNao0LOXqK9A7YrnBn0HmIWxt", token.Token) + require.Equal(t, int64(1713880800), token.ExpiresAt) +}