Skip to content

Commit

Permalink
(refactor) rename rest -> rest1, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
f3rno committed Sep 7, 2018
1 parent 10887df commit 1a424bc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 152 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = {
RESTv1: require('./lib/rest'),
RESTv1: require('./lib/rest1'),
RESTv2: require('./lib/rest2')
}
4 changes: 2 additions & 2 deletions lib/rest.js → lib/rest1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

const request = require('request')
const { genAuthSig, nonce } = require('bfx-api-node-util')
const { Config } = require('bfx-api-node-core')
const API_URL = 'https://api.bitfinex.com'

/**
* Communicates with v1 of the Bitfinex HTTP API
*/
class RESTv1 {
constructor (opts = {}) {
this._url = opts.url || Config.REST_URL
this._url = opts.url || API_URL
this._apiKey = opts.apiKey || ''
this._apiSecret = opts.apiSecret || ''
this._agent = opts.agent
Expand Down
82 changes: 59 additions & 23 deletions lib/rest2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'

const rp = require('request-promise')
const _isEmpty = require('lodash/isEmpty')
const { URLSearchParams } = require('url')

const RESTv1 = require('./rest')
const { genAuthSig, nonce, isClass } = require('bfx-api-node-util')
const {
FundingCredit,
Expand All @@ -21,9 +20,12 @@ const {
Alert,
Candle,
Movement,
LedgerEntry
LedgerEntry,
UserInfo
} = require('bfx-api-node-models')

const RESTv1 = require('./rest1')

const BASE_TIMEOUT = 15000
const API_URL = 'https://api.bitfinex.com'

Expand All @@ -37,20 +39,23 @@ class RESTv2 {
* @param {Object} opts
* @param {string} opts.apiKey
* @param {string} opts.apiSecret
* @param {string} opts.authToken - optional auth option
* @param {string} opts.url - endpoint URL
* @param {boolean} opts.transform - default false
* @param {Object} opts.agent - optional node agent for connection (proxy)
*/
constructor (opts = {
apiKey: '',
apiSecret: '',
authToken: '',
url: API_URL,
transform: false,
agent: null
}) {
this._url = opts.url || API_URL
this._apiKey = opts.apiKey || ''
this._apiSecret = opts.apiSecret || ''
this._authToken = opts.authToken || ''
this._transform = !!opts.transform
this._agent = opts.agent

Expand All @@ -66,22 +71,27 @@ class RESTv2 {
* @private
*/
_makeAuthRequest (path, payload = {}, cb = () => {}, transformer) {
if (!this._apiKey || !this._apiSecret) {
if ((!this._apiKey || !this._apiSecret) && !this._authToken) {
return cb(new Error('missing api key or secret'))
}

const url = `${this._url}/v2${path}`
const n = nonce()
const sigPayload = `/api/v2${path}${n}${JSON.stringify(payload)}`
const { sig } = genAuthSig(this._apiSecret, sigPayload)
const keys = () => {
const sigPayload = `/api/v2${path}${n}${JSON.stringify(payload)}`
const { sig } = genAuthSig(this._apiSecret, sigPayload)
return { 'bfx-apikey': this._apiKey, 'bfx-signature': sig }
}
const auth = (this._authToken)
? { 'bfx-token': this._authToken }
: keys()

return rp({
url,
method: 'POST',
headers: {
'bfx-nonce': n,
'bfx-apikey': this._apiKey,
'bfx-signature': sig
...auth
},
agent: this._agent,
body: payload,
Expand Down Expand Up @@ -382,7 +392,7 @@ class RESTv2 {
}

/**
* @param {string} symbol
* @param {string?} symbol - optional, omit/leave empty for all
* @param {number} start
* @param {number} end
* @param {number} limit
Expand All @@ -391,8 +401,12 @@ class RESTv2 {
* @return {Promise} p
* @see https://docs.bitfinex.com/v2/reference#rest-auth-trades-hist
*/
accountTrades (symbol = 'tBTCUSD', start = null, end = null, limit = null, sort = null, cb) {
return this._makeAuthRequest(`/auth/r/trades/${symbol}/hist`, {
accountTrades (symbol, start = null, end = null, limit = null, sort = null, cb) {
const url = !_isEmpty(symbol)
? `/auth/r/trades/${symbol}/hist`
: '/auth/r/trades/hist'

return this._makeAuthRequest(url, {
start, end, limit, sort
}, cb, Trade)
}
Expand All @@ -406,6 +420,15 @@ class RESTv2 {
return this._makeAuthRequest('/auth/r/wallets', {}, cb, Wallet)
}

/**
* @param {Method} cb
* @return {Promise} p
* @see https://docs.bitfinex.com/v2/reference#rest-auth-wallets
*/
userInfo (cb) {
return this._makeAuthRequest('/auth/r/info/user', {}, cb, UserInfo)
}

/**
* @param {Method} cb
* @return {Promise} p
Expand Down Expand Up @@ -450,16 +473,20 @@ class RESTv2 {
}

/**
* @param {string} symbol
* @param {string?} symbol - optional, omit/leave empty for all
* @param {number} start
* @param {number} end
* @param {number} limit
* @param {Method} cb
* @return {Promise} p
* @see https://docs.bitfinex.com/v2/reference#orders-history
*/
orderHistory (symbol = 'tBTCUSD', start = null, end = null, limit = null, cb) {
return this._makeAuthRequest(`/auth/r/orders/${symbol}/hist`, {
orderHistory (symbol, start = null, end = null, limit = null, cb) {
const url = !_isEmpty(symbol)
? `/auth/r/orders/${symbol}/hist`
: `/auth/r/orders/hist`

return this._makeAuthRequest(url, {
start, end, limit
}, cb, Order)
}
Expand Down Expand Up @@ -500,16 +527,19 @@ class RESTv2 {
}

/**
* @param {string} symbol
* @param {string} symbol - optional, omit/leave empty for all
* @param {number} start
* @param {number} end
* @param {number} limit
* @param {Method} cb
* @return {Promise} p
* @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-offers-hist
*/
fundingOfferHistory (symbol = 'tBTCUSD', start = null, end = null, limit = null, cb) {
return this._makeAuthRequest(`/auth/r/funding/offers/${symbol}/hist`, {
fundingOfferHistory (symbol, start = null, end = null, limit = null, cb) {
const url = !_isEmpty(symbol)
? `/auth/r/funding/offers/${symbol}/hist`
: '/auth/r/funding/offers/hist'
return this._makeAuthRequest(url, {
start, end, limit
}, cb, FundingOffer)
}
Expand All @@ -525,16 +555,19 @@ class RESTv2 {
}

/**
* @param {string} symbol
* @param {string} symbol - optional, omit/leave empty for all
* @param {number} start
* @param {number} end
* @param {number} limit
* @param {Method} cb
* @return {Promise} p
* @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-loans-hist
*/
fundingLoanHistory (symbol = 'tBTCUSD', start = null, end = null, limit = null, cb) {
return this._makeAuthRequest(`/auth/r/funding/loans/${symbol}/hist`, {
fundingLoanHistory (symbol, start = null, end = null, limit = null, cb) {
const url = !_isEmpty(symbol)
? `/auth/r/funding/loans/${symbol}/hist`
: '/auth/r/funding/loans/hist'
return this._makeAuthRequest(url, {
start, end, limit
}, cb, FundingLoan)
}
Expand All @@ -550,16 +583,19 @@ class RESTv2 {
}

/**
* @param {string} symbol
* @param {string} symbol - optional, omit/leave empty for all
* @param {number} start
* @param {number} end
* @param {number} limit
* @param {Method} cb
* @return {Promise} p
* @see https://docs.bitfinex.com/v2/reference#rest-auth-funding-credits-hist
*/
fundingCreditHistory (symbol = 'tBTCUSD', start = null, end = null, limit = null, cb) {
return this._makeAuthRequest(`/auth/r/funding/credits/${symbol}/hist`, {
fundingCreditHistory (symbol, start = null, end = null, limit = null, cb) {
const url = !_isEmpty(symbol)
? `/auth/r/funding/credits/${symbol}/hist`
: '/auth/r/funding/credits/hist'
return this._makeAuthRequest(url, {
start, end, limit
}, cb, FundingCredit)
}
Expand Down
68 changes: 0 additions & 68 deletions test/helpers/data.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/lib/rest-1-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PORT = 1337

const assert = require('assert')
const http = require('http')
const RESTv1 = require('../../lib/rest')
const RESTv1 = require('../../lib/rest1')

describe('rest integration test', () => {
it('should get the fundingbook asks, zero bids, 100 asks', (done) => {
Expand Down
56 changes: 0 additions & 56 deletions test/lib/rest-2-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const assert = require('assert')
const RESTv2 = require('../../lib/rest2')
const { MockRESTv2Server } = require('bfx-api-mock-srv')
const { getTradingTicker, getFundingTicker, auditTicker } = require('../helpers/data')

const getTestREST2 = (args = {}) => {
return new RESTv2({
Expand Down Expand Up @@ -143,61 +142,6 @@ describe('RESTv2 integration (mock server) tests', () => {
})
})

it('correctly parses a mixed tickers response', (done) => {
const srv = new MockRESTv2Server({ listen: true })
const r = getTestREST2({ transform: true })

srv.setResponse('tickers', [
getTradingTicker('tETHUSD'),
getFundingTicker('fUSD')
])

r.tickers(['tETHUSD', 'fUSD'], (err, data = []) => {
if (err) {
return srv.close().then(() => done(err)).catch(done)
}

assert.equal(data.length, 2)

auditTicker(data[0], 'tETHUSD')
auditTicker(data[1], 'fUSD')

srv.close().then(done).catch(done)
})
})

it('correctly parses single trading ticker response', (done) => {
const srv = new MockRESTv2Server({ listen: true })
const r = getTestREST2({ transform: true })

srv.setResponse('ticker.tETHUSD', getTradingTicker())

r.ticker('tETHUSD', (err, ticker = {}) => {
if (err) {
return srv.close().then(() => done(err)).catch(done)
}

auditTicker(ticker, 'tETHUSD')
srv.close().then(done).catch(done)
})
})

it('correctly parses single funding ticker response', (done) => {
const srv = new MockRESTv2Server({ listen: true })
const r = getTestREST2({ transform: true })

srv.setResponse('ticker.fUSD', getFundingTicker())

r.ticker('fUSD', (err, ticker = {}) => {
if (err) {
return srv.close().then(() => done(err)).catch(done)
}

auditTicker(ticker, 'fUSD')
srv.close().then(done).catch(done)
})
})

it('correctly parses funding offer response', (done) => {
const srv = new MockRESTv2Server({ listen: true })
const r = getTestREST2({ transform: true })
Expand Down
2 changes: 1 addition & 1 deletion test/lib/rest1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const assert = require('assert')
const { expect } = require('chai')
const DNS = require('dns')
const RESTv1 = require('../../lib/rest')
const RESTv1 = require('../../lib/rest1')
const _ = require('lodash')

describe('REST v1', () => {
Expand Down

0 comments on commit 1a424bc

Please sign in to comment.