-
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 operations for the Clients API; Get, Verify and List. Added types for Client and Session.
- Loading branch information
Showing
6 changed files
with
243 additions
and
2 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
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,20 @@ | ||
package clerk | ||
|
||
type Client struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
LastActiveSessionID *string `json:"last_active_session_id"` | ||
SignInID *string `json:"sign_in_id"` | ||
SignUpID *string `json:"sign_up_id"` | ||
SessionIDs []string `json:"session_ids"` | ||
Sessions []*Session `json:"sessions"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} | ||
|
||
type ClientList struct { | ||
APIResource | ||
Clients []*Client `json:"data"` | ||
TotalCount int64 `json:"total_count"` | ||
} |
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,81 @@ | ||
// Package client provides the Client API. | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
) | ||
|
||
//go:generate go run ../cmd/gen/main.go | ||
|
||
const path = "/clients" | ||
|
||
// Client is used to invoke the Client API. | ||
// This is an API client for interacting with Clerk Client resources. | ||
type Client struct { | ||
Backend clerk.Backend | ||
} | ||
|
||
type ClientConfig struct { | ||
clerk.BackendConfig | ||
} | ||
|
||
func NewClient(config *ClientConfig) *Client { | ||
return &Client{ | ||
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
||
// Get retrieves the client specified by ID. | ||
func (c *Client) Get(ctx context.Context, id string) (*clerk.Client, error) { | ||
path, err := clerk.JoinPath(path, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodGet, path) | ||
client := &clerk.Client{} | ||
err = c.Backend.Call(ctx, req, client) | ||
return client, err | ||
} | ||
|
||
type VerifyParams struct { | ||
clerk.APIParams | ||
Token *string `json:"token,omitempty"` | ||
} | ||
|
||
// Verify verifies the Client in the provided JWT. | ||
func (c *Client) Verify(ctx context.Context, params *VerifyParams) (*clerk.Client, error) { | ||
path, err := clerk.JoinPath(path, "/verify") | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
req.SetParams(params) | ||
client := &clerk.Client{} | ||
err = c.Backend.Call(ctx, req, client) | ||
return client, err | ||
} | ||
|
||
type ListParams struct { | ||
clerk.APIParams | ||
clerk.ListParams | ||
} | ||
|
||
func (params *ListParams) ToQuery() url.Values { | ||
return params.ListParams.ToQuery() | ||
} | ||
|
||
// List returns a list of all the clients. | ||
// | ||
// Deprecated: The operation is deprecated and will be removed in | ||
// future versions. | ||
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.ClientList, error) { | ||
req := clerk.NewAPIRequest(http.MethodGet, path) | ||
req.SetParams(params) | ||
list := &clerk.ClientList{} | ||
err := c.Backend.Call(ctx, req, list) | ||
return list, 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,84 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
"github.com/clerk/clerk-sdk-go/v2/clerktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestClientClientGet(t *testing.T) { | ||
t.Parallel() | ||
id := "client_123" | ||
config := &ClientConfig{} | ||
config.HTTPClient = &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s"}`, id)), | ||
Method: http.MethodGet, | ||
Path: "/v1/clients/" + id, | ||
}, | ||
} | ||
c := NewClient(config) | ||
client, err := c.Get(context.Background(), id) | ||
require.NoError(t, err) | ||
require.Equal(t, id, client.ID) | ||
} | ||
|
||
func TestClientClientVerify(t *testing.T) { | ||
t.Parallel() | ||
id := "client_123" | ||
token := "the-token" | ||
config := &ClientConfig{} | ||
config.HTTPClient = &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
In: json.RawMessage(fmt.Sprintf(`{"token":"%s"}`, token)), | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s"}`, id)), | ||
Method: http.MethodPost, | ||
Path: "/v1/clients/verify", | ||
}, | ||
} | ||
c := NewClient(config) | ||
client, err := c.Verify(context.Background(), &VerifyParams{ | ||
Token: clerk.String("the-token"), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, id, client.ID) | ||
} | ||
|
||
func TestClientClientList(t *testing.T) { | ||
t.Parallel() | ||
config := &ClientConfig{} | ||
config.HTTPClient = &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
Out: json.RawMessage(`{ | ||
"data": [{"id":"client_123","last_active_session_id":"sess_123"}], | ||
"total_count": 1 | ||
}`), | ||
Method: http.MethodGet, | ||
Path: "/v1/clients", | ||
Query: &url.Values{ | ||
"limit": []string{"1"}, | ||
"offset": []string{"2"}, | ||
}, | ||
}, | ||
} | ||
c := NewClient(config) | ||
params := &ListParams{} | ||
params.Limit = clerk.Int64(1) | ||
params.Offset = clerk.Int64(2) | ||
list, err := c.List(context.Background(), params) | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), list.TotalCount) | ||
require.Equal(t, 1, len(list.Clients)) | ||
require.Equal(t, "client_123", list.Clients[0].ID) | ||
require.Equal(t, "sess_123", *list.Clients[0].LastActiveSessionID) | ||
} |
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,19 @@ | ||
package clerk | ||
|
||
import "encoding/json" | ||
|
||
type Session struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
ClientID string `json:"client_id"` | ||
UserID string `json:"user_id"` | ||
Status string `json:"status"` | ||
LastActiveOrganizationID string `json:"last_active_organization_id,omitempty"` | ||
Actor json.RawMessage `json:"actor,omitempty"` | ||
LastActiveAt int64 `json:"last_active_at"` | ||
ExpireAt int64 `json:"expire_at"` | ||
AbandonAt int64 `json:"abandon_at"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} |