Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET /organization/:orgId/roles support #338

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/workos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def self.key
autoload :Profile, 'workos/profile'
autoload :ProfileAndToken, 'workos/profile_and_token'
autoload :RefreshAuthenticationResponse, 'workos/refresh_authentication_response'
autoload :Role, 'workos/role'
autoload :Session, 'workos/session'
autoload :SSO, 'workos/sso'
autoload :Types, 'workos/types'
Expand Down
26 changes: 26 additions & 0 deletions lib/workos/organizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ def delete_organization(id:)
response.is_a? Net::HTTPSuccess
end

# Retrieve a list of roles for the given organization.
#
# @param [String] organizationId The ID of the organization to fetch roles for.
def list_organization_roles(organization_id:)
response = execute_request(
request: get_request(
path: "/organizations/#{organization_id}/roles",
auth: true,
),
)

parsed_response = JSON.parse(response.body)

roles = parsed_response['data'].map do |role|
WorkOS::Role.new(role.to_json)
end

WorkOS::Types::ListStruct.new(
data: roles,
list_metadata: {
after: nil,
before: nil,
},
)
Comment on lines +200 to +206
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit silly. There's no pagination for this API, so perhaps I should just create a new type. Let me know your thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you could just return the roles array directly if you don't think it'll ever be a paginated API.

But if there's a chance it'll become paginated later, then returning this at least prevents the need to introduce a breaking change later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having to introduce a breaking change later is my worry. Pagination doesn't really make sense right now from a product perspective, but I worry if this will change as we see more use cases. I'll leave as-is for now.

end

private

def check_and_raise_organization_error(response:)
Expand Down
36 changes: 36 additions & 0 deletions lib/workos/role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module WorkOS
# The Role class provides a lightweight wrapper around
# a WorkOS Role resource. This class is not meant to be instantiated
# in user space, and is instantiated internally but exposed.
class Role
include HashProvider

attr_accessor :id, :name, :slug, :description, :type, :created_at, :updated_at

def initialize(json)
hash = JSON.parse(json, symbolize_names: true)

@id = hash[:id]
@name = hash[:name]
@slug = hash[:slug]
@description = hash[:description]
@type = hash[:type]
@created_at = hash[:created_at]
@updated_at = hash[:updated_at]
end

def to_json(*)
{
id: id,
name: name,
slug: slug,
description: description,
type: type,
created_at: created_at,
updated_at: updated_at,
}
end
end
end
18 changes: 18 additions & 0 deletions spec/lib/workos/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,22 @@
end
end
end

describe '.list_organization_roles' do
context 'with no options' do
it 'returns roles for organization' do
expected_metadata = {
after: nil,
before: nil,
}

VCR.use_cassette 'organization/list_organization_roles' do
roles = described_class.list_organization_roles(organization_id: 'org_01JEXP6Z3X7HE4CB6WQSH9ZAFE')

expect(roles.data.size).to eq(7)
expect(roles.list_metadata).to eq(expected_metadata)
end
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading