-
Notifications
You must be signed in to change notification settings - Fork 15
/
organizations.go
105 lines (89 loc) · 3.33 KB
/
organizations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package clerk
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
type OrganizationsService service
type Organization struct {
Object string `json:"object"`
ID string `json:"id"`
Name string `json:"name"`
Slug *string `json:"slug"`
LogoURL *string `json:"logo_url"`
MembersCount *int `json:"members_count,omitempty"`
MaxAllowedMemberships int `json:"max_allowed_memberships"`
PublicMetadata json.RawMessage `json:"public_metadata"`
PrivateMetadata json.RawMessage `json:"private_metadata,omitempty"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type CreateOrganizationParams struct {
Name string `json:"name"`
Slug *string `json:"slug,omitempty"`
CreatedBy string `json:"created_by"`
MaxAllowedMemberships *int `json:"max_allowed_memberships,omitempty"`
PublicMetadata json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata json.RawMessage `json:"private_metadata,omitempty"`
}
func (s *OrganizationsService) Create(params CreateOrganizationParams) (*Organization, error) {
req, _ := s.client.NewRequest(http.MethodPost, OrganizationsUrl, ¶ms)
var organization Organization
_, err := s.client.Do(req, &organization)
if err != nil {
return nil, err
}
return &organization, nil
}
type UpdateOrganizationParams struct {
Name *string `json:"name,omitempty"`
MaxAllowedMemberships *int `json:"max_allowed_memberships,omitempty"`
}
func (s *OrganizationsService) Update(organizationID string, params UpdateOrganizationParams) (*Organization, error) {
req, _ := s.client.NewRequest(http.MethodPatch, fmt.Sprintf("%s/%s", OrganizationsUrl, organizationID), ¶ms)
var organization Organization
_, err := s.client.Do(req, &organization)
if err != nil {
return nil, err
}
return &organization, nil
}
func (s *OrganizationsService) Delete(organizationID string) (*DeleteResponse, error) {
req, _ := s.client.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s", OrganizationsUrl, organizationID))
var deleteResponse DeleteResponse
_, err := s.client.Do(req, &deleteResponse)
if err != nil {
return nil, err
}
return &deleteResponse, nil
}
type OrganizationsResponse struct {
Data []Organization `json:"data"`
TotalCount int64 `json:"total_count"`
}
type ListAllOrganizationsParams struct {
Limit *int
Offset *int
IncludeMembersCount bool
}
func (s *OrganizationsService) ListAll(params ListAllOrganizationsParams) (*OrganizationsResponse, error) {
req, _ := s.client.NewRequest(http.MethodGet, OrganizationsUrl)
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))
}
if params.IncludeMembersCount {
query.Set("include_members_count", strconv.FormatBool(params.IncludeMembersCount))
}
req.URL.RawQuery = query.Encode()
var organizationsResponse *OrganizationsResponse
_, err := s.client.Do(req, &organizationsResponse)
if err != nil {
return nil, err
}
return organizationsResponse, nil
}