forked from zerodha/gokiteconnect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
179 lines (150 loc) · 5.66 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package kiteapi
import (
"crypto/sha256"
"fmt"
"net/http"
"net/url"
)
// UserSession represents the response after a successful authentication.
type UserSession struct {
UserProfile
UserSessionTokens
APIKey string `json:"api_key"`
PublicToken string `json:"public_token"`
LoginTime Time `json:"login_time"`
}
// UserSessionTokens represents response after renew access token.
type UserSessionTokens struct {
UserID string `json:"user_id"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
// Bank represents the details of a single bank account entry on a user's file.
type Bank struct {
Name string `json:"name"`
Branch string `json:"branch"`
Account string `json:"account"`
}
// UserProfile represents a user's personal and financial profile.
type UserProfile struct {
UserName string `json:"user_name"`
UserShortName string `json:"user_shortname"`
AvatarURL string `json:"avatar_url"`
UserType string `json:"user_type"`
Email string `json:"email"`
Phone string `json:"phone"`
Broker string `json:"broker"`
Products []string `json:"products"`
OrderTypes []string `json:"order_types"`
Exchanges []string `json:"exchanges"`
}
// Margins represents the user margins for a segment.
type Margins struct {
Category string `json:"-"`
Enabled bool `json:"enabled"`
Net float64 `json:"net"`
Available AvailableMargins `json:"available"`
Used UsedMargins `json:"utilised"`
}
// AvailableMargins represents the available margins from the margins response for a single segment.
type AvailableMargins struct {
AdHocMargin float64 `json:"adhoc_margin"`
Cash float64 `json:"cash"`
Collateral float64 `json:"collateral"`
IntradayPayin float64 `json:"intraday_payin"`
}
// UsedMargins represents the used margins from the margins response for a single segment.
type UsedMargins struct {
Debits float64 `json:"debits"`
Exposure float64 `json:"exposure"`
M2MRealised float64 `json:"m2m_realised"`
M2MUnrealised float64 `json:"m2m_unrealised"`
OptionPremium float64 `json:"option_premium"`
Payout float64 `json:"payout"`
Span float64 `json:"span"`
HoldingSales float64 `json:"holding_sales"`
Turnover float64 `json:"turnover"`
}
// AllMargins contains both equity and commodity margins.
type AllMargins struct {
Equity Margins `json:"equity"`
Commodity Margins `json:"commodity"`
}
// GenerateSession gets a user session details in exchange or request token.
// Access token is automatically set if the session is retrieved successfully.
// Do the token exchange with the `requestToken` obtained after the login flow,
// and retrieve the `accessToken` required for all subsequent requests. The
// response contains not just the `accessToken`, but metadata for the user who has authenticated.
func (c *Client) GenerateSession(requestToken string, apiSecret string) (UserSession, error) {
// Get SHA256 checksum
h := sha256.New()
h.Write([]byte(c.apiKey + requestToken + apiSecret))
// construct url values
params := url.Values{}
params.Add("api_key", c.apiKey)
params.Add("request_token", requestToken)
params.Set("checksum", fmt.Sprintf("%x", h.Sum(nil)))
var session UserSession
err := c.doEnvelope(http.MethodPost, URIUserSession, params, nil, &session)
// Set accessToken on successful session retrieve
if err != nil && session.AccessToken != "" {
c.SetAccessToken(session.AccessToken)
}
return session, err
}
func (c *Client) invalidateToken(tokenType string, token string) (bool, error) {
var b bool
// construct url values
params := url.Values{}
params.Add("api_key", c.apiKey)
params.Add(tokenType, token)
err := c.doEnvelope(http.MethodDelete, URIUserSessionInvalidate, params, nil, nil)
if err == nil {
b = true
}
return b, err
}
// InvalidateAccessToken invalidates the current access token.
func (c *Client) InvalidateAccessToken() (bool, error) {
return c.invalidateToken("access_token", c.accessToken)
}
// RenewAccessToken renews expired access token using valid refresh token.
func (c *Client) RenewAccessToken(refreshToken string, apiSecret string) (UserSessionTokens, error) {
// Get SHA256 checksum
h := sha256.New()
h.Write([]byte(c.apiKey + refreshToken + apiSecret))
// construct url values
params := url.Values{}
params.Add("api_key", c.apiKey)
params.Add("refresh_token", refreshToken)
params.Set("checksum", fmt.Sprintf("%x", h.Sum(nil)))
var session UserSessionTokens
err := c.doEnvelope(http.MethodPost, URIUserSessionRenew, params, nil, &session)
// Set accessToken on successful session retrieve
if err != nil && session.AccessToken != "" {
c.SetAccessToken(session.AccessToken)
}
return session, err
}
// InvalidateRefreshToken invalidates the given refresh token.
func (c *Client) InvalidateRefreshToken(refreshToken string) (bool, error) {
return c.invalidateToken("refresh_token", refreshToken)
}
// GetUserProfile gets user profile.
func (c *Client) GetUserProfile() (UserProfile, error) {
var userProfile UserProfile
err := c.doEnvelope(http.MethodGet, URIUserProfile, nil, nil, &userProfile)
return userProfile, err
}
// GetUserMargins gets all user margins.
func (c *Client) GetUserMargins() (AllMargins, error) {
var allUserMargins AllMargins
err := c.doEnvelope(http.MethodGet, URIUserMargins, nil, nil, &allUserMargins)
return allUserMargins, err
}
// GetUserSegmentMargins gets segmentwise user margins.
func (c *Client) GetUserSegmentMargins(segment string) (Margins, error) {
var margins Margins
err := c.doEnvelope(http.MethodGet, fmt.Sprintf(URIUserMarginsSegment, segment), nil, nil, &margins)
return margins, err
}