forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
125 lines (109 loc) · 4.99 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
package easypost
import (
"context"
"fmt"
)
// 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"`
Phone *string `json:"phone,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) {
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.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) {
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.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) {
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.patch(ctx, 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.del(ctx, "users/"+userID)
}
// 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.get(ctx, "users", &out)
return
}
// 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.patch(ctx, updateBrandURL, newParams, &out)
return
}