1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-22 01:36:56 +01:00

update endpoints

This commit is contained in:
manolodewiner 2018-03-21 11:30:54 +01:00
parent c3e1244e52
commit 2451ed2ae6
4 changed files with 58 additions and 105 deletions

View File

@ -19,10 +19,11 @@ export default class Connection {
getApiUrls(endpoint) { getApiUrls(endpoint) {
return this.path + { return this.path + {
'blocks': 'blocks', 'blocks': 'blocks',
'blocksDetail': 'blocks/%(blockId)s', 'blocksDetail': 'blocks/%(blockHeight)s',
'outputs': 'outputs', 'outputs': 'outputs',
'statuses': 'statuses',
'transactions': 'transactions', 'transactions': 'transactions',
'transactionsSync': 'transactions?mode=sync',
'transactionsCommit': 'transactions?mode=commit',
'transactionsDetail': 'transactions/%(transactionId)s', 'transactionsDetail': 'transactions/%(transactionId)s',
'assets': 'assets', 'assets': 'assets',
'metadata': 'metadata', 'metadata': 'metadata',
@ -38,24 +39,12 @@ export default class Connection {
/** /**
* @public * @public
* @param blockId * @param blockHeight
*/ */
getBlock(blockId) { getBlock(blockHeight) {
return this._req(this.getApiUrls('blocksDetail'), { return this._req(this.getApiUrls('blocksDetail'), {
urlTemplateSpec: { urlTemplateSpec: {
blockId blockHeight
}
})
}
/**
* @public
* @param transactionId
*/
getStatus(transactionId) {
return this._req(this.getApiUrls('statuses'), {
query: {
transaction_id: transactionId
} }
}) })
} }
@ -77,11 +66,10 @@ export default class Connection {
* @param transactionId * @param transactionId
* @param status * @param status
*/ */
listBlocks(transactionId, status) { listBlocks(transactionId) {
return this._req(this.getApiUrls('blocks'), { return this._req(this.getApiUrls('blocks'), {
query: { query: {
transaction_id: transactionId, transaction_id: transactionId,
status
} }
}) })
} }
@ -133,27 +121,12 @@ export default class Connection {
/** /**
* @public * @public
* @param txId * @param transaction
* @return {Promise}
*/ */
pollStatusAndFetchTransaction(txId) { postTransaction(transaction) {
return new Promise((resolve, reject) => { return this._req(this.getApiUrls('transactions'), {
const timer = setInterval(() => { method: 'POST',
this.getStatus(txId) jsonBody: transaction
.then((res) => {
if (res.status === 'valid') {
clearInterval(timer)
this.getTransaction(txId)
.then((res_) => {
resolve(res_)
})
}
})
.catch((err) => {
clearInterval(timer)
reject(err)
})
}, 500)
}) })
} }
@ -161,8 +134,20 @@ export default class Connection {
* @public * @public
* @param transaction * @param transaction
*/ */
postTransaction(transaction) { postTransactionSync(transaction) {
return this._req(this.getApiUrls('transactions'), { return this._req(this.getApiUrls('transactionsSync'), {
method: 'POST',
jsonBody: transaction
})
}
/**
* @public
* @param transaction
*/
postTransactionCommit(transaction) {
return this._req(this.getApiUrls('transactionsCommit'), {
method: 'POST', method: 'POST',
jsonBody: transaction jsonBody: transaction
}) })

View File

@ -38,7 +38,7 @@ export default class Transaction {
'inputs': [], 'inputs': [],
'metadata': null, 'metadata': null,
'asset': null, 'asset': null,
'version': '1.0', 'version': '2.0',
} }
return txTemplate return txTemplate
} }

View File

@ -24,9 +24,8 @@ test('Payload thrown at incorrect API_PATH', t => {
test('Generate API URLS', t => { test('Generate API URLS', t => {
const endpoints = { const endpoints = {
'blocks': 'blocks', 'blocks': 'blocks',
'blocksDetail': 'blocks/%(blockId)s', 'blocksDetail': 'blocks/%(blockHeight)s',
'outputs': 'outputs', 'outputs': 'outputs',
'statuses': 'statuses',
'transactions': 'transactions', 'transactions': 'transactions',
'transactionsDetail': 'transactions/%(transactionId)s', 'transactionsDetail': 'transactions/%(transactionId)s',
'assets': 'assets', 'assets': 'assets',
@ -59,30 +58,15 @@ test('Request with custom headers', t => {
test('Get block for a block id', t => { test('Get block for a block id', t => {
const expectedPath = 'path' const expectedPath = 'path'
const blockId = 'abc' const blockHeight = 'abc'
conn._req = sinon.spy() conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath) conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.getBlock(blockId) conn.getBlock(blockHeight)
t.truthy(conn._req.calledWith( t.truthy(conn._req.calledWith(
expectedPath, expectedPath,
{ urlTemplateSpec: { blockId } } { urlTemplateSpec: { blockHeight } }
))
})
test('Get status for a transaction id', 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 } }
)) ))
}) })
@ -105,18 +89,16 @@ test('Get transaction for a transaction id', t => {
test('Get list of blocks for a transaction id', t => { test('Get list of blocks for a transaction id', t => {
const expectedPath = 'path' const expectedPath = 'path'
const transactionId = 'abc' const transactionId = 'abc'
const status = 'status'
conn._req = sinon.spy() conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath) conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.listBlocks(transactionId, status) conn.listBlocks(transactionId)
t.truthy(conn._req.calledWith( t.truthy(conn._req.calledWith(
expectedPath, expectedPath,
{ {
query: { query: {
transaction_id: transactionId, transaction_id: transactionId,
status
} }
} }
)) ))

View File

@ -36,8 +36,7 @@ test('Valid CREATE transaction', t => {
) )
const txSigned = Transaction.signTransaction(tx, alice.privateKey) const txSigned = Transaction.signTransaction(tx, alice.privateKey)
return conn.postTransaction(txSigned) return conn.postTransactionSync(txSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(resTx => t.truthy(resTx)) .then(resTx => t.truthy(resTx))
}) })
@ -55,8 +54,7 @@ test('Valid TRANSFER transaction with single Ed25519 input', t => {
alice.privateKey alice.privateKey
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => { .then(() => {
const transferTx = Transaction.makeTransferTransaction( const transferTx = Transaction.makeTransferTransaction(
[{ tx: createTxSigned, output_index: 0 }], [{ tx: createTxSigned, output_index: 0 }],
@ -67,8 +65,7 @@ test('Valid TRANSFER transaction with single Ed25519 input', t => {
transferTx, transferTx,
alice.privateKey alice.privateKey
) )
return conn.postTransaction(transferTxSigned) return conn.postTransactionSync(transferTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(resTx => t.truthy(resTx)) .then(resTx => t.truthy(resTx))
}) })
}) })
@ -87,8 +84,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => {
alice.privateKey alice.privateKey
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ 'id': txId }) => conn.pollStatusAndFetchTransaction(txId))
.then(() => { .then(() => {
const transferTx = Transaction.makeTransferTransaction( const transferTx = Transaction.makeTransferTransaction(
[{ tx: createTxSigned, output_index: 0 }, { tx: createTxSigned, output_index: 1 }], [{ tx: createTxSigned, output_index: 0 }, { tx: createTxSigned, output_index: 1 }],
@ -100,8 +96,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => {
alice.privateKey, alice.privateKey,
bob.privateKey bob.privateKey
) )
return conn.postTransaction(transferTxSigned) return conn.postTransactionSync(transferTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(resTx => t.truthy(resTx)) .then(resTx => t.truthy(resTx))
}) })
}) })
@ -129,8 +124,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs from different tra
alice.privateKey alice.privateKey
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ 'id': txId }) => conn.pollStatusAndFetchTransaction(txId))
.then(() => { .then(() => {
const transferTx1 = Transaction.makeTransferTransaction( const transferTx1 = Transaction.makeTransferTransaction(
[{ tx: createTxSigned, output_index: 0 }], [{ tx: createTxSigned, output_index: 0 }],
@ -151,10 +145,8 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs from different tra
bob.privateKey bob.privateKey
) )
return conn.postTransaction(transferTxSigned1) return conn.postTransactionSync(transferTxSigned1)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id)) .then(conn.postTransactionSync(transferTxSigned2))
.then(conn.postTransaction(transferTxSigned2))
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => { .then(() => {
const transferTxMultipleInputs = Transaction.makeTransferTransaction( const transferTxMultipleInputs = Transaction.makeTransferTransaction(
[{ tx: transferTxSigned1, output_index: 0 }, [{ tx: transferTxSigned1, output_index: 0 },
@ -167,8 +159,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs from different tra
carol.privateKey, carol.privateKey,
trent.privateKey trent.privateKey
) )
return conn.postTransaction(transferTxSignedMultipleInputs) return conn.postTransactionSync(transferTxSignedMultipleInputs)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(resTx => t.truthy(resTx)) .then(resTx => t.truthy(resTx))
}) })
}) })
@ -208,10 +199,8 @@ test('Search for spent and unspent outputs of a given public key', t => {
carol.privateKey, carol.privateKey,
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id)) .then(() => conn.postTransactionSync(transferTxSigned))
.then(() => conn.postTransaction(transferTxSigned))
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => conn.listOutputs(carol.publicKey)) .then(() => conn.listOutputs(carol.publicKey))
// now listOutputs should return us outputs 0 and 1 (unfiltered) // now listOutputs should return us outputs 0 and 1 (unfiltered)
.then(outputs => t.truthy(outputs.length === 2)) .then(outputs => t.truthy(outputs.length === 2))
@ -250,10 +239,8 @@ test('Search for unspent outputs for a given public key', t => {
carol.privateKey, carol.privateKey,
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id)) .then(() => conn.postTransactionSync(transferTxSigned))
.then(() => conn.postTransaction(transferTxSigned))
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
// now listOutputs should return us outputs 0 and 2 (1 is spent) // now listOutputs should return us outputs 0 and 2 (1 is spent)
.then(() => conn.listOutputs(carol.publicKey, 'false')) .then(() => conn.listOutputs(carol.publicKey, 'false'))
.then(outputs => t.truthy(outputs.length === 2)) .then(outputs => t.truthy(outputs.length === 2))
@ -292,10 +279,8 @@ test('Search for spent outputs for a given public key', t => {
carol.privateKey, carol.privateKey,
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id)) .then(() => conn.postTransactionSync(transferTxSigned))
.then(() => conn.postTransaction(transferTxSigned))
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
// now listOutputs should only return us output 1 (0 and 2 are unspent) // now listOutputs should only return us output 1 (0 and 2 are unspent)
.then(() => conn.listOutputs(carol.publicKey, true)) .then(() => conn.listOutputs(carol.publicKey, true))
.then(outputs => t.truthy(outputs.length === 1)) .then(outputs => t.truthy(outputs.length === 1))
@ -316,10 +301,10 @@ test('Search for an asset', t => {
alice.privateKey alice.privateKey
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => conn.searchAssets(createTxSigned.asset.data.message)) .then(() => conn.searchAssets(createTxSigned.asset.data.message))
.then(assets => t.truthy( .then(assets => t.truthy(
console.log('llllllllllllllllllll', createTxSigned.asset.data.message, assets.pop()),
assets.pop(), assets.pop(),
createTxSigned.asset.data.message createTxSigned.asset.data.message
)) ))
@ -340,8 +325,7 @@ test('Search for metadata', t => {
alice.privateKey alice.privateKey
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => conn.searchMetadata(createTxSigned.metadata.message)) .then(() => conn.searchMetadata(createTxSigned.metadata.message))
.then(assets => t.truthy( .then(assets => t.truthy(
assets.pop(), assets.pop(),
@ -349,6 +333,7 @@ test('Search for metadata', t => {
)) ))
}) })
test('Search blocks containing a transaction', t => { test('Search blocks containing a transaction', t => {
const conn = new Connection(API_PATH) const conn = new Connection(API_PATH)
@ -363,9 +348,8 @@ test('Search blocks containing a transaction', t => {
alice.privateKey alice.privateKey
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id)) .then(({ id }) => conn.listBlocks(id))
.then(({ id }) => conn.listBlocks(id, 'VALID'))
.then(blocks => conn.getBlock(blocks.pop())) .then(blocks => conn.getBlock(blocks.pop()))
.then(({ block: { transactions } }) => transactions.filter(({ id }) => id === createTxSigned.id)) .then(({ block: { transactions } }) => transactions.filter(({ id }) => id === createTxSigned.id))
.then(transactions => t.truthy(transactions.length === 1)) .then(transactions => t.truthy(transactions.length === 1))
@ -386,10 +370,12 @@ test('Search transaction containing an asset', t => {
alice.privateKey alice.privateKey
) )
return conn.postTransaction(createTxSigned) return conn.postTransactionSync(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id, 'CREATE'))
.then(({ id }) => conn.listTransactions(id)) .then(({ id }) => conn.listTransactions(id))
.then(transactions => t.truthy(transactions.length === 1)) .then(transactions => {
console.log('cccccccccccc', transactions, createTxSigned.id)
t.truthy(transactions.length === 1)
})
}) })