forked from yonch/go-tdameritrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
83 lines (69 loc) · 2.82 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
package tdameritrade
import (
"context"
"fmt"
"strings"
)
// GetPreferences returns Preferences for a specific account.
// See https://developer.tdameritrade.com/user-principal/apis/get/accounts/%7BaccountId%7D/preferences-0
func (s *UserService) GetPreferences(ctx context.Context, accountID string) (*Preferences, *Response, error) {
u := fmt.Sprintf("accounts/%s/preferences", accountID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
preferences := new(Preferences)
resp, err := s.client.Do(ctx, req, preferences)
if err != nil {
return nil, resp, err
}
return preferences, resp, err
}
// GetStreamerSubscriptionKeys returns Subscription Keys for provided accounts or default accounts.
// See https://developer.tdameritrade.com/user-principal/apis/get/userprincipals/streamersubscriptionkeys-0
func (s *UserService) GetStreamerSubscriptionKeys(ctx context.Context, accountIDs ...string) (*StreamerSubscriptionKeys, *Response, error) {
u := fmt.Sprintf("userprincipals/streamersubscriptionkeys?accountIds=%s", strings.Join(accountIDs, ","))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
streamerSubscriptionKeys := new(StreamerSubscriptionKeys)
resp, err := s.client.Do(ctx, req, streamerSubscriptionKeys)
if err != nil {
return nil, resp, err
}
return streamerSubscriptionKeys, resp, err
}
// GetUserPrincipals returns User Principal details.
// Valid values for `fields` are "streamerSubscriptionKeys", "streamerConnectionInfo", "preferences" and "surrogateIds"
// See https://developer.tdameritrade.com/user-principal/apis/get/userprincipals-0
func (s *UserService) GetUserPrincipals(ctx context.Context, fields ...string) (*UserPrincipal, *Response, error) {
u := "userprincipals"
if len(fields) > 0 {
u = fmt.Sprintf("%s?fields=%s", u, strings.Join(fields, ","))
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
userPrincipal := new(UserPrincipal)
resp, err := s.client.Do(ctx, req, userPrincipal)
if err != nil {
return nil, resp, err
}
return userPrincipal, resp, err
}
// UpdatePreferences updates Preferences for a specific account.
// Please note that the directOptionsRouting and directEquityRouting values cannot be modified via this operation, even though they are in the request body.
// See https://developer.tdameritrade.com/user-principal/apis/put/accounts/%7BaccountId%7D/preferences-0
func (s *UserService) UpdatePreferences(ctx context.Context, accountID string, newPreferences *Preferences) (*Response, error) {
if newPreferences == nil {
return nil, fmt.Errorf("newPreferences is nil")
}
u := fmt.Sprintf("accounts/%s/preferences", accountID)
req, err := s.client.NewRequest("PUT", u, newPreferences)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}