From 74a4f484a1b45b82224e1ee7520407f2d25552c1 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Mon, 11 Sep 2017 11:32:44 +0200 Subject: [PATCH 1/2] Support using custom HTTP client --- v1/account_test.go | 4 ++-- v1/client.go | 28 +++++++++++++++++++++------- v1/deposit_test.go | 2 +- v1/history_test.go | 6 +++--- v1/lendbook_test.go | 4 ++-- v1/margin_funding_test.go | 2 +- v1/offers_test.go | 2 +- v1/order_book_test.go | 2 +- v1/orders_test.go | 6 +++--- v1/pairs_test.go | 4 ++-- v1/positions_test.go | 7 +++---- v1/stats_test.go | 2 +- v1/ticker_test.go | 2 +- v1/trades_test.go | 2 +- v1/wallet_test.go | 6 +++--- 15 files changed, 46 insertions(+), 33 deletions(-) diff --git a/v1/account_test.go b/v1/account_test.go index eaad4ee79..0d972c140 100644 --- a/v1/account_test.go +++ b/v1/account_test.go @@ -8,7 +8,7 @@ import ( ) func TestAccountInfo(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "maker_fees":"0.1", "taker_fees":"0.2", @@ -47,7 +47,7 @@ func TestAccountInfo(t *testing.T) { } func TestAccountKeyPermission(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "account":{ "read":true, diff --git a/v1/client.go b/v1/client.go index 375193eba..44a54f2c6 100644 --- a/v1/client.go +++ b/v1/client.go @@ -29,6 +29,9 @@ type Client struct { WebSocketURL string WebSocketTLSSkipVerify bool + // HTTP client + HTTPClient *http.Client + // Auth data APIKey string APISecret string @@ -54,11 +57,18 @@ type Client struct { Wallet *WalletService } -// NewClient creates new Bitfinex.com API client. -func NewClient() *Client { +// NewClientWithHTTP creates new Bitfinex.com API client with the given +// HTTP client. +func NewClientWithHTTP(httpClient *http.Client) *Client { baseURL, _ := url.Parse(BaseURL) - c := &Client{BaseURL: baseURL, WebSocketURL: WebSocketURL} + c := &Client{ + BaseURL: baseURL, + WebSocketURL: WebSocketURL, + HTTPClient: httpClient, + WebSocketTLSSkipVerify: false, + } + c.Pairs = &PairsService{client: c} c.Stats = &StatsService{client: c} c.Account = &AccountService{client: c} @@ -77,11 +87,15 @@ func NewClient() *Client { c.Positions = &PositionsService{client: c} c.Wallet = &WalletService{client: c} c.WebSocket = NewWebSocketService(c) - c.WebSocketTLSSkipVerify = false return c } +// NewClient creates new Bitfinex.com API client. +func NewClient() *Client { + return NewClientWithHTTP(http.DefaultClient) +} + // NewRequest create new API request. Relative url can be provided in refURL. func (c *Client) newRequest(method string, refURL string, params url.Values) (*http.Request, error) { rel, err := url.Parse(refURL) @@ -149,13 +163,13 @@ func (c *Client) Auth(key string, secret string) *Client { return c } -var httpDo = func(req *http.Request) (*http.Response, error) { - return http.DefaultClient.Do(req) +var httpDo = func(client *http.Client, req *http.Request) (*http.Response, error) { + return client.Do(req) } // Do executes API request created by NewRequest method or custom *http.Request. func (c *Client) do(req *http.Request, v interface{}) (*Response, error) { - resp, err := httpDo(req) + resp, err := httpDo(c.HTTPClient, req) if err != nil { return nil, err diff --git a/v1/deposit_test.go b/v1/deposit_test.go index 4cce22557..0206b6b8e 100644 --- a/v1/deposit_test.go +++ b/v1/deposit_test.go @@ -8,7 +8,7 @@ import ( ) func TestDepositNew(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "result":"success", "method":"bitcoin", diff --git a/v1/history_test.go b/v1/history_test.go index 1c734196c..731ba2ad3 100644 --- a/v1/history_test.go +++ b/v1/history_test.go @@ -9,7 +9,7 @@ import ( ) func TestHistoryBalance(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "currency":"USD", "amount":"-246.94", @@ -38,7 +38,7 @@ func TestHistoryBalance(t *testing.T) { } func TestHistoryMovements(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "id":581183, "currency":"BTC", @@ -70,7 +70,7 @@ func TestHistoryMovements(t *testing.T) { } func TestHistoryTrades(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "price":"246.94", "amount":"1.0", diff --git a/v1/lendbook_test.go b/v1/lendbook_test.go index 6f922332a..dd527392d 100644 --- a/v1/lendbook_test.go +++ b/v1/lendbook_test.go @@ -8,7 +8,7 @@ import ( ) func TestLendbookGet(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "bids":[{ "rate":"9.1287", @@ -51,7 +51,7 @@ func TestLendbookGet(t *testing.T) { } func TestLendbookLends(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "rate":"9.8998", "amount_lent":"22528933.77950878", diff --git a/v1/margin_funding_test.go b/v1/margin_funding_test.go index b16ff53bc..e1560dd68 100644 --- a/v1/margin_funding_test.go +++ b/v1/margin_funding_test.go @@ -8,7 +8,7 @@ import ( ) func TestMarginFundingNew(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "id":13800585, "currency":"USD", diff --git a/v1/offers_test.go b/v1/offers_test.go index abdda6aa9..3c90d3ea1 100644 --- a/v1/offers_test.go +++ b/v1/offers_test.go @@ -8,7 +8,7 @@ import ( ) func TestOfferNew(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "id":13800585, "currency":"USD", diff --git a/v1/order_book_test.go b/v1/order_book_test.go index c82ca1675..4d9819663 100644 --- a/v1/order_book_test.go +++ b/v1/order_book_test.go @@ -8,7 +8,7 @@ import ( ) func TestOrderBookGet(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "bids":[{ "rate":"9.1287", diff --git a/v1/orders_test.go b/v1/orders_test.go index 97a1f1387..2b4d91edf 100644 --- a/v1/orders_test.go +++ b/v1/orders_test.go @@ -8,7 +8,7 @@ import ( ) func TestOrdersAll(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := ` [{ "id":448411365, @@ -49,7 +49,7 @@ func TestOrdersAll(t *testing.T) { } func TestCreateMulti(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "order_ids":[{ "id":448383727, @@ -117,7 +117,7 @@ func TestCreateMulti(t *testing.T) { } func TestCancelMulti(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{"result":"Orders cancelled"}` resp := http.Response{ Body: ioutil.NopCloser(bytes.NewBufferString(msg)), diff --git a/v1/pairs_test.go b/v1/pairs_test.go index 468944af4..587eabc94 100644 --- a/v1/pairs_test.go +++ b/v1/pairs_test.go @@ -8,7 +8,7 @@ import ( ) func TestPairsGetAll(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `["btcusd","ltcusd","ltcbtc","ethusd","ethbtc"]` resp := http.Response{ Body: ioutil.NopCloser(bytes.NewBufferString(msg)), @@ -37,7 +37,7 @@ func TestPairsGetAll(t *testing.T) { } func TestPairsAllDetailed(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "pair":"btcusd", "price_precision":5, diff --git a/v1/positions_test.go b/v1/positions_test.go index bfbff9a54..6cdb776af 100644 --- a/v1/positions_test.go +++ b/v1/positions_test.go @@ -9,7 +9,7 @@ import ( ) func TestPositions(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[ { "id":943715, @@ -49,7 +49,7 @@ func TestPositions(t *testing.T) { } func TestPositionsWhenEmpty(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "id":943715, "symbol":"btcusd", @@ -95,8 +95,7 @@ func TestPositionsWhenEmpty(t *testing.T) { } func TestClaimPosition(t *testing.T) { - - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "id":943715, "symbol":"btcusd", diff --git a/v1/stats_test.go b/v1/stats_test.go index 8f329654c..aaa4abf45 100644 --- a/v1/stats_test.go +++ b/v1/stats_test.go @@ -8,7 +8,7 @@ import ( ) func TestStatsAll(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "period":1, "volume":"7967.96766158" diff --git a/v1/ticker_test.go b/v1/ticker_test.go index 40bf76a17..54a3db530 100644 --- a/v1/ticker_test.go +++ b/v1/ticker_test.go @@ -8,7 +8,7 @@ import ( ) func TestTickerGet(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `{ "mid":"244.755", "bid":"244.75", diff --git a/v1/trades_test.go b/v1/trades_test.go index e17e9640c..861defa81 100644 --- a/v1/trades_test.go +++ b/v1/trades_test.go @@ -9,7 +9,7 @@ import ( ) func TestTradesServiceGet(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "timestamp":1444266681, "tid":11988919, diff --git a/v1/wallet_test.go b/v1/wallet_test.go index 0f28c530d..95e1c4654 100644 --- a/v1/wallet_test.go +++ b/v1/wallet_test.go @@ -8,7 +8,7 @@ import ( ) func TestWalletTransfer(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "status":"success", "message":"1.0 USD transfered from Exchange to Deposit" @@ -33,7 +33,7 @@ func TestWalletTransfer(t *testing.T) { } func TestWithdrawCrypto(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "status":"success", "message":"Your withdrawal request has been successfully submitted.", @@ -64,7 +64,7 @@ func TestWithdrawCrypto(t *testing.T) { } func TestWithdrawWire(t *testing.T) { - httpDo = func(req *http.Request) (*http.Response, error) { + httpDo = func(_ *http.Client, req *http.Request) (*http.Response, error) { msg := `[{ "status":"success", "message":"Your withdrawal request has been successfully submitted.", From 18f8729ed93cfc5de36f9fa497fbc4c227e73002 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Mon, 11 Sep 2017 11:33:03 +0200 Subject: [PATCH 2/2] Fix broken test --- v1/positions_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v1/positions_test.go b/v1/positions_test.go index 6cdb776af..a22115dda 100644 --- a/v1/positions_test.go +++ b/v1/positions_test.go @@ -113,7 +113,7 @@ func TestClaimPosition(t *testing.T) { return &resp, nil } - position, err := NewClient().Positions.Claim("943715", "0.5") + position, err := NewClient().Positions.Claim(943715, "0.5") if err != nil { t.Error(err)