-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathuser.go
229 lines (195 loc) · 7.61 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package kiteconnect
import (
"crypto/sha256"
"fmt"
"net/http"
"net/url"
"github.com/zerodha/gokiteconnect/v4/models"
)
// UserSession represents the response after a successful authentication.
type UserSession struct {
UserProfile
UserSessionTokens
UserID string `json:"user_id"`
APIKey string `json:"api_key"`
PublicToken string `json:"public_token"`
LoginTime models.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"`
}
// UserMeta contains meta data of the user.
type UserMeta struct {
DematConsent string `json:"demat_consent"`
}
// FullUserMeta contains full meta data of the user.
type FullUserMeta struct {
DematConsent string `json:"poa"`
Silo string `json:"silo"`
AccountBlocks []string `json:"account_blocks"`
}
// UserProfile represents a user's personal and financial profile.
type UserProfile struct {
UserID string `json:"user_id"`
UserName string `json:"user_name"`
UserShortName string `json:"user_shortname"`
AvatarURL string `json:"avatar_url"`
UserType string `json:"user_type"`
Email string `json:"email"`
Broker string `json:"broker"`
Meta UserMeta `json:"meta"`
Products []string `json:"products"`
OrderTypes []string `json:"order_types"`
Exchanges []string `json:"exchanges"`
}
type FullUserProfile struct {
UserID string `json:"user_id"`
UserName string `json:"user_name"`
AvatarURL string `json:"avatar_url"`
UserType string `json:"user_type"`
Email string `json:"email"`
Phone string `json:"phone"`
Broker string `json:"broker"`
TwoFAType string `json:"twofa_type"`
Banks []Bank `json:"bank_accounts"`
DPIDs []string `json:"dp_ids"`
Products []string `json:"products"`
OrderTypes []string `json:"order_types"`
Exchanges []string `json:"exchanges"`
PAN string `json:"pan"`
UserShortName string `json:"user_shortname"`
Tags []string `json:"tags"`
PasswordTimestamp models.Time `json:"password_timestamp"`
TwoFATimestamp models.Time `json:"twofa_timestamp"`
Meta FullUserMeta `json:"meta"`
}
// 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"`
LiveBalance float64 `json:"live_balance"`
OpeningBalance float64 `json:"opening_balance"`
}
// 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"`
LiquidCollateral float64 `json:"liquid_collateral"`
StockCollateral float64 `json:"stock_collateral"`
Delivery float64 `json:"delivery"`
}
// 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
}
// GetFullUserProfile gets full user profile.
func (c *Client) GetFullUserProfile() (FullUserProfile, error) {
var fUserProfile FullUserProfile
err := c.doEnvelope(http.MethodGet, URIFullUserProfile, nil, nil, &fUserProfile)
return fUserProfile, 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
}