-
Notifications
You must be signed in to change notification settings - Fork 32
/
users.go
57 lines (47 loc) · 1.4 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
package registry
import (
"context"
"net/url"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/envelope"
)
// UsersClient represents the registry `/users` endpoints.
type UsersClient struct {
client RoundTripper
}
// Create attempts to register a new user
func (u *UsersClient) Create(ctx context.Context, userObj *envelope.User,
signup apitypes.Signup) (envelope.UserInf, error) {
v := &url.Values{}
if signup.InviteCode != "" {
v.Set("code", signup.InviteCode)
}
if signup.OrgInvite && signup.OrgName != "" {
v.Set("org", signup.OrgName)
}
if signup.Email != "" {
v.Set("email", signup.Email)
}
e := envelope.Unsigned{}
err := u.client.RoundTrip(ctx, "POST", "/users", v, &userObj, &e)
if err != nil {
return nil, err
}
return envelope.ConvertUser(&e)
}
// VerifyEmail will confirm the user's email with the registry
func (u *UsersClient) VerifyEmail(ctx context.Context, verifyCode string) error {
verify := apitypes.VerifyEmail{
Code: verifyCode,
}
return u.client.RoundTrip(ctx, "POST", "/users/verify", nil, &verify, nil)
}
// Update patches the user object with whitelisted fields
func (u *UsersClient) Update(ctx context.Context, userObj interface{}) (envelope.UserInf, error) {
e := envelope.Unsigned{}
err := u.client.RoundTrip(ctx, "PATCH", "/users/self", nil, userObj, &e)
if err != nil {
return nil, err
}
return envelope.ConvertUser(&e)
}