-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapi.go
170 lines (149 loc) · 4.21 KB
/
api.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
package bitmex
import (
"context"
"github.com/chuckpreslar/emission"
"github.com/frankrap/bitmex-api/recws"
"golang.org/x/net/proxy"
"log"
"net"
"time"
//"github.com/recws-org/recws"
"github.com/frankrap/bitmex-api/swagger"
"net/http"
"net/url"
"sync"
)
const (
HostReal = "www.bitmex.com"
HostTestnet = "testnet.bitmex.com"
)
type RateLimit struct {
Limit int64
Remaining int64
Reset int64
}
// BitMEX describes the API
type BitMEX struct {
Key string
Secret string
host string
proxyURL string
debugMode bool
ctx context.Context
timeout time.Duration
httpClient *http.Client
cfg *swagger.Configuration
client *swagger.APIClient
rateLimitMutexPublic sync.RWMutex
rateLimitMutex sync.RWMutex
rateLimitPublic RateLimit
rateLimit RateLimit
ws recws.RecConn
emitter *emission.Emitter
subscribeCmd *WSCmd
orderBookLocals map[string]*OrderBookLocal // key: symbol
orderLocals map[string]*swagger.Order // key: OrderID
orderBookLoaded map[string]bool // key: symbol
}
// New allows the use of the public or private and websocket api
func New(httpClient *http.Client, host string, key string, secret string, debugMode bool) *BitMEX {
b := &BitMEX{}
b.Key = key
b.Secret = secret
b.debugMode = debugMode
b.emitter = emission.NewEmitter()
b.orderBookLocals = make(map[string]*OrderBookLocal)
b.orderLocals = make(map[string]*swagger.Order)
b.orderBookLoaded = make(map[string]bool)
b.ws = recws.RecConn{
SubscribeHandler: b.subscribeHandler,
}
b.host = host
b.ctx = MakeContext(key, secret, host, 10)
b.timeout = 10 * time.Second
b.cfg = GetConfiguration(b.ctx)
if httpClient == nil {
httpClient = &http.Client{
Timeout: b.timeout,
}
}
b.httpClient = httpClient
b.cfg.HTTPClient = httpClient
b.client = swagger.NewAPIClient(b.cfg)
return b
}
// SetHttpProxy proxyURL: http://127.0.0.1:1080
func (b *BitMEX) SetHttpProxy(proxyURL string) error {
proxyURL_, err := url.Parse(proxyURL)
if err != nil {
return err
}
//adding the proxy settings to the Transport object
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL_),
}
//adding the Transport object to the http Client
client := &http.Client{
Transport: transport,
Timeout: b.timeout,
}
b.httpClient = client
b.cfg.HTTPClient = client
b.proxyURL = proxyURL
return nil
}
// SetProxy proxyURL: 127.0.0.1:1080
func (b *BitMEX) SetProxy(socks5Proxy string) error {
// socks5Proxy := "127.0.0.1:1080"
dialer, err := proxy.SOCKS5("tcp", socks5Proxy, nil, proxy.Direct)
if err != nil {
log.Fatal("Error creating dialer, aborting.")
}
dialFunc := func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
tr := &http.Transport{DialContext: dialFunc} // Dial: dialer.Dial,
client := &http.Client{Transport: tr, Timeout: b.timeout}
b.httpClient = client
b.cfg.HTTPClient = client
b.proxyURL = "http://" + socks5Proxy
return nil
}
func (b *BitMEX) GetRateLimit() RateLimit {
b.rateLimitMutex.RLock()
defer b.rateLimitMutex.RUnlock()
return b.rateLimit
}
func (b *BitMEX) GetRateLimitPublic() RateLimit {
b.rateLimitMutexPublic.RLock()
defer b.rateLimitMutexPublic.RUnlock()
return b.rateLimitPublic
}
func MakeContext(key string, secret string, host string, timeout int64) context.Context {
return context.WithValue(context.TODO(), swagger.ContextAPIKey, swagger.APIKey{
Key: key,
Secret: secret,
Host: host,
Timeout: timeout,
})
}
func GetConfiguration(ctx context.Context) *swagger.Configuration {
c := ctx.Value(swagger.ContextAPIKey).(swagger.APIKey)
cfg := &swagger.Configuration{
BasePath: "https://" + c.Host + "/api/v1",
DefaultHeader: make(map[string]string),
UserAgent: "Swagger-Codegen/1.0.0/go",
ExpireTime: 5, //seconds
}
return cfg
}
func GetClient(ctx context.Context) *swagger.APIClient {
c := ctx.Value(swagger.ContextAPIKey).(swagger.APIKey)
cfg := &swagger.Configuration{
BasePath: "https://" + c.Host + "/api/v1",
DefaultHeader: make(map[string]string),
UserAgent: "Swagger-Codegen/1.0.0/go",
ExpireTime: 5, //seconds
}
return swagger.NewAPIClient(cfg)
}