From 6795e3a2709f94ad13914f6986462a76e195da9b Mon Sep 17 00:00:00 2001 From: Haris Chaniotakis Date: Thu, 16 Nov 2023 13:51:20 +0200 Subject: [PATCH] feat: Support 'order_by' & 'query' parameters on Organization Role & Permission List methods As part of the List operations for Organization Roles & Permissions, we now support the 'order_by' & 'query' parameters along with the regular pagination --- clerk/instance_organization_permissions.go | 14 ++++++++++++-- clerk/instance_organization_permissions_test.go | 12 ++++++++---- clerk/instance_organization_roles.go | 14 ++++++++++++-- clerk/instance_organization_roles_test.go | 16 +++++++++++++++- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/clerk/instance_organization_permissions.go b/clerk/instance_organization_permissions.go index bfeda599..30789dbe 100644 --- a/clerk/instance_organization_permissions.go +++ b/clerk/instance_organization_permissions.go @@ -5,8 +5,10 @@ import ( ) type ListInstanceOrganizationPermissionsParams struct { - Limit *int `json:"limit,omitempty"` - Offset *int `json:"offset,omitempty"` + Limit *int + Offset *int + Query *string + OrderBy *string } func (s *InstanceService) ListOrganizationPermissions(params ListInstanceOrganizationPermissionsParams) (*PermissionsResponse, error) { @@ -15,6 +17,14 @@ func (s *InstanceService) ListOrganizationPermissions(params ListInstanceOrganiz paginationParams := PaginationParams{Limit: params.Limit, Offset: params.Offset} query := req.URL.Query() addPaginationParams(query, paginationParams) + + if params.Query != nil { + query.Set("query", *params.Query) + } + if params.OrderBy != nil { + query.Set("order_by", *params.OrderBy) + } + req.URL.RawQuery = query.Encode() response := &PermissionsResponse{} diff --git a/clerk/instance_organization_permissions_test.go b/clerk/instance_organization_permissions_test.go index 8824c787..73b7b8e8 100644 --- a/clerk/instance_organization_permissions_test.go +++ b/clerk/instance_organization_permissions_test.go @@ -26,8 +26,10 @@ func TestInstanceService_List_happyPathWithParameters(t *testing.T) { actualQuery := req.URL.Query() expectedQuery := url.Values(map[string][]string{ - "limit": {"3"}, - "offset": {"2"}, + "limit": {"3"}, + "offset": {"2"}, + "query": {"my-query"}, + "order_by": {"created_at"}, }) assert.Equal(t, expectedQuery, actualQuery) fmt.Fprint(w, expectedResponse) @@ -37,8 +39,10 @@ func TestInstanceService_List_happyPathWithParameters(t *testing.T) { _ = json.Unmarshal([]byte(expectedResponse), want) got, _ := client.Instances().ListOrganizationPermissions(ListInstanceOrganizationPermissionsParams{ - Limit: intToPtr(3), - Offset: intToPtr(2), + Limit: intToPtr(3), + Offset: intToPtr(2), + Query: stringToPtr("my-query"), + OrderBy: stringToPtr("created_at"), }) if len(got.Data) != len(want.Data) { t.Errorf("Was expecting %d organization permissions to be returned, instead got %d", len(want.Data), len(got.Data)) diff --git a/clerk/instance_organization_roles.go b/clerk/instance_organization_roles.go index 65485d62..8e43d6e0 100644 --- a/clerk/instance_organization_roles.go +++ b/clerk/instance_organization_roles.go @@ -24,8 +24,10 @@ func (s *InstanceService) CreateOrganizationRole(params CreateInstanceOrganizati } type ListInstanceOrganizationRoleParams struct { - Limit *int `json:"limit,omitempty"` - Offset *int `json:"offset,omitempty"` + Limit *int + Offset *int + Query *string + OrderBy *string } func (s *InstanceService) ListOrganizationRole(params ListInstanceOrganizationRoleParams) (*RolesResponse, error) { @@ -34,6 +36,14 @@ func (s *InstanceService) ListOrganizationRole(params ListInstanceOrganizationRo paginationParams := PaginationParams{Limit: params.Limit, Offset: params.Offset} query := req.URL.Query() addPaginationParams(query, paginationParams) + + if params.Query != nil { + query.Set("query", *params.Query) + } + if params.OrderBy != nil { + query.Set("order_by", *params.OrderBy) + } + req.URL.RawQuery = query.Encode() var orgRolesResponse *RolesResponse diff --git a/clerk/instance_organization_roles_test.go b/clerk/instance_organization_roles_test.go index 2a994b77..7432c6f8 100644 --- a/clerk/instance_organization_roles_test.go +++ b/clerk/instance_organization_roles_test.go @@ -124,13 +124,27 @@ func TestOrganizationsService_List_happyPath(t *testing.T) { mux.HandleFunc("/organization_roles", func(w http.ResponseWriter, req *http.Request) { testHttpMethod(t, req, "GET") testHeader(t, req, "Authorization", "Bearer token") + + expectedQuery := url.Values{ + "limit": {"5"}, + "offset": {"6"}, + "query": {"my-query"}, + "order_by": {"created_at"}, + } + assert.Equal(t, expectedQuery, req.URL.Query()) + fmt.Fprint(w, expectedResponse) }) var want *RolesResponse _ = json.Unmarshal([]byte(expectedResponse), &want) - got, _ := client.Instances().ListOrganizationRole(ListInstanceOrganizationRoleParams{}) + got, _ := client.Instances().ListOrganizationRole(ListInstanceOrganizationRoleParams{ + Limit: intToPtr(5), + Offset: intToPtr(6), + Query: stringToPtr("my-query"), + OrderBy: stringToPtr("created_at"), + }) if len(got.Data) != len(want.Data) { t.Errorf("Was expecting %d organization roles to be returned, instead got %d", len(want.Data), len(got.Data)) }