Skip to content

Commit

Permalink
Convert API to promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Aug 4, 2015
1 parent 0afca56 commit bbd51a0
Show file tree
Hide file tree
Showing 25 changed files with 365 additions and 274 deletions.
19 changes: 14 additions & 5 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -18,6 +18,7 @@
"async": "~0.9.0",
"babel-runtime": "^5.5.4",
"bignumber.js": "^2.0.3",
"es6-promisify": "^2.0.0",
"extend": "~1.2.1",
"https-proxy-agent": "^1.0.0",
"is-my-json-valid": "^2.12.0",
Expand Down
6 changes: 4 additions & 2 deletions src/api/common/index.js
Expand Up @@ -9,8 +9,10 @@ module.exports = {
dropsToXrp: utils.dropsToXrp,
xrpToDrops: utils.xrpToDrops,
toRippledAmount: utils.toRippledAmount,
wrapCatch: utils.wrapCatch,
composeAsync: utils.composeAsync,
wrapCatch: utils.wrapCatch,
convertExceptions: utils.convertExceptions,
convertKeysFromSnakeCaseToCamelCase: utils.convertKeysFromSnakeCaseToCamelCase
convertKeysFromSnakeCaseToCamelCase:
utils.convertKeysFromSnakeCaseToCamelCase,
promisify: utils.promisify
};
2 changes: 1 addition & 1 deletion src/api/common/schemas/settings-transaction.json
Expand Up @@ -10,6 +10,6 @@
"address": {"$ref": "address"},
"sequence": {"$ref": "sequence"}
},
"required": ["type", "id", "address", "sequence", "specification", "outcome"],
"required": ["type", "id", "address", "sequence", "specification"],
"additionalProperties": false
}
10 changes: 8 additions & 2 deletions src/api/common/utils.js
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const BigNumber = require('bignumber.js');
const core = require('../../core');
const errors = require('./errors');
const es6promisify = require('es6-promisify');

type Amount = {currency: string, issuer: string, value: string}

Expand Down Expand Up @@ -85,13 +86,18 @@ function convertKeysFromSnakeCaseToCamelCase(obj: any): any {
return obj;
}

function promisify<T>(asyncFunction: AsyncFunction): Function {
return es6promisify(wrapCatch(asyncFunction));
}

module.exports = {
core,
dropsToXrp,
xrpToDrops,
toRippledAmount,
wrapCatch,
composeAsync,
wrapCatch,
convertExceptions,
convertKeysFromSnakeCaseToCamelCase
convertKeysFromSnakeCaseToCamelCase,
promisify
};
8 changes: 6 additions & 2 deletions src/api/ledger/accountinfo.js
Expand Up @@ -18,7 +18,7 @@ function formatAccountInfo(response) {
});
}

function getAccountInfo(account, options, callback) {
function getAccountInfoAsync(account, options, callback) {
validate.address(account);
validate.getAccountInfoOptions(options);

Expand All @@ -31,4 +31,8 @@ function getAccountInfo(account, options, callback) {
composeAsync(formatAccountInfo, callback));
}

module.exports = utils.wrapCatch(getAccountInfo);
function getAccountInfo(account: string, options={}) {
return utils.promisify(getAccountInfoAsync.bind(this))(account, options);
}

module.exports = getAccountInfo;
16 changes: 13 additions & 3 deletions src/api/ledger/balances.js
Expand Up @@ -24,16 +24,26 @@ function formatBalances(balances) {
balances.trustlines.map(getTrustlineBalanceAmount));
}

function getBalances(account, options, callback) {
function getTrustlinesAsync(account, options, callback) {
getTrustlines.bind(this)(account, options)
.then(data => callback(null, data))
.catch(callback);
}

function getBalancesAsync(account, options, callback) {
validate.address(account);
validate.getBalancesOptions(options);

const ledgerVersion = options.ledgerVersion
|| this.remote.getLedgerSequence();
async.parallel({
xrp: _.partial(utils.getXRPBalance, this.remote, account, ledgerVersion),
trustlines: _.partial(getTrustlines.bind(this), account, options)
trustlines: _.partial(getTrustlinesAsync.bind(this), account, options)
}, composeAsync(formatBalances, callback));
}

module.exports = utils.wrapCatch(getBalances);
function getBalances(account: string, options={}) {
return utils.promisify(getBalancesAsync.bind(this))(account, options);
}

module.exports = getBalances;
9 changes: 7 additions & 2 deletions src/api/ledger/orderbook.js
Expand Up @@ -62,7 +62,7 @@ function formatBidsAndAsks(orderbook, offers) {
return {bids, asks};
}

function getOrderbook(account, orderbook, options, callback) {
function getOrderbookAsync(account, orderbook, options, callback) {
validate.address(account);
validate.orderbook(orderbook);
validate.getOrderbookOptions(options);
Expand All @@ -76,4 +76,9 @@ function getOrderbook(account, orderbook, options, callback) {
callback));
}

module.exports = utils.wrapCatch(getOrderbook);
function getOrderbook(account: string, orderbook: Object, options={}) {
return utils.promisify(getOrderbookAsync.bind(this))(
account, orderbook, options);
}

module.exports = getOrderbook;
8 changes: 6 additions & 2 deletions src/api/ledger/orders.js
Expand Up @@ -20,7 +20,7 @@ function requestAccountOffers(remote, address, ledgerVersion, options,
}), callback));
}

