Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using custom HTTP client #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions v1/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 21 additions & 7 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Client struct {
WebSocketURL string
WebSocketTLSSkipVerify bool

// HTTP client
HTTPClient *http.Client

// Auth data
APIKey string
APISecret string
Expand All @@ -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}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion v1/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions v1/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions v1/lendbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion v1/margin_funding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion v1/offers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion v1/order_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions v1/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
Expand Down
4 changes: 2 additions & 2 deletions v1/pairs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions v1/positions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -114,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)
Expand Down
2 changes: 1 addition & 1 deletion v1/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion v1/ticker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion v1/trades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions v1/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down