From 725fb708cf0a1d39df85dd8f814d480735b6d561 Mon Sep 17 00:00:00 2001 From: Brandon Romano Date: Wed, 4 Dec 2024 15:41:14 -0800 Subject: [PATCH] Adds support for fetching orgs with include_members_count --- organization/client.go | 20 ++++++++++++++++++++ organization/client_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/organization/client.go b/organization/client.go index 8b1c4dd..a34c9e6 100644 --- a/organization/client.go +++ b/organization/client.go @@ -48,14 +48,34 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Organ return organization, err } +type GetParams struct { + clerk.APIParams + IncludeMembersCount *bool `json:"include_members_count,omitempty"` +} + +func (params *GetParams) ToQuery() url.Values { + q := url.Values{} + if params.IncludeMembersCount != nil { + q.Set("include_members_count", strconv.FormatBool(*params.IncludeMembersCount)) + } + return q +} + // Get retrieves details for an organization. // The organization can be fetched by either the ID or its slug. func (c *Client) Get(ctx context.Context, idOrSlug string) (*clerk.Organization, error) { + return c.GetWithParams(ctx, idOrSlug, nil) +} + +// GetWithParams retrieves details for an organization. +// The organization can be fetched by either the ID or its slug. +func (c *Client) GetWithParams(ctx context.Context, idOrSlug string, params *GetParams) (*clerk.Organization, error) { path, err := clerk.JoinPath(path, idOrSlug) if err != nil { return nil, err } req := clerk.NewAPIRequest(http.MethodGet, path) + req.SetParams(params) organization := &clerk.Organization{} err = c.Backend.Call(ctx, req, organization) return organization, err diff --git a/organization/client_test.go b/organization/client_test.go index 4809e1e..622b370 100644 --- a/organization/client_test.go +++ b/organization/client_test.go @@ -82,6 +82,31 @@ func TestOrganizationClientGet(t *testing.T) { require.Equal(t, name, organization.Name) } +func TestOrganizationClientGetWithParams(t *testing.T) { + t.Parallel() + id := "org_123" + name := "Acme Inc" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","name":"%s"}`, id, name)), + Method: http.MethodGet, + Path: "/v1/organizations/" + id, + Query: &url.Values{ + "include_members_count": []string{"true"}, + }, + }, + } + client := NewClient(config) + organization, err := client.GetWithParams(context.Background(), id, &GetParams{ + IncludeMembersCount: clerk.Bool(true), + }) + require.NoError(t, err) + require.Equal(t, id, organization.ID) + require.Equal(t, name, organization.Name) +} + func TestOrganizationClientUpdate(t *testing.T) { t.Parallel() id := "org_123"