forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
96 lines (85 loc) · 3.34 KB
/
profile.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
package linodego
import (
"context"
"encoding/json"
)
// LishAuthMethod constants start with AuthMethod and include Linode API Lish Authentication Methods
type LishAuthMethod string
// LishAuthMethod constants are the methods of authentication allowed when connecting via Lish
const (
AuthMethodPasswordKeys LishAuthMethod = "password_keys"
AuthMethodKeysOnly LishAuthMethod = "keys_only"
AuthMethodDisabled LishAuthMethod = "disabled"
)
// ProfileReferrals represent a User's status in the Referral Program
type ProfileReferrals struct {
Total int `json:"total"`
Completed int `json:"completed"`
Pending int `json:"pending"`
Credit float64 `json:"credit"`
Code string `json:"code"`
URL string `json:"url"`
}
// Profile represents a Profile object
type Profile struct {
UID int `json:"uid"`
Username string `json:"username"`
Email string `json:"email"`
Timezone string `json:"timezone"`
EmailNotifications bool `json:"email_notifications"`
IPWhitelistEnabled bool `json:"ip_whitelist_enabled"`
TwoFactorAuth bool `json:"two_factor_auth"`
Restricted bool `json:"restricted"`
LishAuthMethod LishAuthMethod `json:"lish_auth_method"`
Referrals ProfileReferrals `json:"referrals"`
AuthorizedKeys []string `json:"authorized_keys"`
}
// ProfileUpdateOptions fields are those accepted by UpdateProfile
type ProfileUpdateOptions struct {
Email string `json:"email,omitempty"`
Timezone string `json:"timezone,omitempty"`
EmailNotifications *bool `json:"email_notifications,omitempty"`
IPWhitelistEnabled *bool `json:"ip_whitelist_enabled,omitempty"`
LishAuthMethod LishAuthMethod `json:"lish_auth_method,omitempty"`
AuthorizedKeys *[]string `json:"authorized_keys,omitempty"`
TwoFactorAuth *bool `json:"two_factor_auth,omitempty"`
Restricted *bool `json:"restricted,omitempty"`
}
// GetUpdateOptions converts a Profile to ProfileUpdateOptions for use in UpdateProfile
func (i Profile) GetUpdateOptions() (o ProfileUpdateOptions) {
o.Email = i.Email
o.Timezone = i.Timezone
o.EmailNotifications = copyBool(&i.EmailNotifications)
o.IPWhitelistEnabled = copyBool(&i.IPWhitelistEnabled)
o.LishAuthMethod = i.LishAuthMethod
authorizedKeys := make([]string, len(i.AuthorizedKeys))
copy(authorizedKeys, i.AuthorizedKeys)
o.AuthorizedKeys = &authorizedKeys
o.TwoFactorAuth = copyBool(&i.TwoFactorAuth)
o.Restricted = copyBool(&i.Restricted)
return
}
// GetProfile returns the Profile of the authenticated user
func (c *Client) GetProfile(ctx context.Context) (*Profile, error) {
e := "profile"
req := c.R(ctx).SetResult(&Profile{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Profile), nil
}
// UpdateProfile updates the Profile with the specified id
func (c *Client) UpdateProfile(ctx context.Context, opts ProfileUpdateOptions) (*Profile, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := "profile"
req := c.R(ctx).SetResult(&Profile{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*Profile), nil
}