function getOrders(account, options, callback) {
function getOrdersAsync(account, options, callback) {
validate.address(account);
validate.getOrdersOptions(options);

Expand All @@ -33,4 +33,8 @@ function getOrders(account, options, callback) {
(order) => order.properties.sequence), callback));
}

module.exports = utils.wrapCatch(getOrders);
function getOrders(account: string, options={}) {
return utils.promisify(getOrdersAsync.bind(this))(account, options);
}

module.exports = getOrders;
8 changes: 6 additions & 2 deletions src/api/ledger/pathfind.js
Expand Up @@ -103,7 +103,7 @@ function formatResponse(pathfind, paths) {
}
}

function getPaths(pathfind, callback) {
function getPathsAsync(pathfind, callback) {
validate.pathfind(pathfind);

const address = pathfind.source.address;
Expand All @@ -113,4 +113,8 @@ function getPaths(pathfind, callback) {
], composeAsync(_.partial(formatResponse, pathfind), callback));
}

module.exports = utils.wrapCatch(getPaths);
function getPaths(pathfind: Object) {
return utils.promisify(getPathsAsync.bind(this))(pathfind);
}

module.exports = getPaths;
8 changes: 6 additions & 2 deletions src/api/ledger/settings.js
Expand Up @@ -24,7 +24,7 @@ function formatSettings(response) {
return _.assign({}, parsedFlags, parsedFields);
}

function getSettings(account, options, callback) {
function getSettingsAsync(account, options, callback) {
validate.address(account);
validate.getSettingsOptions(options);

Expand All @@ -37,4 +37,8 @@ function getSettings(account, options, callback) {
composeAsync(formatSettings, callback));
}

module.exports = utils.wrapCatch(getSettings);
function getSettings(account: string, options={}) {
return utils.promisify(getSettingsAsync.bind(this))(account, options);
}

module.exports = getSettings;
8 changes: 6 additions & 2 deletions src/api/ledger/transaction.js
Expand Up @@ -36,7 +36,7 @@ function isTransactionInRange(tx, options) {
|| tx.ledger_index <= options.maxLedgerVersion);
}

function getTransaction(identifier, options, callback) {
function getTransactionAsync(identifier, options, callback) {
validate.identifier(identifier);
validate.getTransactionOptions(options);

Expand Down Expand Up @@ -71,4 +71,8 @@ function getTransaction(identifier, options, callback) {
], callbackWrapper);
}

module.exports = utils.wrapCatch(getTransaction);
function getTransaction(identifier: string, options={}) {
return utils.promisify(getTransactionAsync.bind(this))(identifier, options);
}

module.exports = getTransaction;
16 changes: 8 additions & 8 deletions src/api/ledger/transactions.js
Expand Up @@ -98,27 +98,27 @@ function getTransactionsInternal(remote, address, options, callback) {
utils.getRecursive(getter, options.limit, composeAsync(format, callback));
}

function getTransactions(account, options, callback) {
function getTransactionsAsync(account, options, callback) {
validate.address(account);
validate.getTransactionsOptions(options);

const defaults = {maxLedgerVersion: this.remote.getLedgerSequence()};
if (options.start) {
getTransaction.bind(this)(options.start, {}, (error, tx) => {
if (error) {
callback(error);
return;
}
getTransaction.bind(this)(options.start).then(tx => {
const ledgerVersion = tx.outcome.ledgerVersion;
const bound = options.earliestFirst ?
{minLedgerVersion: ledgerVersion} : {maxLedgerVersion: ledgerVersion};
const newOptions = _.assign(defaults, options, {startTx: tx}, bound);
getTransactionsInternal(this.remote, account, newOptions, callback);
});
}).catch(callback);
} else {
const newOptions = _.assign(defaults, options);
getTransactionsInternal(this.remote, account, newOptions, callback);
}
}

module.exports = utils.wrapCatch(getTransactions);
function getTransactions(account: string, options={}) {
return utils.promisify(getTransactionsAsync.bind(this))(account, options);
}

module.exports = getTransactions;
8 changes: 6 additions & 2 deletions src/api/ledger/trustlines.js
Expand Up @@ -29,7 +29,7 @@ function getAccountLines(remote, address, ledgerVersion, options, marker, limit,
});
}

