forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
161 lines (141 loc) · 5.25 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
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
// User describes a user account.
type User struct {
ID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Username string `json:"username,omitempty"`
Telephone string `json:"telephone,omitempty"`
Country string `json:"country,omitempty"`
Zipcode string `json:"zipcode,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
APIKey string `json:"api_key,omitempty"`
TwoFA bool `json:"two_factor_authentication_enabled,omitempty"`
Betas []string `json:"betas,omitempty"`
Accounts []Account `json:"organizations,omitempty"`
}
// UserResponse wraps a response containing User accounts.
type UserResponse struct {
Response
Result User `json:"result"`
}
// userBillingProfileResponse wraps a response containing Billing Profile information.
type userBillingProfileResponse struct {
Response
Result UserBillingProfile
}
// UserBillingProfile contains Billing Profile information.
type UserBillingProfile struct {
ID string `json:"id,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Address string `json:"address,omitempty"`
Address2 string `json:"address2,omitempty"`
Company string `json:"company,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
ZipCode string `json:"zipcode,omitempty"`
Country string `json:"country,omitempty"`
Telephone string `json:"telephone,omitempty"`
CardNumber string `json:"card_number,omitempty"`
CardExpiryYear int `json:"card_expiry_year,omitempty"`
CardExpiryMonth int `json:"card_expiry_month,omitempty"`
VAT string `json:"vat,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
EditedOn *time.Time `json:"edited_on,omitempty"`
}
type UserBillingHistoryResponse struct {
Response
Result []UserBillingHistory `json:"result"`
ResultInfo ResultInfo `json:"result_info"`
}
type UserBillingHistory struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Action string `json:"action,omitempty"`
Description string `json:"description,omitempty"`
OccurredAt *time.Time `json:"occurred_at,omitempty"`
Amount float32 `json:"amount,omitempty"`
Currency string `json:"currency,omitempty"`
Zone userBillingHistoryZone `json:"zone"`
}
type userBillingHistoryZone struct {
Name string `json:"name,omitempty"`
}
type UserBillingOptions struct {
PaginationOptions
Order string `url:"order,omitempty"`
Type string `url:"type,omitempty"`
OccurredAt *time.Time `url:"occurred_at,omitempty"`
Action string `url:"action,omitempty"`
}
// UserDetails provides information about the logged-in user.
//
// API reference: https://api.cloudflare.com/#user-user-details
func (api *API) UserDetails(ctx context.Context) (User, error) {
var r UserResponse
res, err := api.makeRequestContext(ctx, http.MethodGet, "/user", nil)
if err != nil {
return User{}, err
}
err = json.Unmarshal(res, &r)
if err != nil {
return User{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UpdateUser updates the properties of the given user.
//
// API reference: https://api.cloudflare.com/#user-update-user
func (api *API) UpdateUser(ctx context.Context, user *User) (User, error) {
var r UserResponse
res, err := api.makeRequestContext(ctx, http.MethodPatch, "/user", user)
if err != nil {
return User{}, err
}
err = json.Unmarshal(res, &r)
if err != nil {
return User{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UserBillingProfile returns the billing profile of the user.
//
// API reference: https://api.cloudflare.com/#user-billing-profile
func (api *API) UserBillingProfile(ctx context.Context) (UserBillingProfile, error) {
var r userBillingProfileResponse
res, err := api.makeRequestContext(ctx, http.MethodGet, "/user/billing/profile", nil)
if err != nil {
return UserBillingProfile{}, err
}
err = json.Unmarshal(res, &r)
if err != nil {
return UserBillingProfile{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UserBillingHistory return the billing history of the user
//
// API reference: https://api.cloudflare.com/#user-billing-history-billing-history-details
func (api *API) UserBillingHistory(ctx context.Context, pageOpts UserBillingOptions) ([]UserBillingHistory, error) {
uri := buildURI("/user/billing/history", pageOpts)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []UserBillingHistory{}, err
}
var r UserBillingHistoryResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []UserBillingHistory{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}