-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathconnect.go
105 lines (84 loc) · 2.53 KB
/
connect.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
package smartapigo
import (
_ "fmt"
"net/http"
"time"
)
// Client represents interface for Kite Connect client.
type Client struct {
clientCode string
password string
accessToken string
debug bool
baseURI string
apiKey string
httpClient HTTPClient
}
const (
name string = "smartapi-go"
requestTimeout time.Duration = 7000 * time.Millisecond
baseURI string = "https://apiconnect.angelbroking.com/"
)
// New creates a new Smart API client.
func New(clientCode string,password string,apiKey string) *Client {
client := &Client{
clientCode: clientCode,
password: password,
apiKey: apiKey,
baseURI: baseURI,
}
// Create a default http handler with default timeout.
client.SetHTTPClient(&http.Client{
Timeout: requestTimeout,
})
return client
}
// SetHTTPClient overrides default http handler with a custom one.
// This can be used to set custom timeouts and transport.
func (c *Client) SetHTTPClient(h *http.Client) {
c.httpClient = NewHTTPClient(h, nil, c.debug)
}
// SetDebug sets debug mode to enable HTTP logs.
func (c *Client) SetDebug(debug bool) {
c.debug = debug
c.httpClient.GetClient().debug = debug
}
// SetBaseURI overrides the base SmartAPI endpoint with custom url.
func (c *Client) SetBaseURI(baseURI string) {
c.baseURI = baseURI
}
// SetTimeout sets request timeout for default http client.
func (c *Client) SetTimeout(timeout time.Duration) {
hClient := c.httpClient.GetClient().client
hClient.Timeout = timeout
}
// SetAccessToken sets the access token to the Kite Connect instance.
func (c *Client) SetAccessToken(accessToken string) {
c.accessToken = accessToken
}
func (c *Client) doEnvelope(method, uri string, params map[string]interface{}, headers http.Header, v interface{}, authorization ...bool) error {
if params == nil {
params = map[string]interface{}{}
}
// Send custom headers set
if headers == nil {
headers = map[string][]string{}
}
localIp,publicIp,mac,err := getIpAndMac()
if err != nil {
return err
}
// Add Kite Connect version to header
headers.Add("Content-Type", "application/json")
headers.Add("X-ClientLocalIP", localIp)
headers.Add("X-ClientPublicIP", publicIp)
headers.Add("X-MACAddress", mac)
headers.Add("Accept", "application/json")
headers.Add("X-UserType", "USER")
headers.Add("X-SourceID", "WEB")
headers.Add("X-PrivateKey",c.apiKey)
if authorization != nil && authorization[0]{
headers.Add("Authorization","Bearer "+c.accessToken)
}
return c.httpClient.DoEnvelope(method, c.baseURI+uri, params, headers, v)
}