1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-12-28 15:47:50 +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) {
// TODO: Use camel case
return {
'blocks': `${this.path}blocks`,
'blocks_detail': `${this.path}blocks/%(blockId)s`,
'outputs': `${this.path}outputs`,
'statuses': `${this.path}statuses`,
'transactions': `${this.path}transactions`,
'transactions_detail': `${this.path}transactions/%(txId)s`,
'search_assets': `${this.path}assets`,
'votes': `${this.path}votes`
return this.path + {
'blocks': 'blocks',
'blocks_detail': 'blocks/%(blockId)s',
'outputs': 'outputs',
'statuses': 'statuses',
'transactions': 'transactions',
'transactions_detail': 'transactions/%(transactionId)s',
'search_assets': 'assets',
'votes': 'votes'
}[endpoints]
}
@ -41,37 +41,37 @@ export default class Connection {
/**
* @public
* @param tx_id
* @param transactionId
*/
getStatus(tx_id) { // eslint-disable-line camelcase
getStatus(transactionId) {
return this._req(this.getApiUrls('statuses'), {
query: {
tx_id
transaction_id: transactionId
}
})
}
/**
* @public
* @param txId
* @param transactionId
*/
getTransaction(txId) {
getTransaction(transactionId) {
return this._req(this.getApiUrls('transactions_detail'), {
urlTemplateSpec: {
txId
transaction_id: transactionId
}
})
}
/**
* @public
* @param tx_id
* @param transactionId
* @param status
*/
listBlocks({ tx_id, status }) {
listBlocks({ transactionId, status }) {
return this._req(this.getApiUrls('blocks'), {
query: {
tx_id,
transaction_id: transactionId,
status
}
})
@ -83,6 +83,7 @@ export default class Connection {
* @param unspent
* @param onlyJsonResponse
*/
// TODO: Use camel case for parameters
listOutputs({ public_key, unspent }, onlyJsonResponse = true) {
return this._req(this.getApiUrls('outputs'), {
query: {
@ -97,6 +98,7 @@ export default class Connection {
* @param asset_id
* @param operation
*/
// TODO: Use camel case for parameters
listTransactions({ asset_id, operation }) {
return this._req(this.getApiUrls('transactions'), {
query: {
@ -108,12 +110,12 @@ export default class Connection {
/**
* @public
* @param block_id
* @param blockId
*/
listVotes(block_id) { // eslint-disable-line camelcase
listVotes(blockId) {
return this._req(this.getApiUrls('votes'), {
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 } }
))
})