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

feat: Add organization roles endpoints #173

Merged
merged 1 commit into from
Nov 7, 2023
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
43 changes: 22 additions & 21 deletions clerk/clerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,28 @@ const version = "1.48.4"
const (
ProdUrl = "https://api.clerk.dev/v1/"

ActorTokensUrl = "actor_tokens"
AllowlistsUrl = "allowlist_identifiers"
BlocklistsUrl = "blocklist_identifiers"
ClientsUrl = "clients"
ClientsVerifyUrl = ClientsUrl + "/verify"
DomainsURL = "domains"
EmailAddressesURL = "email_addresses"
EmailsUrl = "emails"
InvitationsURL = "invitations"
OrganizationsUrl = "organizations"
PhoneNumbersURL = "phone_numbers"
ProxyChecksURL = "proxy_checks"
RedirectURLsUrl = "redirect_urls"
SAMLConnectionsUrl = "saml_connections"
SessionsUrl = "sessions"
SMSUrl = "sms_messages"
TemplatesUrl = "templates"
UsersUrl = "users"
UsersCountUrl = UsersUrl + "/count"
WebhooksUrl = "webhooks"
JWTTemplatesUrl = "jwt_templates"
ActorTokensUrl = "actor_tokens"
AllowlistsUrl = "allowlist_identifiers"
BlocklistsUrl = "blocklist_identifiers"
ClientsUrl = "clients"
ClientsVerifyUrl = ClientsUrl + "/verify"
DomainsURL = "domains"
EmailAddressesURL = "email_addresses"
EmailsUrl = "emails"
InvitationsURL = "invitations"
OrganizationsUrl = "organizations"
OrganizationRolesUrl = "organization_roles"
PhoneNumbersURL = "phone_numbers"
ProxyChecksURL = "proxy_checks"
RedirectURLsUrl = "redirect_urls"
SAMLConnectionsUrl = "saml_connections"
SessionsUrl = "sessions"
SMSUrl = "sms_messages"
TemplatesUrl = "templates"
UsersUrl = "users"
UsersCountUrl = UsersUrl + "/count"
WebhooksUrl = "webhooks"
JWTTemplatesUrl = "jwt_templates"
)

var defaultHTTPClient = &http.Client{Timeout: time.Second * 5}
Expand Down
121 changes: 121 additions & 0 deletions clerk/instance_organization_roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package clerk

import (
"fmt"
"net/http"
"strconv"
)

type InsOrgRole struct {
Object string `json:"object"`
ID string `json:"id"`
Name string `json:"name"`
Key string `json:"key"`
Description string `json:"description"`
Permissions []InsOrgPermission `json:"permissions"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type InsOrgRolesResponse struct {
Data []InsOrgRole `json:"data"`
TotalCount int64 `json:"total_count"`
}

// TODO: move this to a separate file once custom permissions endpoints are done
type InsOrgPermission struct {
Object string `json:"object"`
ID string `json:"id"`
Name string `json:"name"`
Key string `json:"key"`
Description string `json:"description"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type CreateInsOrgRoleParams struct {
Name string `json:"name"`
Key string `json:"key"`
Description string `json:"description"`
Permissions []string `json:"permissions,omitempty"`
}

func (s *InstanceService) CreateOrganizationRole(params CreateInsOrgRoleParams) (*InsOrgRole, error) {
req, _ := s.client.NewRequest(http.MethodPost, OrganizationRolesUrl, &params)

var orgRole InsOrgRole
_, err := s.client.Do(req, &orgRole)
if err != nil {
return nil, err
}
return &orgRole, nil
}

type ListInsOrgRoleParams struct {
Limit *int `json:"limit,omitempty"`
Offset *int `json:"offset,omitempty"`
}

func (s *InstanceService) ListOrganizationRole(params ListInsOrgRoleParams) (*InsOrgRolesResponse, error) {
req, _ := s.client.NewRequest(http.MethodGet, OrganizationRolesUrl)

query := req.URL.Query()
if params.Limit != nil {
query.Set("limit", strconv.Itoa(*params.Limit))
}
if params.Offset != nil {
query.Set("offset", strconv.Itoa(*params.Offset))
}
req.URL.RawQuery = query.Encode()

var orgRolesResponse *InsOrgRolesResponse
_, err := s.client.Do(req, &orgRolesResponse)
if err != nil {
return nil, err
}
return orgRolesResponse, nil
}

func (s *InstanceService) ReadOrganizationRole(orgRoleID string) (*InsOrgRole, error) {
req, err := s.client.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", OrganizationRolesUrl, orgRoleID))
if err != nil {
return nil, err
}

var orgRole InsOrgRole
_, err = s.client.Do(req, &orgRole)
if err != nil {
return nil, err
}
return &orgRole, nil
}

type UpdateInsOrgRoleParams struct {
Name *string `json:"name,omitempty"`
Key *string `json:"key,omitempty"`
Description *string `json:"description,omitempty"`
Permissions *[]string `json:"permissions,omitempty"`
}

func (s *InstanceService) UpdateOrganizationRole(orgRoleID string, params UpdateInsOrgRoleParams) (*InsOrgRole, error) {
req, _ := s.client.NewRequest(http.MethodPatch, fmt.Sprintf("%s/%s", OrganizationRolesUrl, orgRoleID), &params)

var orgRole InsOrgRole
_, err := s.client.Do(req, &orgRole)
if err != nil {
return nil, err
}
return &orgRole, nil
}

func (s *InstanceService) DeleteOrganizationRole(orgRoleID string) (*DeleteResponse, error) {
req, _ := s.client.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s", OrganizationRolesUrl, orgRoleID))

var deleteResponse DeleteResponse
_, err := s.client.Do(req, &deleteResponse)
if err != nil {
return nil, err
}
return &deleteResponse, nil
}
Loading
Loading