-
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.
feat: Add support for waitlist entries endpoints (#360)
- Loading branch information
Showing
5 changed files
with
253 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
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,18 @@ | ||
package clerk | ||
|
||
type WaitlistEntry struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
EmailAddress string `json:"email_address"` | ||
Status string `json:"status"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
Invitation *Invitation `json:"invitation"` | ||
} | ||
|
||
type WaitlistEntriesList struct { | ||
APIResource | ||
WaitlistEntries []*WaitlistEntry `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,70 @@ | ||
package waitlistentry | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
) | ||
|
||
//go:generate go run ../cmd/gen/main.go | ||
|
||
const path = "/waitlist_entries" | ||
|
||
// Client is used to invoke the Waitlist Entries API. | ||
type Client struct { | ||
Backend clerk.Backend | ||
} | ||
|
||
func NewClient(config *clerk.ClientConfig) *Client { | ||
return &Client{ | ||
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
||
type ListParams struct { | ||
clerk.APIParams | ||
clerk.ListParams | ||
OrderBy *string `json:"order_by,omitempty"` | ||
Query *string `json:"query,omitempty"` | ||
Statuses []string `json:"status,omitempty"` | ||
} | ||
|
||
// ToQuery returns query string values from the params. | ||
func (params *ListParams) ToQuery() url.Values { | ||
q := params.ListParams.ToQuery() | ||
if params.OrderBy != nil { | ||
q.Set("order_by", *params.OrderBy) | ||
} | ||
if params.Query != nil { | ||
q.Set("query", *params.Query) | ||
} | ||
for _, status := range params.Statuses { | ||
q.Add("status", status) | ||
} | ||
return q | ||
} | ||
|
||
// List returns all waitlist entries. | ||
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.WaitlistEntriesList, error) { | ||
req := clerk.NewAPIRequest(http.MethodGet, path) | ||
req.SetParams(params) | ||
list := &clerk.WaitlistEntriesList{} | ||
err := c.Backend.Call(ctx, req, list) | ||
return list, err | ||
} | ||
|
||
type CreateParams struct { | ||
clerk.APIParams | ||
EmailAddress string `json:"email_address"` | ||
} | ||
|
||
// Create adds a new waitlist entry. | ||
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.WaitlistEntry, error) { | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
req.SetParams(params) | ||
invitation := &clerk.WaitlistEntry{} | ||
err := c.Backend.Call(ctx, req, invitation) | ||
return invitation, 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,139 @@ | ||
package waitlistentry | ||
|
||
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 TestWaitlistList(t *testing.T) { | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
Out: json.RawMessage(`{ | ||
"data": [{"id":"wle_123","email_address":"[email protected]"}], | ||
"total_count": 1 | ||
}`), | ||
Path: "/v1/waitlist_entries", | ||
Method: http.MethodGet, | ||
}, | ||
}, | ||
})) | ||
|
||
list, err := List(context.Background(), &ListParams{}) | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), list.TotalCount) | ||
require.Equal(t, 1, len(list.WaitlistEntries)) | ||
require.Equal(t, "wle_123", list.WaitlistEntries[0].ID) | ||
require.Equal(t, "[email protected]", list.WaitlistEntries[0].EmailAddress) | ||
} | ||
|
||
func TestWaitlistEntriesListWithParams(t *testing.T) { | ||
limit := int64(10) | ||
offset := int64(20) | ||
orderBy := "-created_at" | ||
query := "[email protected]" | ||
status1 := "pending" | ||
status2 := "invited" | ||
|
||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
Out: json.RawMessage(`{ | ||
"data": [ | ||
{"id":"wle_123","email_address":"[email protected]"}, | ||
{"id":"wle_124","email_address":"[email protected]","invitation":{"id":"inv_124","email_address":"[email protected]"}} | ||
], | ||
"total_count": 2 | ||
}`), | ||
Path: "/v1/waitlist_entries", | ||
Method: http.MethodGet, | ||
Query: &url.Values{ | ||
"limit": []string{fmt.Sprintf("%d", limit)}, | ||
"offset": []string{fmt.Sprintf("%d", offset)}, | ||
"order_by": []string{orderBy}, | ||
"query": []string{query}, | ||
"status": []string{status1, status2}, | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
list, err := List(context.Background(), &ListParams{ | ||
ListParams: clerk.ListParams{ | ||
Limit: &limit, | ||
Offset: &offset, | ||
}, | ||
OrderBy: &orderBy, | ||
Query: &query, | ||
Statuses: []string{status1, status2}, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, int64(2), list.TotalCount) | ||
require.Len(t, list.WaitlistEntries, 2) | ||
require.Equal(t, "wle_123", list.WaitlistEntries[0].ID) | ||
require.Equal(t, "[email protected]", list.WaitlistEntries[0].EmailAddress) | ||
require.Nil(t, list.WaitlistEntries[0].Invitation) | ||
require.Equal(t, "wle_124", list.WaitlistEntries[1].ID) | ||
require.Equal(t, "[email protected]", list.WaitlistEntries[1].EmailAddress) | ||
require.NotNil(t, list.WaitlistEntries[1].Invitation) | ||
require.Equal(t, "inv_124", list.WaitlistEntries[1].Invitation.ID) | ||
require.Equal(t, "[email protected]", list.WaitlistEntries[1].Invitation.EmailAddress) | ||
} | ||
|
||
func TestWaitlistEntryCreate(t *testing.T) { | ||
emailAddress := "[email protected]" | ||
id := "inv_123" | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
In: json.RawMessage(fmt.Sprintf(`{"email_address":"%s"}`, emailAddress)), | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","email_address":"%s"}`, id, emailAddress)), | ||
Method: http.MethodPost, | ||
Path: "/v1/waitlist_entries", | ||
}, | ||
}, | ||
})) | ||
|
||
entry, err := Create(context.Background(), &CreateParams{ | ||
EmailAddress: emailAddress, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, id, entry.ID) | ||
require.Equal(t, emailAddress, entry.EmailAddress) | ||
} | ||
|
||
func TestWaitlistEntryCreate_Error(t *testing.T) { | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
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" | ||
}`), | ||
}, | ||
}, | ||
})) | ||
|
||
_, err := 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) | ||
} |