/
user.go
153 lines (132 loc) · 4.26 KB
/
user.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
package upvest
import (
"fmt"
"net/http"
"net/url"
"github.com/pkg/errors"
)
// UserService handles operations related to the user
// For more details see https://doc.upvest.co/reference#tenancy_user_create
type UserService struct {
service
}
// User is the resource representing your Upvest Tenant user.
// For more details see https://doc.upvest.co/reference#tenancy_user_create
type User struct {
Username string `json:"username,omitempty"`
RecoveryKit string `json:"recoverykit,omitempty"`
WalletIDs map[int]string `json:"wallet_ids,omitempty"`
}
// UserList is a list object for users.
type UserList struct {
Meta ListMeta
Values []User `json:"results"`
}
// ChangePasswordParams is the set of parameters that can be used when changing user password
// For more details see https://doc.upvest.co/reference#tenancy_user_password_update
type ChangePasswordParams struct {
//Params `json:"-"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}
// Create creates a new user
// For more details https://doc.upvest.co/reference#tenancy_user_create
func (s *UserService) Create(username, password string) (*User, error) {
u := "/tenancy/users/"
usr := &User{}
data := map[string]string{"username": username, "password": password}
p := &Params{}
p.SetAuthProvider(s.auth)
err := s.client.Call(http.MethodPost, u, data, usr, p)
return usr, err
}
// ChangePassword changes user password with the provided password
// For more details https://doc.upvest.co/reference#tenancy_user_password_update
func (s *UserService) ChangePassword(username string, params *ChangePasswordParams) (*User, error) {
u := fmt.Sprintf("/tenancy/users/%s", username)
usr := &User{}
p := &Params{}
p.SetAuthProvider(s.auth)
err := s.client.Call(http.MethodPatch, u, params, usr, p)
return usr, err
}
// Delete permanently deletes a user
// For more details https://doc.upvest.co/reference#tenancy_user_create
func (s *UserService) Delete(username string) error {
u := fmt.Sprintf("/tenancy/users/%s", username)
resp := &Response{}
p := &Params{}
p.SetAuthProvider(s.auth)
err := s.client.Call(http.MethodDelete, u, map[string]string{}, resp, p)
return err
}
// Get returns the details of a user.
// For more details see
func (s *UserService) Get(username string) (*User, error) {
u := fmt.Sprintf("/tenancy/users/%s", username)
user := &User{}
p := &Params{}
p.SetAuthProvider(s.auth)
err := s.client.Call(http.MethodGet, u, nil, user, p)
return user, err
}
// List returns list of all users.
// For more details see https://doc.upvest.co/reference#tenancy_user_list
func (s *UserService) List() (*UserList, error) {
path := "/tenancy/users/"
u, _ := url.Parse(path)
//q := u.Query()
//q.Set("page_size", fmt.Sprintf("%d", maxPageSize))
//u.RawQuery = q.Encode()
p := &Params{}
p.SetAuthProvider(s.auth)
var results []User
users := &UserList{}
for {
err := s.client.Call(http.MethodGet, u.String(), nil, users, p)
if err != nil {
return nil, errors.Wrap(err, "Could not retrieve list of users")
}
results = append(results, users.Values...)
// append page_size param to the returned params
u1, err := url.Parse(users.Meta.Next)
q := u1.Query()
q.Set("page_size", string(MaxPageSize))
u.RawQuery = q.Encode()
if users.Meta.Next == "" {
break
}
}
return &UserList{Values: results}, nil
}
// ListN returns a specific number of users
// For more details see https://doc.upvest.co/reference#tenancy_user_list
func (s *UserService) ListN(count int) (*UserList, error) {
path := "/tenancy/users/"
u, _ := url.Parse(path)
// q := u.Query()
// q.Set("page_size", fmt.Sprintf("%d", maxPageSize))
// u.RawQuery = q.Encode()
p := &Params{}
p.SetAuthProvider(s.auth)
var results []User
users := &UserList{}
total := 0
for total <= count {
err := s.client.Call(http.MethodGet, u.String(), nil, users, p)
if err != nil {
return nil, errors.Wrap(err, "Could not retrieve list of users")
}
results = append(results, users.Values...)
total += len(users.Values)
// append page_size param to the returned params
u1, err := url.Parse(users.Meta.Next)
q := u1.Query()
q.Set("page_size", string(MaxPageSize))
u.RawQuery = q.Encode()
if users.Meta.Next == "" {
break
}
}
return &UserList{Values: results}, nil
}