api and backwards compat work

This commit is contained in:
Josh Harvey 2014-07-04 16:29:54 -04:00
parent ae1a0e80a2
commit 3601a6ec12
3 changed files with 79 additions and 30 deletions

View file

@ -3,6 +3,7 @@
var _trader;
var _lamassuConfig;
var logger = require('./logger');
var ApiResponse = require('./api_response');
// Make sure these are higher than polling interval
// or there will be a lot of errors
@ -11,45 +12,43 @@ var STALE_BALANCE = 180000;
var API_VERSION = 1;
Error.prototype.toJSON = function () {
var self = this;
var ret = {};
Object.getOwnPropertyNames(self).forEach(function (key) {
ret[key] = self[key];
function prepareApi(req, res) {
return ApiResponse.factory({
response: res,
targetVersion: req.body.version || 0,
version: API_VERSION
});
return ret;
};
}
var poll = function(req, res) {
var rateRec = _trader.rate();
var balanceRec = _trader.balance;
var fingerprint = req.connection.getPeerCertificate().fingerprint;
var api = prepareApi(req, res);
logger.debug('poll request from: %s', fingerprint);
// `rateRec` and `balanceRec` are both objects, so there's no danger
// of misinterpreting rate or balance === 0 as 'Server initializing'.
if (!rateRec || !balanceRec) {
return res.json({err: 'Server initializing'});
return api.respond('Server initializing');
}
var now = Date.now();
if (now - rateRec.timestamp > STALE_TICKER) {
return res.json({err: 'Stale ticker'});
return api.respond('Stale ticker');
}
if (now - balanceRec.timestamp > STALE_BALANCE) {
return res.json({err: 'Stale balance'});
return api.respond('Stale balance');
}
var rate = rateRec.rate;
if (rate === null) return res.json({err: 'No rate available'});
if (rate === null) return api.respond('No rate available');
var fiatBalance = _trader.fiatBalance(fingerprint);
if (fiatBalance === null) return res.json({err: 'No balance available'});
if (fiatBalance === null) return api.respond('No balance available');
res.json({
err: null,
version: API_VERSION,
api.respond(null, {
rate: rate * _trader.config.exchanges.settings.commission,
fiat: fiatBalance,
locale: _trader.config.brain.locale,
@ -59,28 +58,36 @@ var poll = function(req, res) {
var trade = function (req, res) {
var fingerprint = req.connection.getPeerCertificate().fingerprint;
var api = prepareApi(req, res);
_trader.trade(req.body, fingerprint);
res.json({err: null});
api.respond();
};
var event = function (req, res) {
var deviceEvent = function deviceEvent(req, res) {
var fingerprint = req.connection.getPeerCertificate().fingerprint;
var api = prepareApi(req, res);
_trader.event(req.body, fingerprint);
res.json({err: null});
api.respond();
};
var idVerify = function idVerify(req, res) {
var fingerprint = req.connection.getPeerCertificate().fingerprint;
var api = prepareApi(req, res);
_idVerifier.idVerify(req.body, fingerprint, function (err, idResult) {
api.respond(err, idResult);
});
};
var send = function(req, res) {
var fingerprint = req.connection.getPeerCertificate().fingerprint;
var api = prepareApi(req, res);
_trader.sendBitcoins(fingerprint, req.body, function(err, txHash) {
res.json({
err: err && err.message,
txHash: txHash,
errType: err && err.name
});
api.respond(err, {txHash: txHash});
});
};
var pair = function(req, res) {
var api = prepareApi(req, res);
var token = req.body.token;
var name = req.body.name;
@ -89,11 +96,8 @@ var pair = function(req, res) {
req.connection.getPeerCertificate().fingerprint,
name,
function(err) {
if (err) {
return res.json(500, { err: err.message });
}
res.json(200);
if (err) return api.respond(err, null, 500);
api.respond();
}
);
};
@ -105,7 +109,8 @@ exports.init = function(app, config, trader, authMiddleware) {
app.get('/poll', authMiddleware, poll);
app.post('/send', authMiddleware, send);
app.post('/trade', authMiddleware, trade);
app.post('/event', authMiddleware, event);
app.post('/event', authMiddleware, deviceEvent);
app.post('/id_verify', authMiddleware, idVerify);
app.post('/pair', pair);
return app;