forked from nntaoli-project/goex
/
APIBuilder.go
270 lines (246 loc) · 8.02 KB
/
APIBuilder.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package builder
import (
"context"
"fmt"
. "github.com/nntaoli-project/GoEx"
"github.com/nntaoli-project/GoEx/bigone"
"github.com/nntaoli-project/GoEx/binance"
"github.com/nntaoli-project/GoEx/bitfinex"
"github.com/nntaoli-project/GoEx/bithumb"
"github.com/nntaoli-project/GoEx/bitmex"
"github.com/nntaoli-project/GoEx/bitstamp"
"github.com/nntaoli-project/GoEx/bittrex"
"github.com/nntaoli-project/GoEx/coinbene"
//"github.com/nntaoli-project/GoEx/coin58"
"github.com/nntaoli-project/GoEx/coinex"
"github.com/nntaoli-project/GoEx/fcoin"
"github.com/nntaoli-project/GoEx/gateio"
"github.com/nntaoli-project/GoEx/gdax"
"github.com/nntaoli-project/GoEx/hitbtc"
"github.com/nntaoli-project/GoEx/huobi"
"github.com/nntaoli-project/GoEx/kraken"
"github.com/nntaoli-project/GoEx/okcoin"
"github.com/nntaoli-project/GoEx/okex"
"github.com/nntaoli-project/GoEx/poloniex"
"github.com/nntaoli-project/GoEx/zb"
"net"
"net/http"
"net/url"
"time"
)
type APIBuilder struct {
HttpClientConfig *HttpClientConfig
client *http.Client
httpTimeout time.Duration
apiKey string
secretkey string
clientId string
apiPassphrase string
}
type HttpClientConfig struct {
HttpTimeout time.Duration
Proxy *url.URL
MaxIdleConns int
}
func (c HttpClientConfig) String() string {
return fmt.Sprintf("{ProxyUrl:\"%s\",HttpTimeout:%s,MaxIdleConns:%d}", c.Proxy, c.HttpTimeout.String(), c.MaxIdleConns)
}
func (c *HttpClientConfig) SetHttpTimeout(timeout time.Duration) *HttpClientConfig {
c.HttpTimeout = timeout
return c
}
func (c *HttpClientConfig) SetProxyUrl(proxyUrl string) *HttpClientConfig {
if proxyUrl == "" {
return c
}
proxy, err := url.Parse(proxyUrl)
if err != nil {
return c
}
c.Proxy = proxy
return c
}
func (c *HttpClientConfig) SetMaxIdleConns(max int) *HttpClientConfig {
c.MaxIdleConns = max
return c
}
var (
DefaultHttpClientConfig = &HttpClientConfig{
Proxy: nil,
HttpTimeout: 5 * time.Second,
MaxIdleConns: 10}
DefaultAPIBuilder = NewAPIBuilder()
)
func NewAPIBuilder() (builder *APIBuilder) {
return NewAPIBuilder2(DefaultHttpClientConfig)
}
func NewAPIBuilder2(config *HttpClientConfig) *APIBuilder {
if config == nil {
config = DefaultHttpClientConfig
}
return &APIBuilder{
HttpClientConfig: config,
client: &http.Client{
Timeout: config.HttpTimeout,
Transport: &http.Transport{
Proxy: func(request *http.Request) (*url.URL, error) {
return config.Proxy, nil
},
MaxIdleConns: config.MaxIdleConns,
IdleConnTimeout: 5 * config.HttpTimeout,
MaxConnsPerHost: 2,
MaxIdleConnsPerHost: 2,
TLSHandshakeTimeout: config.HttpTimeout,
ResponseHeaderTimeout: config.HttpTimeout,
ExpectContinueTimeout: config.HttpTimeout,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, config.HttpTimeout)
}},
}}
}
func NewCustomAPIBuilder(client *http.Client) (builder *APIBuilder) {
return &APIBuilder{client: client}
}
func (builder *APIBuilder) GetHttpClientConfig() *HttpClientConfig {
return builder.HttpClientConfig
}
func (builder *APIBuilder) GetHttpClient() *http.Client {
return builder.client
}
func (builder *APIBuilder) HttpProxy(proxyUrl string) (_builder *APIBuilder) {
if proxyUrl == "" {
return builder
}
proxy, err := url.Parse(proxyUrl)
if err != nil {
return builder
}
builder.HttpClientConfig.Proxy = proxy
transport := builder.client.Transport.(*http.Transport)
transport.Proxy = http.ProxyURL(proxy)
return builder
}
func (builder *APIBuilder) HttpTimeout(timeout time.Duration) (_builder *APIBuilder) {
builder.HttpClientConfig.HttpTimeout = timeout
builder.httpTimeout = timeout
builder.client.Timeout = timeout
transport := builder.client.Transport.(*http.Transport)
if transport != nil {
//transport.ResponseHeaderTimeout = timeout
//transport.TLSHandshakeTimeout = timeout
transport.IdleConnTimeout = timeout
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
}
return builder
}
func (builder *APIBuilder) APIKey(key string) (_builder *APIBuilder) {
builder.apiKey = key
return builder
}
func (builder *APIBuilder) APISecretkey(key string) (_builder *APIBuilder) {
builder.secretkey = key
return builder
}
func (builder *APIBuilder) ClientID(id string) (_builder *APIBuilder) {
builder.clientId = id
return builder
}
func (builder *APIBuilder) ApiPassphrase(apiPassphrase string) (_builder *APIBuilder) {
builder.apiPassphrase = apiPassphrase
return builder
}
func (builder *APIBuilder) Build(exName string) (api API) {
var _api API
switch exName {
//case OKCOIN_CN:
// _api = okcoin.New(builder.client, builder.apiKey, builder.secretkey)
case POLONIEX:
_api = poloniex.New(builder.client, builder.apiKey, builder.secretkey)
case OKCOIN_COM:
_api = okcoin.NewCOM(builder.client, builder.apiKey, builder.secretkey)
case BITSTAMP:
_api = bitstamp.NewBitstamp(builder.client, builder.apiKey, builder.secretkey, builder.clientId)
case HUOBI_PRO:
_api = huobi.NewHuoBiProSpot(builder.client, builder.apiKey, builder.secretkey)
case OKEX:
_api = okcoin.NewOKExSpot(builder.client, builder.apiKey, builder.secretkey)
case OKEX_V3:
_api = okex.NewOKEx(&APIConfig{
HttpClient: builder.client,
ApiKey: builder.apiKey,
ApiSecretKey: builder.secretkey,
ApiPassphrase: builder.apiPassphrase,
Endpoint: "https://www.okex.com",
})
case BITFINEX:
_api = bitfinex.New(builder.client, builder.apiKey, builder.secretkey)
case KRAKEN:
_api = kraken.New(builder.client, builder.apiKey, builder.secretkey)
case BINANCE:
_api = binance.New(builder.client, builder.apiKey, builder.secretkey)
case BITTREX:
_api = bittrex.New(builder.client, builder.apiKey, builder.secretkey)
case BITHUMB:
_api = bithumb.New(builder.client, builder.apiKey, builder.secretkey)
case GDAX:
_api = gdax.New(builder.client, builder.apiKey, builder.secretkey)
case GATEIO:
_api = gateio.New(builder.client, builder.apiKey, builder.secretkey)
case ZB:
_api = zb.New(builder.client, builder.apiKey, builder.secretkey)
case COINEX:
_api = coinex.New(builder.client, builder.apiKey, builder.secretkey)
case FCOIN:
_api = fcoin.NewFCoin(builder.client, builder.apiKey, builder.secretkey)
case FCOIN_MARGIN:
_api = fcoin.NewFcoinMargin(builder.client, builder.apiKey, builder.secretkey)
//case COIN58:
// _api = coin58.New58Coin(builder.client, builder.apiKey, builder.secretkey)
case BIGONE:
_api = bigone.New(builder.client, builder.apiKey, builder.secretkey)
case HITBTC:
_api = hitbtc.New(builder.client, builder.apiKey, builder.secretkey)
default:
println("exchange name error [" + exName + "].")
}
return _api
}
func (builder *APIBuilder) BuildFuture(exName string) (api FutureRestAPI) {
switch exName {
case BITMEX:
return bitmex.New(&APIConfig{
Endpoint: "https://www.bitmex.com/",
HttpClient: builder.client,
ApiKey: builder.apiKey,
ApiSecretKey: builder.secretkey})
case OKEX_FUTURE:
//return okcoin.NewOKEx(builder.client, builder.apiKey, builder.secretkey)
return okex.NewOKEx(&APIConfig{
HttpClient: builder.client,
Endpoint: "https://www.okex.com",
ApiKey: builder.apiKey,
ApiSecretKey: builder.secretkey,
ApiPassphrase: builder.apiPassphrase}).OKExFuture
case HBDM:
return huobi.NewHbdm(&APIConfig{HttpClient: builder.client, ApiKey: builder.apiKey, ApiSecretKey: builder.secretkey})
case OKEX_SWAP:
return okex.NewOKEx(&APIConfig{
HttpClient: builder.client,
Endpoint: "https://www.okex.com",
ApiKey: builder.apiKey,
ApiSecretKey: builder.secretkey,
ApiPassphrase: builder.apiPassphrase}).OKExSwap
case COINBENE:
return coinbene.NewCoinbeneSwap(APIConfig{
HttpClient: builder.client,
Endpoint: "http://openapi-contract.coinbene.com",
ApiKey: builder.apiKey,
ApiSecretKey: builder.secretkey,
})
default:
println(fmt.Sprintf("%s not support future", exName))
return nil
}
}