1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-22 17:50:09 +01:00
js-bigchaindb-driver/src/connection.js

179 lines
4.1 KiB
JavaScript
Raw Normal View History

import request from './request'
2017-05-11 12:45:19 +02:00
2017-06-22 17:19:31 +02:00
const HEADER_BLACKLIST = ['content-type']
/**
* Base connection
*/
2017-05-11 18:51:30 +02:00
export default class Connection {
2017-06-22 17:19:31 +02:00
constructor(path, headers = {}) {
2017-06-12 16:57:29 +02:00
this.path = path
2017-06-22 17:19:31 +02:00
this.headers = Object.assign({}, headers)
Object.keys(headers).forEach(header => {
if (HEADER_BLACKLIST.includes(header.toLowerCase())) {
throw new Error(`Header ${header} is reserved and cannot be set.`)
}
})
2017-05-11 17:19:07 +02:00
}
2017-05-11 12:45:19 +02:00
2017-06-21 11:17:59 +02:00
getApiUrls(endpoint) {
2017-06-19 14:40:22 +02:00
return this.path + {
'blocks': 'blocks',
2018-03-21 11:30:54 +01:00
'blocksDetail': 'blocks/%(blockHeight)s',
2017-06-19 14:40:22 +02:00
'outputs': 'outputs',
'transactions': 'transactions',
2018-03-21 11:30:54 +01:00
'transactionsSync': 'transactions?mode=sync',
2018-06-12 14:42:42 +02:00
'transactionsAsync': 'transactions?mode=async',
2018-03-21 11:30:54 +01:00
'transactionsCommit': 'transactions?mode=commit',
2017-06-19 14:56:00 +02:00
'transactionsDetail': 'transactions/%(transactionId)s',
'assets': 'assets',
2017-11-21 16:51:31 +01:00
'metadata': 'metadata',
2017-06-19 14:40:22 +02:00
'votes': 'votes'
2017-06-21 11:17:59 +02:00
}[endpoint]
2017-05-11 17:19:07 +02:00
}
2017-05-11 12:45:19 +02:00
2017-06-12 16:57:29 +02:00
_req(path, options = {}) {
2017-05-11 17:19:07 +02:00
// NOTE: `options.headers` could be undefined, but that's OK.
options.headers = Object.assign({}, options.headers, this.headers)
return request(path, options)
}
2017-05-11 12:45:19 +02:00
2017-05-11 17:19:07 +02:00
/**
2018-03-21 11:30:54 +01:00
* @param blockHeight
2017-05-11 17:19:07 +02:00
*/
2018-03-21 11:30:54 +01:00
getBlock(blockHeight) {
2017-06-19 14:56:00 +02:00
return this._req(this.getApiUrls('blocksDetail'), {
2017-06-12 16:57:29 +02:00
urlTemplateSpec: {
2018-03-21 11:30:54 +01:00
blockHeight
2017-06-12 16:57:29 +02:00
}
})
2017-05-11 17:19:07 +02:00
}
2017-05-11 12:45:19 +02:00
2017-05-11 17:19:07 +02:00
/**
2017-06-19 14:40:22 +02:00
* @param transactionId
2017-05-11 17:19:07 +02:00
*/
2017-06-19 14:40:22 +02:00
getTransaction(transactionId) {
2017-06-19 14:56:00 +02:00
return this._req(this.getApiUrls('transactionsDetail'), {
2017-06-12 16:57:29 +02:00
urlTemplateSpec: {
2017-06-19 14:56:00 +02:00
transactionId
2017-06-12 16:57:29 +02:00
}
})
2017-05-11 17:19:07 +02:00
}
2017-05-11 12:45:19 +02:00
2017-05-11 17:19:07 +02:00
/**
2017-06-19 14:40:22 +02:00
* @param transactionId
2017-05-11 17:19:07 +02:00
* @param status
*/
2018-03-21 11:30:54 +01:00
listBlocks(transactionId) {
2017-05-14 20:03:54 +02:00
return this._req(this.getApiUrls('blocks'), {
2017-06-12 16:57:29 +02:00
query: {
2017-06-19 14:40:22 +02:00
transaction_id: transactionId,
2017-06-12 16:57:29 +02:00
}
})
2017-05-11 17:19:07 +02:00
}
2017-05-11 12:45:19 +02:00
2017-05-11 17:19:07 +02:00
/**
2017-06-20 17:46:25 +02:00
* @param publicKey
* @param spent
2017-05-11 17:19:07 +02:00
*/
listOutputs(publicKey, spent) {
2017-06-20 17:46:25 +02:00
const query = {
public_key: publicKey
}
// NOTE: If `spent` is not defined, it must not be included in the
// query parameters.
if (spent !== undefined) {
query.spent = spent.toString()
2017-06-20 17:46:25 +02:00
}
2017-05-14 20:03:54 +02:00
return this._req(this.getApiUrls('outputs'), {
2017-06-20 17:46:25 +02:00
query
})
2017-05-11 17:19:07 +02:00
}
2017-05-11 12:45:19 +02:00
2017-05-11 17:19:07 +02:00
/**
2017-06-21 11:01:28 +02:00
* @param assetId
2017-05-11 17:19:07 +02:00
* @param operation
*/
2017-06-21 11:01:28 +02:00
listTransactions(assetId, operation) {
2017-05-14 20:03:54 +02:00
return this._req(this.getApiUrls('transactions'), {
2017-05-11 17:19:07 +02:00
query: {
2017-06-21 11:01:28 +02:00
asset_id: assetId,
2017-05-11 17:19:07 +02:00
operation
}
})
}
/**
2017-06-19 14:40:22 +02:00
* @param blockId
2017-05-11 17:19:07 +02:00
*/
2017-06-19 14:40:22 +02:00
listVotes(blockId) {
2017-05-14 20:03:54 +02:00
return this._req(this.getApiUrls('votes'), {
2017-06-12 16:57:29 +02:00
query: {
2017-06-19 14:40:22 +02:00
block_id: blockId
2017-06-12 16:57:29 +02:00
}
})
2017-05-11 17:19:07 +02:00
}
/**
2018-03-21 11:30:54 +01:00
* @param transaction
2017-05-11 17:19:07 +02:00
*/
2018-03-21 11:30:54 +01:00
postTransaction(transaction) {
2018-06-19 16:52:23 +02:00
return this.postTransactionCommit(transaction)
2017-05-11 17:19:07 +02:00
}
2017-05-11 12:45:19 +02:00
2017-05-11 17:19:07 +02:00
/**
* @param transaction
*/
2018-03-21 11:30:54 +01:00
postTransactionSync(transaction) {
return this._req(this.getApiUrls('transactionsSync'), {
method: 'POST',
jsonBody: transaction
})
}
2018-06-12 14:42:42 +02:00
/**
* @param transaction
*/
postTransactionAsync(transaction) {
return this._req(this.getApiUrls('transactionsAsync'), {
method: 'POST',
jsonBody: transaction
})
}
2018-03-21 11:30:54 +01:00
/**
* @param transaction
*/
postTransactionCommit(transaction) {
return this._req(this.getApiUrls('transactionsCommit'), {
2017-05-11 17:19:07 +02:00
method: 'POST',
jsonBody: transaction
})
}
2017-05-14 16:35:47 +02:00
/**
* @param search
2017-05-14 16:35:47 +02:00
*/
searchAssets(search) {
return this._req(this.getApiUrls('assets'), {
2017-05-14 16:35:47 +02:00
query: {
search
2017-05-14 16:35:47 +02:00
}
})
}
2017-11-21 16:51:31 +01:00
/**
* @param search
*/
searchMetadata(search) {
return this._req(this.getApiUrls('metadata'), {
query: {
search
}
})
}
2017-05-11 12:45:19 +02:00
}