mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2025-02-14 21:10:32 +01:00
Add optional headers to connection
This commit is contained in:
parent
862ad6e4e6
commit
53b9513206
29
API.md
29
API.md
@ -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)
|
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**
|
**Parameters**
|
||||||
|
|
||||||
- `blockId`
|
- `blockId`
|
||||||
- `API_PATH`
|
|
||||||
|
|
||||||
## getTransaction
|
### getTransaction
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `txId`
|
- `txId`
|
||||||
- `API_PATH`
|
|
||||||
|
|
||||||
## getStatus
|
### getStatus
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `tx_id`
|
- `tx_id`
|
||||||
- `API_PATH`
|
|
||||||
|
|
||||||
## listBlocks
|
### listBlocks
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||||
- `$0.tx_id`
|
- `$0.tx_id`
|
||||||
- `$0.status`
|
- `$0.status`
|
||||||
- `API_PATH`
|
|
||||||
- `tx_id`
|
- `tx_id`
|
||||||
- `status`
|
- `status`
|
||||||
|
|
||||||
## listOutputs
|
### listOutputs
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||||
- `$0.public_key`
|
- `$0.public_key`
|
||||||
- `$0.unspent`
|
- `$0.unspent`
|
||||||
- `API_PATH`
|
|
||||||
- `onlyJsonResponse`
|
- `onlyJsonResponse`
|
||||||
- `public_key`
|
- `public_key`
|
||||||
- `unspent`
|
- `unspent`
|
||||||
|
|
||||||
## listTransactions
|
### listTransactions
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||||
- `$0.asset_id`
|
- `$0.asset_id`
|
||||||
- `$0.operation`
|
- `$0.operation`
|
||||||
- `API_PATH`
|
|
||||||
- `asset_id`
|
- `asset_id`
|
||||||
- `operation`
|
- `operation`
|
||||||
|
|
||||||
## listVotes
|
### listVotes
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `block_id`
|
- `block_id`
|
||||||
- `API_PATH`
|
|
||||||
|
|
||||||
## pollStatusAndFetchTransaction
|
### pollStatusAndFetchTransaction
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `tx_id`
|
- `tx_id`
|
||||||
- `API_PATH`
|
|
||||||
|
|
||||||
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
|
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
|
||||||
|
|
||||||
## postTransaction
|
### postTransaction
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
|
|
||||||
- `transaction`
|
- `transaction`
|
||||||
- `API_PATH`
|
|
||||||
|
@ -1,154 +1,158 @@
|
|||||||
import request from '../request';
|
import request from '../request';
|
||||||
|
|
||||||
|
|
||||||
export default function getApiUrls(API_PATH) {
|
class Connection {
|
||||||
return {
|
constructor(path, headers) {
|
||||||
'blocks': API_PATH + 'blocks',
|
this.path = path;
|
||||||
'blocks_detail': API_PATH + 'blocks/%(blockId)s',
|
this.headers = headers;
|
||||||
'outputs': API_PATH + 'outputs',
|
}
|
||||||
'statuses': API_PATH + 'statuses',
|
|
||||||
'transactions': API_PATH + 'transactions',
|
|
||||||
'transactions_detail': API_PATH + 'transactions/%(txId)s',
|
|
||||||
'votes': API_PATH + 'votes'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
getApiUrls(endpoints) {
|
||||||
* @public
|
return {
|
||||||
* @param blockId
|
'blocks': this.path + 'blocks',
|
||||||
* @param API_PATH
|
'blocks_detail': this.path + 'blocks/%(blockId)s',
|
||||||
*/
|
'outputs': this.path + 'outputs',
|
||||||
export default function getBlock(blockId, API_PATH) {
|
'statuses': this.path + 'statuses',
|
||||||
return request(getApiUrls(API_PATH)['blocks_detail'], {
|
'transactions': this.path + 'transactions',
|
||||||
urlTemplateSpec: {
|
'transactions_detail': this.path + 'transactions/%(txId)s',
|
||||||
blockId
|
'votes': this.path + 'votes'
|
||||||
}
|
}[endpoints];
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
req(path, options={}) {
|
||||||
* @public
|
// NOTE: `options.headers` could be undefined, but that's OK.
|
||||||
* @param tx_id
|
options.headers = Object.assign({}, options.headers, this.headers)
|
||||||
* @param API_PATH
|
return request(path, options)
|
||||||
*/
|
}
|
||||||
export default function getStatus(tx_id, API_PATH) {
|
|
||||||
return request(getApiUrls(API_PATH)['statuses'], {
|
/**
|
||||||
|
* @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: {
|
query: {
|
||||||
tx_id
|
public_key,
|
||||||
|
unspent
|
||||||
}
|
}
|
||||||
});
|
}, onlyJsonResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
* @param txId
|
* @param asset_id
|
||||||
* @param API_PATH
|
* @param operation
|
||||||
*/
|
*/
|
||||||
export default function getTransaction(txId, API_PATH) {
|
listTransactions({ asset_id, operation }) {
|
||||||
return request(getApiUrls(API_PATH)['transactions_detail'], {
|
return this.req(this.getApiUrls('transactions'), {
|
||||||
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'], {
|
|
||||||
query: {
|
query: {
|
||||||
tx_id,
|
asset_id,
|
||||||
status
|
operation
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
* @param public_key
|
* @param block_id
|
||||||
* @param unspent
|
*/
|
||||||
* @param API_PATH
|
listVotes(block_id) {
|
||||||
* @param onlyJsonResponse
|
return this.req(this.getApiUrls('votes'), {
|
||||||
*/
|
query: {
|
||||||
export default function listOutputs({ public_key, unspent }, API_PATH, onlyJsonResponse=true) {
|
block_id
|
||||||
return request(getApiUrls(API_PATH)['outputs'], {
|
}
|
||||||
query: {
|
});
|
||||||
public_key,
|
}
|
||||||
unspent
|
|
||||||
}
|
|
||||||
}, onlyJsonResponse)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
* @param asset_id
|
* @param tx_id
|
||||||
* @param operation
|
* @return {Promise}
|
||||||
* @param API_PATH
|
*/
|
||||||
*/
|
pollStatusAndFetchTransaction(tx_id) {
|
||||||
export default function listTransactions({ asset_id, operation }, API_PATH) {
|
return new Promise((resolve, reject) => {
|
||||||
return request(getApiUrls(API_PATH)['transactions'], {
|
const timer = setInterval(() => {
|
||||||
query: {
|
this.getStatus(tx_id)
|
||||||
asset_id,
|
.then((res) => {
|
||||||
operation
|
console.log('Fetched transaction status:', res);
|
||||||
}
|
if (res.status === 'valid') {
|
||||||
})
|
clearInterval(timer);
|
||||||
}
|
this.getTransaction(tx_id)
|
||||||
|
.then((res) => {
|
||||||
/**
|
console.log('Fetched transaction:', res);
|
||||||
* @public
|
resolve(res);
|
||||||
* @param block_id
|
});
|
||||||
* @param API_PATH
|
}
|
||||||
*/
|
})
|
||||||
export default function listVotes(block_id, API_PATH) {
|
.catch((err) => {
|
||||||
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') {
|
|
||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
getTransaction(tx_id, API_PATH)
|
reject(err);
|
||||||
.then((res) => {
|
});
|
||||||
console.log('Fetched transaction:', res);
|
}, 500)
|
||||||
resolve(res);
|
})
|
||||||
});
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
clearInterval(timer);
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
}, 500)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
*
|
*
|
||||||
* @param transaction
|
* @param transaction
|
||||||
* @param API_PATH
|
*/
|
||||||
*/
|
postTransaction(transaction) {
|
||||||
export default function postTransaction(transaction, API_PATH) {
|
return this.req(this.getApiUrls('transactions'), {
|
||||||
return request(getApiUrls(API_PATH)['transactions'], {
|
method: 'POST',
|
||||||
method: 'POST',
|
jsonBody: transaction
|
||||||
jsonBody: transaction
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@ const DEFAULT_REQUEST_CONFIG = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Small wrapper around js-utility-belt's request that provides url resolving, default settings, and
|
* Small wrapper around js-utility-belt's request that provides url resolving,
|
||||||
* response handling.
|
* default settings, and response handling.
|
||||||
*/
|
*/
|
||||||
export default function request(url, config = {}, onlyJsonResponse=true) {
|
export default function request(url, config = {}, onlyJsonResponse=true) {
|
||||||
// Load default fetch configuration and remove any falsy query parameters
|
// Load default fetch configuration and remove any falsy query parameters
|
||||||
@ -42,4 +42,3 @@ export default function request(url, config = {}, onlyJsonResponse=true) {
|
|||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user