forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.go
More file actions
102 lines (90 loc) · 3.93 KB
/
user.go
File metadata and controls
102 lines (90 loc) · 3.93 KB
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
package easypost
import "context"
// A User contains data about an EasyPost account and child accounts.
type User struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
ParentID string `json:"parent_id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
Balance string `json:"balance,omitempty"`
RechargeAmount string `json:"recharge_amount,omitempty"`
SecondaryRechargeAmount string `json:"secondary_recharge_amount,omitempty"`
RechargeThreshold string `json:"recharge_threshold,omitempty"`
Children []*User `json:"children,omitempty"`
APIKeys []*APIKey `json:"api_keys,omitempty"`
}
// UserOptions specifies options for creating or updating a user.
type UserOptions struct {
ID string `json:"-"`
Email *string `json:"email,omitempty"`
Password *string `json:"password,omitempty"`
PasswordConfirmation *string `json:"password_confirmation,omitempty"`
CurrentPassword *string `json:"current_password,omitempty"`
Name *string `json:"name,omitempty"`
PhoneNumber *string `json:"phone_number,omitempty"`
RechargeAmount *string `json:"recharge_amount,omitempty"`
SecondaryRechargeAmount *string `json:"secondary_recharge_amount,omitempty"`
RechargeThreshold *string `json:"recharge_threshold,omitempty"`
}
type userRequest struct {
UserOptions *UserOptions `json:"user,omitempty"`
}
// 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) {
err = c.post(nil, "users", &userRequest{UserOptions: in}, &out)
return
}
// 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.post(ctx, "users", &userRequest{UserOptions: in}, &out)
return
}
// GetUser retrieves a User object by ID.
func (c *Client) GetUser(userID string) (out *User, err error) {
err = c.get(nil, "users/"+userID, &out)
return
}
// 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.get(ctx, "users/"+userID, &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) {
req := userRequest{UserOptions: in}
path := "users"
if in.ID != "" {
path += "/" + in.ID
}
err = c.put(nil, path, req, &out)
return
}
// 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.put(ctx, path, req, &out)
return
}
// DeleteUser removes a child user.
func (c *Client) DeleteUser(userID string) error {
return c.del(nil, "users/"+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.del(ctx, "users/"+userID)
}