-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient.go
136 lines (126 loc) · 3.34 KB
/
client.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
package go5paisa
import (
"bytes"
"encoding/json"
// "fmt"
"errors"
"golang.org/x/net/publicsuffix"
"io/ioutil"
// "log"
"net/http"
"net/http/cookiejar"
"time"
)
const (
baseURL string = "https://Openapi.5paisa.com/VendorsAPI/Service1.svc"
loginRoute string = "/V2/LoginRequestMobileNewbyEmail"
marginRoute string = "/V3/Margin"
orderBookRoute string = "/V2/OrderBook"
holdingsRoute string = "/V2/Holding"
positionsRoute string = "/V1/NetPositionNetWise"
orderPlacementRoute string = "/V1/OrderRequest"
orderStatusRoute string = "/OrderStatus"
tradeInfoRoute string = "/TradeInformation"
// Request codes
marginRequestCode string = "5PMarginV3"
orderBookRequestCode string = "5POrdBkV2"
holdingsRequestCode string = "5PHoldingV2"
positionsRequestCode string = "5PNPNWV1"
tradeInfoRequestCode string = "5PTrdInfo"
orderStatusRequestCode string = "5POrdStatus"
orderPlacementRequestCode string = "5POrdReq"
loginRequestCode string = "5PLoginV2"
// Content Type
contentType string = "application/json"
)
// Config is the app configuration
type Config struct {
AppName string
AppSource string
UserID string
Password string
UserKey string
EncryptionKey string
}
// AppConfig is a reusable config struct
type AppConfig struct {
config *Config
head *payloadHead
}
//Client is the client configuration
type Client struct {
clientCode string
connection *http.Client
appConfig *AppConfig
}
// Init initializes the AppConfig struct
func Init(c *Config) *AppConfig {
head := &payloadHead{
AppName: c.AppName,
AppVer: "1.0",
Key: c.UserKey,
OsName: "WEB",
RequestCode: "",
UserID: c.UserID,
Password: c.Password,
}
appConfig := &AppConfig{
config: c,
head: head,
}
return appConfig
}
//Login logs in a client
func Login(conf *AppConfig, email string, password string, dob string) (*Client, error) {
encryptedEmail := encrypt(conf.config.EncryptionKey, email)
encryptedPassword := encrypt(conf.config.EncryptionKey, password)
encryptedDOB := encrypt(conf.config.EncryptionKey, dob)
var client *Client
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return client, err
}
httpClient := &http.Client{
Jar: jar,
Timeout: 10 * time.Second,
}
loginRequestBody := loginBody{
Email: encryptedEmail,
Password: encryptedPassword,
LocalIP: "192.168.1.1",
PublicIP: "192.168.1.1",
SerialNumber: "",
MAC: "",
MachineID: "039377",
VersionNo: "1.7",
RequestNo: "1",
My2PIN: encryptedDOB,
ConnectionType: "1",
}
conf.head.RequestCode = loginRequestCode
loginDetails := loginPayload{
Head: conf.head,
Body: loginRequestBody,
}
jsonValue, _ := json.Marshal(loginDetails)
res, err := httpClient.Post(baseURL+loginRoute, contentType, bytes.NewBuffer(jsonValue))
if err != nil {
return client, err
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return client, err
}
var b body
parseResBody(resBody, &b)
if b.ClientCode == "" || b.ClientCode == "INVALID CODE" {
return client, errors.New(b.Message)
}
client = &Client{
clientCode: b.ClientCode,
connection: httpClient,
appConfig: conf,
}
return client, nil
}