forked from angel-one/smartapigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
178 lines (159 loc) · 4.41 KB
/
utils.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
package smartapigo
import (
"errors"
"io/ioutil"
"net"
"net/http"
"reflect"
"strings"
"time"
)
type Time struct {
time.Time
}
// API endpoints
const (
URILogin string = "rest/auth/angelbroking/user/v1/loginByPassword"
URIUserSessionRenew string = "rest/auth/angelbroking/jwt/v1/generateTokens"
URIUserProfile string = "rest/secure/angelbroking/user/v1/getProfile"
URILogout string = "rest/secure/angelbroking/user/v1/logout"
URIGetOrderBook string = "rest/secure/angelbroking/order/v1/getOrderBook"
URIPlaceOrder string = "rest/secure/angelbroking/order/v1/placeOrder"
URIModifyOrder string = "rest/secure/angelbroking/order/v1/modifyOrder"
URICancelOrder string = "rest/secure/angelbroking/order/v1/cancelOrder"
URIGetHoldings string = "rest/secure/angelbroking/portfolio/v1/getHolding"
URIGetPositions string = "rest/secure/angelbroking/order/v1/getPosition"
URIGetTradeBook string = "rest/secure/angelbroking/order/v1/getTradeBook"
URILTP string = "rest/secure/angelbroking/order/v1/getLtpData"
URIMARKETDATA string = "rest/secure/angelbroking/market/v1/quote"
URIOPTIONGREEKS string = "rest/secure/angelbroking/marketData/v1/optionGreek"
URIRMS string = "rest/secure/angelbroking/user/v1/getRMS"
URIConvertPosition string = "rest/secure/angelbroking/order/v1/convertPosition"
URIHitorical string = "rest/secure/angelbroking/historical/v1/getCandleData"
)
func structToMap(obj interface{}, tagName string) map[string]interface{} {
var values reflect.Value
switch con := obj.(type) {
case OrderParams:
{
values = reflect.ValueOf(&con).Elem()
}
case ModifyOrderParams:
{
values = reflect.ValueOf(&con).Elem()
}
case LTPParams:
{
values = reflect.ValueOf(&con).Elem()
}
case MarketDataRequest:
{
values = reflect.ValueOf(&con).Elem()
}
case OptionGreeksRequest:
{
values = reflect.ValueOf(&con).Elem()
}
case ConvertPositionParams:
{
values = reflect.ValueOf(&con).Elem()
}
case HistoricalDataRequest:
{
values = reflect.ValueOf(&con).Elem()
}
}
tags := reflect.TypeOf(obj)
params := make(map[string]interface{})
for i := 0; i < values.NumField(); i++ {
params[tags.Field(i).Tag.Get(tagName)] = values.Field(i).Interface()
}
return params
}
func getIpAndMac() (string, string, string, error) {
//----------------------
// Get the local machine IP address
//----------------------
var localIp, currentNetworkHardwareName string
localIp, err := getLocalIP()
if err != nil {
return "", "", "", err
}
// get all the system's or local machine's network interfaces
interfaces, _ := net.Interfaces()
for _, interf := range interfaces {
if addrs, err := interf.Addrs(); err == nil {
for _, addr := range addrs {
// only interested in the name with current IP address
if strings.Contains(addr.String(), localIp) {
currentNetworkHardwareName = interf.Name
}
}
}
}
// extract the hardware information base on the interface name
// capture above
netInterface, err := net.InterfaceByName(currentNetworkHardwareName)
if err != nil {
return "", "", "", err
}
macAddress := netInterface.HardwareAddr
// verify if the MAC address can be parsed properly
_, err = net.ParseMAC(macAddress.String())
if err != nil {
return "", "", "", err
}
publicIp, err := getPublicIp()
if err != nil {
return "", "", "", err
}
return localIp, publicIp, macAddress.String(), nil
}
func getLocalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("please check your network connection")
}
func getPublicIp() (string, error) {
resp, err := http.Get("https://myexternalip.com/raw")
if err != nil {
return "", err
}
content, _ := ioutil.ReadAll(resp.Body)
err = resp.Body.Close()
if err != nil {
return "", err
}
return string(content), nil
}