-
Notifications
You must be signed in to change notification settings - Fork 121
/
service_def.go
112 lines (99 loc) · 3.24 KB
/
service_def.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
package binance
import (
"context"
"fmt"
"net/http"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
)
// Service represents service layer for Binance API.
//
// The main purpose for this layer is to be replaced with dummy implementation
// if necessary without need to replace Binance instance.
type Service interface {
Ping() error
Time() (time.Time, error)
OrderBook(obr OrderBookRequest) (*OrderBook, error)
AggTrades(atr AggTradesRequest) ([]*AggTrade, error)
Klines(kr KlinesRequest) ([]*Kline, error)
Ticker24(tr TickerRequest) (*Ticker24, error)
TickerAllPrices() ([]*PriceTicker, error)
TickerAllBooks() ([]*BookTicker, error)
NewOrder(or NewOrderRequest) (*ProcessedOrder, error)
NewOrderTest(or NewOrderRequest) error
QueryOrder(qor QueryOrderRequest) (*ExecutedOrder, error)
CancelOrder(cor CancelOrderRequest) (*CanceledOrder, error)
OpenOrders(oor OpenOrdersRequest) ([]*ExecutedOrder, error)
AllOrders(aor AllOrdersRequest) ([]*ExecutedOrder, error)
Account(ar AccountRequest) (*Account, error)
MyTrades(mtr MyTradesRequest) ([]*Trade, error)
Withdraw(wr WithdrawRequest) (*WithdrawResult, error)
DepositHistory(hr HistoryRequest) ([]*Deposit, error)
WithdrawHistory(hr HistoryRequest) ([]*Withdrawal, error)
StartUserDataStream() (*Stream, error)
KeepAliveUserDataStream(s *Stream) error
CloseUserDataStream(s *Stream) error
DepthWebsocket(dwr DepthWebsocketRequest) (chan *DepthEvent, chan struct{}, error)
KlineWebsocket(kwr KlineWebsocketRequest) (chan *KlineEvent, chan struct{}, error)
TradeWebsocket(twr TradeWebsocketRequest) (chan *AggTradeEvent, chan struct{}, error)
UserDataWebsocket(udwr UserDataWebsocketRequest) (chan *AccountEvent, chan struct{}, error)
}
type apiService struct {
URL string
APIKey string
Signer Signer
Logger log.Logger
Ctx context.Context
}
// NewAPIService creates instance of Service.
//
// If logger or ctx are not provided, NopLogger and Background context are used as default.
// You can use context for one-time request cancel (e.g. when shutting down the app).
func NewAPIService(url, apiKey string, signer Signer, logger log.Logger, ctx context.Context) Service {
if logger == nil {
logger = log.NewNopLogger()
}
if ctx == nil {
ctx = context.Background()
}
return &apiService{
URL: url,
APIKey: apiKey,
Signer: signer,
Logger: logger,
Ctx: ctx,
}
}
func (as *apiService) request(method string, endpoint string, params map[string]string,
apiKey bool, sign bool) (*http.Response, error) {
transport := &http.Transport{}
client := &http.Client{
Transport: transport,
}
url := fmt.Sprintf("%s/%s", as.URL, endpoint)
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to create request")
}
req.WithContext(as.Ctx)
q := req.URL.Query()
for key, val := range params {
q.Add(key, val)
}
if apiKey {
req.Header.Add("X-MBX-APIKEY", as.APIKey)
}
if sign {
level.Debug(as.Logger).Log("queryString", q.Encode())
q.Add("signature", as.Signer.Sign([]byte(q.Encode())))
level.Debug(as.Logger).Log("signature", as.Signer.Sign([]byte(q.Encode())))
}
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}