-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Organization Memberships API (#231)
Added support for the Organization Memberships API. Available operations are Create, Update, Delete and List.
- Loading branch information
Showing
8 changed files
with
436 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package clerk | ||
|
||
import "encoding/json" | ||
|
||
type OrganizationMembership struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
Organization *Organization `json:"organization"` | ||
Permissions []string `json:"permissions"` | ||
PublicMetadata json.RawMessage `json:"public_metadata"` | ||
PrivateMetadata json.RawMessage `json:"private_metadata"` | ||
Role string `json:"role"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
PublicUserData *OrganizationMembershipPublicUserData `json:"public_user_data,omitempty"` | ||
} | ||
|
||
type OrganizationMembershipList struct { | ||
APIResource | ||
OrganizationMemberships []*OrganizationMembership `json:"data"` | ||
TotalCount int64 `json:"total_count"` | ||
} | ||
|
||
type OrganizationMembershipPublicUserData struct { | ||
UserID string `json:"user_id"` | ||
FirstName *string `json:"first_name"` | ||
LastName *string `json:"last_name"` | ||
ImageURL *string `json:"image_url"` | ||
HasImage bool `json:"has_image"` | ||
Identifier string `json:"identifier"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Package organizationmembership provides the Organization Memberships API. | ||
package organizationmembership | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
) | ||
|
||
//go:generate go run ../cmd/gen/main.go | ||
|
||
const path = "/organizations" | ||
|
||
// Client is used to invoke the Organization Memberships API. | ||
type Client struct { | ||
Backend clerk.Backend | ||
} | ||
|
||
type ClientConfig struct { | ||
clerk.BackendConfig | ||
} | ||
|
||
func NewClient(config *ClientConfig) *Client { | ||
return &Client{ | ||
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
||
type CreateParams struct { | ||
clerk.APIParams | ||
UserID *string `json:"user_id,omitempty"` | ||
Role *string `json:"role,omitempty"` | ||
OrganizationID string `json:"-"` | ||
} | ||
|
||
// Create adds a new member to the organization. | ||
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationMembership, error) { | ||
path, err := clerk.JoinPath(path, params.OrganizationID, "/memberships") | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
req.SetParams(params) | ||
membership := &clerk.OrganizationMembership{} | ||
err = c.Backend.Call(ctx, req, membership) | ||
return membership, err | ||
} | ||
|
||
type UpdateParams struct { | ||
clerk.APIParams | ||
Role *string `json:"role,omitempty"` | ||
OrganizationID string `json:"-"` | ||
UserID string `json:"-"` | ||
} | ||
|
||
// Update updates an organization membership. | ||
func (c *Client) Update(ctx context.Context, params *UpdateParams) (*clerk.OrganizationMembership, error) { | ||
path, err := clerk.JoinPath(path, params.OrganizationID, "/memberships", params.UserID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodPatch, path) | ||
req.SetParams(params) | ||
membership := &clerk.OrganizationMembership{} | ||
err = c.Backend.Call(ctx, req, membership) | ||
return membership, err | ||
} | ||
|
||
type DeleteParams struct { | ||
clerk.APIParams | ||
OrganizationID string `json:"-"` | ||
UserID string `json:"-"` | ||
} | ||
|
||
// Delete removes a member from an organization. | ||
func (c *Client) Delete(ctx context.Context, params *DeleteParams) (*clerk.OrganizationMembership, error) { | ||
path, err := clerk.JoinPath(path, params.OrganizationID, "/memberships", params.UserID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodDelete, path) | ||
membership := &clerk.OrganizationMembership{} | ||
err = c.Backend.Call(ctx, req, membership) | ||
return membership, err | ||
} | ||
|
||
type ListParams struct { | ||
clerk.APIParams | ||
clerk.ListParams | ||
OrderBy *string `json:"order_by,omitempty"` | ||
Query *string `json:"query,omitempty"` | ||
Roles []string `json:"role,omitempty"` | ||
UserIDs []string `json:"user_id,omitempty"` | ||
EmailAddresses []string `json:"email_address,omitempty"` | ||
PhoneNumbers []string `json:"phone_number,omitempty"` | ||
Usernames []string `json:"username,omitempty"` | ||
Web3Wallets []string `json:"web3_wallet,omitempty"` | ||
OrganizationID string `json:"-"` | ||
} | ||
|
||
// ToQuery returns the parameters as url.Values so they can be used | ||
// in a URL query string. | ||
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) | ||
} | ||
if params.Roles != nil { | ||
q["role"] = params.Roles | ||
} | ||
if params.UserIDs != nil { | ||
q["user_id"] = params.UserIDs | ||
} | ||
if params.EmailAddresses != nil { | ||
q["email_address"] = params.EmailAddresses | ||
} | ||
if params.PhoneNumbers != nil { | ||
q["phone_number"] = params.PhoneNumbers | ||
} | ||
if params.Usernames != nil { | ||
q["username"] = params.Usernames | ||
} | ||
if params.Web3Wallets != nil { | ||
q["web3_wallet"] = params.Web3Wallets | ||
} | ||
return q | ||
} | ||
|
||
// List returns a list of organization memberships. | ||
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.OrganizationMembershipList, error) { | ||
path, err := clerk.JoinPath(path, params.OrganizationID, "/memberships") | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodGet, path) | ||
req.SetParams(params) | ||
list := &clerk.OrganizationMembershipList{} | ||
err = c.Backend.Call(ctx, req, list) | ||
return list, err | ||
} |
Oops, something went wrong.