function getTrustlines(account: string, options: {currency: string,
function getTrustlinesAsync(account: string, options: {currency: string,
counterparty: string, limit: number, ledgerVersion: number},
callback: () => void): void {
validate.address(account);
Expand All @@ -42,4 +42,8 @@ function getTrustlines(account: string, options: {currency: string,
utils.getRecursive(getter, options.limit, callback);
}

module.exports = utils.wrapCatch(getTrustlines);
function getTrustlines(account: string, options={}) {
return utils.promisify(getTrustlinesAsync.bind(this))(account, options);
}

module.exports = getTrustlines;
8 changes: 5 additions & 3 deletions src/api/ledger/utils.js
Expand Up @@ -13,7 +13,8 @@ function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max);
}

function getXRPBalance(remote: any, address: string, ledgerVersion?: number, callback: Callback): void {
function getXRPBalance(remote: any, address: string, ledgerVersion?: number,
callback: Callback): void {
remote.requestAccountInfo({account: address, ledger: ledgerVersion},
composeAsync((data) => dropsToXrp(data.account_data.Balance), callback));
}
Expand All @@ -22,7 +23,8 @@ type Getter = (marker: ?string, limit: number, callback: Callback) => void

// If the marker is omitted from a response, you have reached the end
// getter(marker, limit, callback), callback(error, {marker, results})
function getRecursiveRecur(getter: Getter, marker?: string, limit: number, callback: Callback): void {
function getRecursiveRecur(getter: Getter, marker?: string, limit: number,
callback: Callback): void {
getter(marker, limit, (error, data) => {
if (error) {
return callback(error);
Expand Down Expand Up @@ -105,7 +107,7 @@ module.exports = {
renameCounterpartyToIssuerInOrder,
getRecursive,
hasCompleteLedgerRange,
wrapCatch: common.wrapCatch,
promisify: common.promisify,
clamp: clamp,
common: common
};
Expand Down
26 changes: 17 additions & 9 deletions src/api/server/server.js
Expand Up @@ -8,14 +8,6 @@ const common = require('../common');
// If a ledger is not received in this time, consider the connection offline
const CONNECTION_TIMEOUT = 1000 * 30;

function connect(callback: (err: any, data: any) => void): void {
this.remote.connect(callback);
}

function disconnect(callback: (err: any, data: any) => void): void {
this.remote.disconnect(callback);
}

function isUpToDate(remote): boolean {
const server = remote.getServer();
return Boolean(server) && (remote._stand_alone
Expand All @@ -26,7 +18,7 @@ function isConnected(): boolean {
return Boolean(this.remote._ledger_current_index) && isUpToDate(this.remote);
}

function getServerInfo(callback: (err: any, data: any) => void): void {
function getServerInfoAsync(callback: (err: any, data: any) => void): void {
this.remote.requestServerInfo((error, response) => {
if (error) {
const message = _.get(error, ['remote', 'error_message'], error.message);
Expand All @@ -45,6 +37,22 @@ function getLedgerVersion(): number {
return this.remote.getLedgerSequence();
}

function connect() {
return common.promisify(callback => {
this.remote.connect(() => callback(null));
})();
}

function disconnect() {
return common.promisify(callback => {
this.remote.disconnect(() => callback(null));
})();
}

function getServerInfo() {
return common.promisify(getServerInfoAsync.bind(this))();
}

module.exports = {
connect,
disconnect,
Expand Down
9 changes: 7 additions & 2 deletions src/api/transaction/order.js
Expand Up @@ -30,9 +30,14 @@ function createOrderTransaction(account, order) {
return transaction;
}

function prepareOrder(account, order, instructions, callback) {
function prepareOrderAsync(account, order, instructions, callback) {
const transaction = createOrderTransaction(account, order);
utils.createTxJSON(transaction, this.remote, instructions, callback);
}

module.exports = utils.wrapCatch(prepareOrder);
function prepareOrder(account: string, order: Object, instructions={}) {
return utils.promisify(prepareOrderAsync.bind(this))(
account, order, instructions);
}

module.exports = prepareOrder;

0 comments on commit bbd51a0

Please sign in to comment.