-
Notifications
You must be signed in to change notification settings - Fork 20
/
users.go
206 lines (180 loc) · 6.19 KB
/
users.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package clerk
import (
"fmt"
"net/http"
"strconv"
)
type UsersService service
type User struct {
ID string `json:"id"`
Object string `json:"object"`
Username *string `json:"username"`
FirstName *string `json:"first_name"`
LastName *string `json:"last_name"`
Gender *string `json:"gender"`
Birthday *string `json:"birthday"`
ProfileImageURL string `json:"profile_image_url"`
PrimaryEmailAddressID *string `json:"primary_email_address_id"`
PrimaryPhoneNumberID *string `json:"primary_phone_number_id"`
PasswordEnabled bool `json:"password_enabled"`
TwoFactorEnabled bool `json:"two_factor_enabled"`
EmailAddresses []EmailAddress `json:"email_addresses"`
PhoneNumbers []PhoneNumber `json:"phone_numbers"`
ExternalAccounts []interface{} `json:"external_accounts"`
PublicMetadata interface{} `json:"public_metadata"`
PrivateMetadata interface{} `json:"private_metadata"`
LastSignInAt *int64 `json:"last_sign_in_at"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type EmailAddress struct {
ID string `json:"id"`
Object string `json:"object"`
EmailAddress string `json:"email_address"`
Verification interface{} `json:"verification"`
LinkedTo []IdentificationLink `json:"linked_to"`
}
type PhoneNumber struct {
ID string `json:"id"`
Object string `json:"object"`
PhoneNumber string `json:"phone_number"`
ReservedForSecondFactor bool `json:"reserved_for_second_factor"`
Verification interface{} `json:"verification"`
LinkedTo []IdentificationLink `json:"linked_to"`
}
type IdentificationLink struct {
IdentType string `json:"type"`
IdentID string `json:"id"`
}
type ListAllUsersParams struct {
Limit *int
Offset *int
EmailAddresses []string
PhoneNumbers []string
Web3Wallets []string
Usernames []string
UserIDs []string
Query *string
OrderBy *string
}
func (s *UsersService) ListAll(params ListAllUsersParams) ([]User, error) {
req, _ := s.client.NewRequest("GET", UsersUrl)
s.addUserSearchParamsToRequest(req, params)
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.OrderBy != nil {
query.Add("order_by", *params.OrderBy)
}
req.URL.RawQuery = query.Encode()
var users []User
_, err := s.client.Do(req, &users)
if err != nil {
return nil, err
}
return users, nil
}
type UserCount struct {
Object string `json:"object"`
TotalCount int `json:"total_count"`
}
func (s *UsersService) Count(params ListAllUsersParams) (*UserCount, error) {
req, _ := s.client.NewRequest("GET", UsersCountUrl)
s.addUserSearchParamsToRequest(req, params)
var userCount UserCount
_, err := s.client.Do(req, &userCount)
if err != nil {
return nil, err
}
return &userCount, nil
}
func (s *UsersService) addUserSearchParamsToRequest(r *http.Request, params ListAllUsersParams) {
query := r.URL.Query()
if params.EmailAddresses != nil {
for _, email := range params.EmailAddresses {
query.Add("email_address", email)
}
}
if params.PhoneNumbers != nil {
for _, phone := range params.PhoneNumbers {
query.Add("phone_number", phone)
}
}
if params.Web3Wallets != nil {
for _, web3Wallet := range params.Web3Wallets {
query.Add("web3_wallet", web3Wallet)
}
}
if params.Usernames != nil {
for _, username := range params.Usernames {
query.Add("username", username)
}
}
if params.UserIDs != nil {
for _, userID := range params.UserIDs {
query.Add("user_id", userID)
}
}
if params.Query != nil {
query.Add("query", *params.Query)
}
r.URL.RawQuery = query.Encode()
}
func (s *UsersService) Read(userId string) (*User, error) {
userUrl := fmt.Sprintf("%s/%s", UsersUrl, userId)
req, _ := s.client.NewRequest("GET", userUrl)
var user User
_, err := s.client.Do(req, &user)
if err != nil {
return nil, err
}
return &user, nil
}
func (s *UsersService) Delete(userId string) (*DeleteResponse, error) {
userUrl := fmt.Sprintf("%s/%s", UsersUrl, userId)
req, _ := s.client.NewRequest("DELETE", userUrl)
var delResponse DeleteResponse
if _, err := s.client.Do(req, &delResponse); err != nil {
return nil, err
}
return &delResponse, nil
}
type UpdateUser struct {
FirstName *string `json:"first_name,omitempty"`
LastName *string `json:"last_name,omitempty"`
PrimaryEmailAddressID *string `json:"primary_email_address_id,omitempty"`
PrimaryPhoneNumberID *string `json:"primary_phone_number_id,omitempty"`
ProfileImage *string `json:"profile_image,omitempty"`
Password *string `json:"password,omitempty"`
PublicMetadata interface{} `json:"public_metadata,omitempty"`
PrivateMetadata interface{} `json:"private_metadata,omitempty"`
}
func (s *UsersService) Update(userId string, updateRequest *UpdateUser) (*User, error) {
userUrl := fmt.Sprintf("%s/%s", UsersUrl, userId)
req, _ := s.client.NewRequest("PATCH", userUrl, updateRequest)
var updatedUser User
_, err := s.client.Do(req, &updatedUser)
if err != nil {
return nil, err
}
return &updatedUser, nil
}
type UpdateUserMetadata struct {
PublicMetadata interface{} `json:"public_metadata"`
PrivateMetadata interface{} `json:"private_metadata"`
UnsafeMetadata interface{} `json:"unsafe_metadata"`
}
func (s *UsersService) UpdateMetadata(userId string, updateMetadataRequest *UpdateUserMetadata) (*User, error) {
updateUserMetadataURL := fmt.Sprintf("%s/%s/metadata", UsersUrl, userId)
req, _ := s.client.NewRequest(http.MethodPatch, updateUserMetadataURL, updateMetadataRequest)
var updatedUser User
_, err := s.client.Do(req, &updatedUser)
if err != nil {
return nil, err
}
return &updatedUser, nil
}