-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuser.go
175 lines (152 loc) · 8.09 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package easypost
import (
"context"
"fmt"
"net/http"
)
// A User contains data about an EasyPost account and child accounts.
type User struct {
ID string `json:"id,omitempty" url:"id,omitempty"`
Object string `json:"object,omitempty" url:"object,omitempty"`
ParentID string `json:"parent_id,omitempty" url:"parent_id,omitempty"`
Name string `json:"name,omitempty" url:"name,omitempty"`
Email string `json:"email,omitempty" url:"email,omitempty"`
PhoneNumber string `json:"phone_number,omitempty" url:"phone_number,omitempty"`
Balance string `json:"balance,omitempty" url:"balance,omitempty"`
RechargeAmount string `json:"recharge_amount,omitempty" url:"recharge_amount,omitempty"`
SecondaryRechargeAmount string `json:"secondary_recharge_amount,omitempty" url:"secondary_recharge_amount,omitempty"`
RechargeThreshold string `json:"recharge_threshold,omitempty" url:"recharge_threshold,omitempty"`
Children []*User `json:"children,omitempty" url:"children,omitempty"`
APIKeys []*APIKey `json:"api_keys,omitempty" url:"api_keys,omitempty"`
Verified bool `json:"verified,omitempty" url:"verified,omitempty"`
}
// UserOptions specifies options for creating or updating a user.
type UserOptions struct {
ID string `json:"-"` // IGNORE
Email *string `json:"email,omitempty" url:"email,omitempty"`
Password *string `json:"password,omitempty" url:"password,omitempty"`
PasswordConfirmation *string `json:"password_confirmation,omitempty" url:"password_confirmation,omitempty"`
CurrentPassword *string `json:"current_password,omitempty" url:"current_password,omitempty"`
Name *string `json:"name,omitempty" url:"name,omitempty"`
Phone *string `json:"phone,omitempty" url:"phone,omitempty"`
PhoneNumber *string `json:"phone_number,omitempty" url:"phone_number,omitempty"`
RechargeAmount *string `json:"recharge_amount,omitempty" url:"recharge_amount,omitempty"`
SecondaryRechargeAmount *string `json:"secondary_recharge_amount,omitempty" url:"secondary_recharge_amount,omitempty"`
RechargeThreshold *string `json:"recharge_threshold,omitempty" url:"recharge_threshold,omitempty"`
}
type userRequest struct {
UserOptions *UserOptions `json:"user,omitempty" url:"user,omitempty"`
}
type ListChildUsersResult struct {
Children []*User `json:"children,omitempty" url:"children,omitempty"`
PaginatedCollection
}
// CreateUser creates a new child user.
//
// c := easypost.New(MyEasyPostAPIKey)
// opts := &easypost.UserOptions{Name: easypost.StringPtr("Child User")}
// out, err := c.CreateUser(opts)
func (c *Client) CreateUser(in *UserOptions) (out *User, err error) {
return c.CreateUserWithContext(context.Background(), in)
}
// CreateUserWithContext performs the same operation as CreateUser, but allows
// specifying a context that can interrupt the request.
func (c *Client) CreateUserWithContext(ctx context.Context, in *UserOptions) (out *User, err error) {
err = c.do(ctx, http.MethodPost, "users", &userRequest{UserOptions: in}, &out)
return
}
// GetUser retrieves a User object by ID.
func (c *Client) GetUser(userID string) (out *User, err error) {
return c.GetUserWithContext(context.Background(), userID)
}
// GetUserWithContext performs the same operation as GetUser, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetUserWithContext(ctx context.Context, userID string) (out *User, err error) {
err = c.do(ctx, http.MethodGet, "users/"+userID, nil, &out)
return
}
// UpdateUser updates a user with the attributes given in the UpdateUserOptions
// parameter. If the ID field of UpdateUserOptions is empty, the operation is
// done on the current user. All other fields are updated if they are non-nil.
func (c *Client) UpdateUser(in *UserOptions) (out *User, err error) {
return c.UpdateUserWithContext(context.Background(), in)
}
// UpdateUserWithContext performs the same operation as UpdateUser, but allows
// specifying a context that can interrupt the request.
func (c *Client) UpdateUserWithContext(ctx context.Context, in *UserOptions) (out *User, err error) {
req := userRequest{UserOptions: in}
path := "users"
if in.ID != "" {
path += "/" + in.ID
}
err = c.do(ctx, http.MethodPatch, path, req, &out)
return
}
// DeleteUser removes a child user.
func (c *Client) DeleteUser(userID string) error {
return c.DeleteUserWithContext(context.Background(), userID)
}
// DeleteUserWithContext performs the same operation as DeleteUser, but allows
// specifying a context that can interrupt the request.
func (c *Client) DeleteUserWithContext(ctx context.Context, userID string) error {
return c.do(ctx, http.MethodDelete, "users/"+userID, nil, nil)
}
// RetrieveMe retrieves the current user.
func (c *Client) RetrieveMe() (out *User, err error) {
return c.RetrieveMeWithContext(context.Background())
}
// RetrieveMeWithContext performs the same operation as RetrieveMe, but allows
// specifying a context that can interrupt the request.
func (c *Client) RetrieveMeWithContext(ctx context.Context) (out *User, err error) {
err = c.do(ctx, http.MethodGet, "users", nil, &out)
return
}
// ListChildUsers retrieves a list of child users.
func (c *Client) ListChildUsers(opts *ListOptions) (out *ListChildUsersResult, err error) {
return c.ListChildUsersWithContext(context.Background(), opts)
}
// ListChildUsersWithContext performs the same operation as ListChildUsers, but allows
// specifying a context that can interrupt the request.
func (c *Client) ListChildUsersWithContext(ctx context.Context, opts *ListOptions) (out *ListChildUsersResult, err error) {
err = c.do(ctx, http.MethodGet, "users/children", opts, &out)
return
}
// GetNextChildUserPage returns the next page of child users.
func (c *Client) GetNextChildUserPage(collection *ListChildUsersResult) (out *ListChildUsersResult, err error) {
return c.GetNextChildUserPageWithContext(context.Background(), collection)
}
// GetNextChildUserPageWithPageSize returns the next page of child users with a specific page size.
func (c *Client) GetNextChildUserPageWithPageSize(collection *ListChildUsersResult, pageSize int) (out *ListChildUsersResult, err error) {
return c.GetNextChildUserPageWithPageSizeWithContext(context.Background(), collection, pageSize)
}
// GetNextChildUserPageWithContext performs the same operation as GetNextChildUserPage, but
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextChildUserPageWithContext(ctx context.Context, collection *ListChildUsersResult) (out *ListChildUsersResult, err error) {
return c.GetNextChildUserPageWithPageSizeWithContext(ctx, collection, 0)
}
// GetNextChildUserPageWithPageSizeWithContext performs the same operation as GetNextChildUserPageWithPageSize, but
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextChildUserPageWithPageSizeWithContext(ctx context.Context, collection *ListChildUsersResult, pageSize int) (out *ListChildUsersResult, err error) {
if len(collection.Children) == 0 {
err = EndOfPaginationError
return
}
lastID := collection.Children[len(collection.Children)-1].ID
params, err := nextChildUserPageParameters(collection.HasMore, lastID, pageSize)
if err != nil {
return
}
return c.ListChildUsersWithContext(ctx, params)
}
// UpdateBrand updates the user brand.
func (c *Client) UpdateBrand(params map[string]interface{}, userID string) (out *Brand, err error) {
return c.UpdateBrandWithContext(context.Background(), params, userID)
}
// UpdateBrandWithContext performs the same operation as UpdateBrand, but allows
// specifying a context that can interrupt the request.
func (c *Client) UpdateBrandWithContext(ctx context.Context, params map[string]interface{}, userID string) (out *Brand, err error) {
newParams := map[string]interface{}{"brand": params}
updateBrandURL := fmt.Sprintf("users/%s/brand", userID)
err = c.do(ctx, http.MethodPatch, updateBrandURL, newParams, &out)
return
}