From 7a481ebef59c357a649e54bb0dfbdea50ed5f542 Mon Sep 17 00:00:00 2001 From: Konstantinos Pittas Date: Fri, 29 Nov 2024 14:20:31 +0200 Subject: [PATCH] feat: Add support for waitlist entries endpoints --- CHANGELOG.md | 1 + waitlist_entries.go | 18 ++++ waitlistentry/api.go | 25 +++++ waitlistentry/client.go | 70 ++++++++++++++ waitlistentry/waitlistentry_test.go | 139 ++++++++++++++++++++++++++++ 5 files changed, 253 insertions(+) create mode 100644 waitlist_entries.go create mode 100644 waitlistentry/api.go create mode 100644 waitlistentry/client.go create mode 100644 waitlistentry/waitlistentry_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b4e2f3..02cc3aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add support for the OAuth Applications API. Added the oauthapplication package for API operations and a clerk.OAuthApplication type. - Add support for multiple invitation templates with the `TemplateSlug` field in `invitation.Create`. +- Add support for listing and creating waitlist entries with the `waitlistentry.List` and `waitlistentry.Create` methods. ## 2.2.0 diff --git a/waitlist_entries.go b/waitlist_entries.go new file mode 100644 index 0000000..8eb0e9c --- /dev/null +++ b/waitlist_entries.go @@ -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"` +} diff --git a/waitlistentry/api.go b/waitlistentry/api.go new file mode 100644 index 0000000..4d6c54d --- /dev/null +++ b/waitlistentry/api.go @@ -0,0 +1,25 @@ +// Code generated by "gen"; DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. +package waitlistentry + +import ( + "context" + + "github.com/clerk/clerk-sdk-go/v2" +) + +// List returns all waitlist entries. +func List(ctx context.Context, params *ListParams) (*clerk.WaitlistEntriesList, error) { + return getClient().List(ctx, params) +} + +// Create adds a new waitlist entry. +func Create(ctx context.Context, params *CreateParams) (*clerk.WaitlistEntry, error) { + return getClient().Create(ctx, params) +} + +func getClient() *Client { + return &Client{ + Backend: clerk.GetBackend(), + } +} diff --git a/waitlistentry/client.go b/waitlistentry/client.go new file mode 100644 index 0000000..9924950 --- /dev/null +++ b/waitlistentry/client.go @@ -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 +} diff --git a/waitlistentry/waitlistentry_test.go b/waitlistentry/waitlistentry_test.go new file mode 100644 index 0000000..2e46487 --- /dev/null +++ b/waitlistentry/waitlistentry_test.go @@ -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":"foo@bar.com"}], + "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, "foo@bar.com", list.WaitlistEntries[0].EmailAddress) +} + +func TestWaitlistEntriesListWithParams(t *testing.T) { + limit := int64(10) + offset := int64(20) + orderBy := "-created_at" + query := "example@email.com" + 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":"foo@bar.com"}, + {"id":"wle_124","email_address":"baz@qux.com","invitation":{"id":"inv_124","email_address":"baz@qux.com"}} + ], + "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, "foo@bar.com", list.WaitlistEntries[0].EmailAddress) + require.Nil(t, list.WaitlistEntries[0].Invitation) + require.Equal(t, "wle_124", list.WaitlistEntries[1].ID) + require.Equal(t, "baz@qux.com", list.WaitlistEntries[1].EmailAddress) + require.NotNil(t, list.WaitlistEntries[1].Invitation) + require.Equal(t, "inv_124", list.WaitlistEntries[1].Invitation.ID) + require.Equal(t, "baz@qux.com", list.WaitlistEntries[1].Invitation.EmailAddress) +} + +func TestWaitlistEntryCreate(t *testing.T) { + emailAddress := "foo@bar.com" + 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) +}