UNPKG

1.53 kBJavaScriptView Raw
1"use strict";
2var lp = require('./system').lp;
3var constant = require('./constant');
4var HttpApiError = require('@you21979/http-api-error');
5
6var makeapi = function(api){
7 return constant.OPT_APIV1_URL + '/' + api;
8}
9var createEndPoint = function(apiv1, pair){
10 return apiv1 + '/' + pair.toLowerCase();
11}
12
13var createGetOption = function(url){
14 return {
15 url: url,
16 method: 'GET',
17 forever: constant.OPT_KEEPALIVE,
18 timeout: Math.floor(constant.OPT_TIMEOUT_SEC * 1000),
19 transform2xxOnly : true,
20 transform: function(body){
21 return JSON.parse(body)
22 },
23 };
24}
25
26var query = exports.query = function(method, pair){
27 return lp.req(createGetOption(createEndPoint(makeapi(method), pair))).
28 then(function(result){
29 if('error' in result){
30 var error_code = (result.error).toUpperCase().replace(' ', '_');
31 throw new HttpApiError(result.error, "API", error_code, result);
32 }else{
33 return result;
34 }
35 });
36}
37
38exports.lastPrice = function(pair){
39 return query('last_price', pair);
40}
41exports.depth = function(pair){
42 return query('depth', pair);
43}
44exports.trades = function(pair){
45 return query('trades', pair);
46}
47exports.ticker = function(pair){
48 return query('ticker', pair);
49}
50exports.currency_pairs = function(pair){
51 return query('currency_pairs', pair || "all");
52}
53exports.currencies = function(pair){
54 return query('currencies', pair || "all");
55}