1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2025-01-01 01:27:54 +01:00

Adjust HTTP API endpoints and test

This commit is contained in:
tim 2017-06-19 14:40:22 +02:00
parent 8cde6df907
commit af49ec1c7a
2 changed files with 113 additions and 21 deletions

View File

@ -9,15 +9,15 @@ export default class Connection {
getApiUrls(endpoints) { getApiUrls(endpoints) {
// TODO: Use camel case // TODO: Use camel case
return { return this.path + {
'blocks': `${this.path}blocks`, 'blocks': 'blocks',
'blocks_detail': `${this.path}blocks/%(blockId)s`, 'blocks_detail': 'blocks/%(blockId)s',
'outputs': `${this.path}outputs`, 'outputs': 'outputs',
'statuses': `${this.path}statuses`, 'statuses': 'statuses',
'transactions': `${this.path}transactions`, 'transactions': 'transactions',
'transactions_detail': `${this.path}transactions/%(txId)s`, 'transactions_detail': 'transactions/%(transactionId)s',
'search_assets': `${this.path}assets`, 'search_assets': 'assets',
'votes': `${this.path}votes` 'votes': 'votes'
}[endpoints] }[endpoints]
} }
@ -41,37 +41,37 @@ export default class Connection {
/** /**
* @public * @public
* @param tx_id * @param transactionId
*/ */
getStatus(tx_id) { // eslint-disable-line camelcase getStatus(transactionId) {
return this._req(this.getApiUrls('statuses'), { return this._req(this.getApiUrls('statuses'), {
query: { query: {
tx_id transaction_id: transactionId
} }
}) })
} }
/** /**
* @public * @public
* @param txId * @param transactionId
*/ */
getTransaction(txId) { getTransaction(transactionId) {
return this._req(this.getApiUrls('transactions_detail'), { return this._req(this.getApiUrls('transactions_detail'), {
urlTemplateSpec: { urlTemplateSpec: {
txId transaction_id: transactionId
} }
}) })
} }
/** /**
* @public * @public
* @param tx_id * @param transactionId
* @param status * @param status
*/ */
listBlocks({ tx_id, status }) { listBlocks({ transactionId, status }) {
return this._req(this.getApiUrls('blocks'), { return this._req(this.getApiUrls('blocks'), {
query: { query: {
tx_id, transaction_id: transactionId,
status status
} }
}) })
@ -83,6 +83,7 @@ export default class Connection {
* @param unspent * @param unspent
* @param onlyJsonResponse * @param onlyJsonResponse
*/ */
// TODO: Use camel case for parameters
listOutputs({ public_key, unspent }, onlyJsonResponse = true) { listOutputs({ public_key, unspent }, onlyJsonResponse = true) {
return this._req(this.getApiUrls('outputs'), { return this._req(this.getApiUrls('outputs'), {
query: { query: {
@ -97,6 +98,7 @@ export default class Connection {
* @param asset_id * @param asset_id
* @param operation * @param operation
*/ */
// TODO: Use camel case for parameters
listTransactions({ asset_id, operation }) { listTransactions({ asset_id, operation }) {
return this._req(this.getApiUrls('transactions'), { return this._req(this.getApiUrls('transactions'), {
query: { query: {
@ -108,12 +110,12 @@ export default class Connection {
/** /**
* @public * @public
* @param block_id * @param blockId
*/ */
listVotes(block_id) { // eslint-disable-line camelcase listVotes(blockId) {
return this._req(this.getApiUrls('votes'), { return this._req(this.getApiUrls('votes'), {
query: { query: {
block_id block_id: blockId
} }
}) })
} }

View File

@ -0,0 +1,90 @@
import test from 'ava'
import sinon from 'sinon'
import { Connection } from '../../src'
const API_PATH = 'http://localhost:9984/api/v1/'
const conn = new Connection(API_PATH)
test('generate API URLS', t => {
const endpoints = {
'blocks': 'blocks',
'blocks_detail': 'blocks/%(blockId)s',
'outputs': 'outputs',
'statuses': 'statuses',
'transactions': 'transactions',
'transactions_detail': 'transactions/%(transactionId)s',
'search_assets': 'assets',
}
Object.keys(endpoints).forEach((endpointName) => {
const url = conn.getApiUrls(endpointName)
const expected = API_PATH + endpoints[endpointName]
t.is(url, expected)
})
})
test('Get status for a transaction', t => {
const expectedPath = 'path'
const transactionId = 'abc'
conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.getStatus(transactionId)
t.truthy(conn._req.calledWith(
expectedPath,
{ query: { transaction_id: transactionId } }
))
})
test('Get transaction for a transaction id', t => {
const expectedPath = 'path'
const transactionId = 'abc'
conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.getTransaction(transactionId)
t.truthy(conn._req.calledWith(
expectedPath,
{ urlTemplateSpec: { transaction_id: transactionId } }
))
})
test('Get list of blocks for a transaction id', t => {
const expectedPath = 'path'
const transactionId = 'abc'
const status = 'status'
conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.listBlocks({ transactionId, status })
t.truthy(conn._req.calledWith(
expectedPath,
{
query: {
transaction_id: transactionId,
status
}
}
))
})
test('Get votes for a block id', t => {
const expectedPath = 'path'
const blockId = 'abc'
conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.listVotes(blockId)
t.truthy(conn._req.calledWith(
expectedPath,
{ query: { block_id: blockId } }
))
})