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

Add optional headers to connection

This commit is contained in:
Tim Daubenschütz 2017-05-11 17:19:07 +02:00
parent 862ad6e4e6
commit 53b9513206
3 changed files with 156 additions and 160 deletions

29
API.md
View File

@ -148,80 +148,73 @@ Returns **cc.Condition** Ed25519 Condition (that will need to wrapped in an Outp
Returns **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Ed25519 Condition (that will need to wrapped in an Output)
## getBlock
## Connection
### getBlock
**Parameters**
- `blockId`
- `API_PATH`
## getTransaction
### getTransaction
**Parameters**
- `txId`
- `API_PATH`
## getStatus
### getStatus
**Parameters**
- `tx_id`
- `API_PATH`
## listBlocks
### listBlocks
**Parameters**
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `$0.tx_id`
- `$0.status`
- `API_PATH`
- `tx_id`
- `status`
## listOutputs
### listOutputs
**Parameters**
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `$0.public_key`
- `$0.unspent`
- `API_PATH`
- `onlyJsonResponse`
- `public_key`
- `unspent`
## listTransactions
### listTransactions
**Parameters**
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `$0.asset_id`
- `$0.operation`
- `API_PATH`
- `asset_id`
- `operation`
## listVotes
### listVotes
**Parameters**
- `block_id`
- `API_PATH`
## pollStatusAndFetchTransaction
### pollStatusAndFetchTransaction
**Parameters**
- `tx_id`
- `API_PATH`
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
## postTransaction
### postTransaction
**Parameters**
- `transaction`
- `API_PATH`

View File

@ -1,154 +1,158 @@
import request from '../request';
export default function getApiUrls(API_PATH) {
return {
'blocks': API_PATH + 'blocks',
'blocks_detail': API_PATH + 'blocks/%(blockId)s',
'outputs': API_PATH + 'outputs',
'statuses': API_PATH + 'statuses',
'transactions': API_PATH + 'transactions',
'transactions_detail': API_PATH + 'transactions/%(txId)s',
'votes': API_PATH + 'votes'
};
}
class Connection {
constructor(path, headers) {
this.path = path;
this.headers = headers;
}
/**
* @public
* @param blockId
* @param API_PATH
*/
export default function getBlock(blockId, API_PATH) {
return request(getApiUrls(API_PATH)['blocks_detail'], {
urlTemplateSpec: {
blockId
}
});
}
getApiUrls(endpoints) {
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',
'votes': this.path + 'votes'
}[endpoints];
}
/**
* @public
* @param tx_id
* @param API_PATH
*/
export default function getStatus(tx_id, API_PATH) {
return request(getApiUrls(API_PATH)['statuses'], {
req(path, options={}) {
// NOTE: `options.headers` could be undefined, but that's OK.
options.headers = Object.assign({}, options.headers, this.headers)
return request(path, options)
}
/**
* @public
* @param blockId
*/
getBlock(blockId) {
return this.req(this.getApiUrls('blocks_detail'), {
urlTemplateSpec: {
blockId
}
});
}
/**
* @public
* @param tx_id
*/
getStatus(tx_id) {
return this.req(this.getApiUrls('statuses'), {
query: {
tx_id
}
});
}
/**
* @public
* @param txId
*/
getTransaction(txId) {
return this.req(this.getApiUrls('transactions_detail'), {
urlTemplateSpec: {
txId
}
});
}
/**
* @public
* @param tx_id
* @param status
*/
listBlocks({ tx_id, status }) {
return this.req(this.getApiUrls('blocks'), {
query: {
tx_id,
status
}
});
}
/**
* @public
* @param public_key
* @param unspent
* @param onlyJsonResponse
*/
listOutputs({ public_key, unspent }, onlyJsonResponse=true) {
return this.req(this.getApiUrls('outputs'), {
query: {
tx_id
public_key,
unspent
}
});
}
}, onlyJsonResponse)
}
/**
* @public
* @param txId
* @param API_PATH
*/
export default function getTransaction(txId, API_PATH) {
return request(getApiUrls(API_PATH)['transactions_detail'], {
urlTemplateSpec: {
txId
}
});
}
/**
* @public
* @param tx_id
* @param status
* @param API_PATH
*/
export default function listBlocks({tx_id, status}, API_PATH) {
return request(getApiUrls(API_PATH)['blocks'], {
/**
* @public
* @param asset_id
* @param operation
*/
listTransactions({ asset_id, operation }) {
return this.req(this.getApiUrls('transactions'), {
query: {
tx_id,
status
asset_id,
operation
}
});
}
})
}
/**
* @public
* @param public_key
* @param unspent
* @param API_PATH
* @param onlyJsonResponse
*/
export default function listOutputs({ public_key, unspent }, API_PATH, onlyJsonResponse=true) {
return request(getApiUrls(API_PATH)['outputs'], {
query: {
public_key,
unspent
}
}, onlyJsonResponse)
}
/**
* @public
* @param block_id
*/
listVotes(block_id) {
return this.req(this.getApiUrls('votes'), {
query: {
block_id
}
});
}
/**
* @public
* @param asset_id
* @param operation
* @param API_PATH
*/
export default function listTransactions({ asset_id, operation }, API_PATH) {
return request(getApiUrls(API_PATH)['transactions'], {
query: {
asset_id,
operation
}
})
}
/**
* @public
* @param block_id
* @param API_PATH
*/
export default function listVotes(block_id, API_PATH) {
return request(getApiUrls(API_PATH)['votes'], {
query: {
block_id
}
});
}
/**
* @public
* @param tx_id
* @param API_PATH
* @return {Promise}
*/
export default function (tx_id, API_PATH) {
return new Promise((resolve, reject) => {
const timer = setInterval(() => {
getStatus(tx_id, API_PATH)
.then((res) => {
console.log('Fetched transaction status:', res);
if (res.status === 'valid') {
/**
* @public
* @param tx_id
* @return {Promise}
*/
pollStatusAndFetchTransaction(tx_id) {
return new Promise((resolve, reject) => {
const timer = setInterval(() => {
this.getStatus(tx_id)
.then((res) => {
console.log('Fetched transaction status:', res);
if (res.status === 'valid') {
clearInterval(timer);
this.getTransaction(tx_id)
.then((res) => {
console.log('Fetched transaction:', res);
resolve(res);
});
}
})
.catch((err) => {
clearInterval(timer);
getTransaction(tx_id, API_PATH)
.then((res) => {
console.log('Fetched transaction:', res);
resolve(res);
});
}
})
.catch((err) => {
clearInterval(timer);
reject(err);
});
}, 500)
})
}
reject(err);
});
}, 500)
})
}
/**
* @public
*
* @param transaction
* @param API_PATH
*/
export default function postTransaction(transaction, API_PATH) {
return request(getApiUrls(API_PATH)['transactions'], {
method: 'POST',
jsonBody: transaction
})
/**
* @public
*
* @param transaction
*/
postTransaction(transaction) {
return this.req(this.getApiUrls('transactions'), {
method: 'POST',
jsonBody: transaction
})
}
}

View File

@ -10,8 +10,8 @@ const DEFAULT_REQUEST_CONFIG = {
};
/**
* Small wrapper around js-utility-belt's request that provides url resolving, default settings, and
* response handling.
* Small wrapper around js-utility-belt's request that provides url resolving,
* default settings, and response handling.
*/
export default function request(url, config = {}, onlyJsonResponse=true) {
// Load default fetch configuration and remove any falsy query parameters
@ -42,4 +42,3 @@ export default function request(url, config = {}, onlyJsonResponse=true) {
throw err;
});
}