-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
75 lines (70 loc) · 2.04 KB
/
main.js
File metadata and controls
75 lines (70 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var request = require('request')
, crypto = require('crypto')
, qs = require('querystring')
, xml2js = require('xml2js')
, endPoint = 'https://api4.int.liverail.com'
, liverail = exports
, token = undefined
;
function setEndPoint(env) {
if ( env === 'production' ) {
endPoint = 'https://api4.liverail.com'
} else {
endPoint = 'https://api4.int.liverail.com'
}
}
liverail.setEndPoint = setEndPoint;
liverail.login = function (user, pass, callback) {
var parser = new xml2js.Parser();
var passHash = crypto.createHash('md5').update(pass).digest('hex');
request({
method: 'POST',
uri: endPoint + '/login',
headers: {"content-type": "application/x-www-form-urlencoded"},
body: qs.encode({
username: user,
password: passHash
})
}, function (error, response, body) {
parser.parseString(body, function (err, result) {
token = result.auth.token;
if (typeof callback !== "undefined") {
if (result.status !== 'success') return callback(result['_ERRORS_'], result)
return callback(false, result);
}
});
})
};
liverail.call = function () {
var parser = new xml2js.Parser();
var argArray = [];
for ( var i = 0 ; i < arguments.length ; i++ ) {
argArray.push(arguments[i]);
}
if ( typeof argArray[argArray.length - 1] === 'function' ) var callback = argArray.pop();
if ( token === undefined ) {
if ( typeof callback === 'function' ) return callback('Login token not set')
return null
}
var route = argArray.shift();
if ( typeof argArray[0] !== 'undefined' ) var input = argArray[0];
if ( typeof input === 'undefined' ) input = {};
input.token = token;
request({
method: 'POST',
uri: endPoint + route,
headers: {"content-type": "application/x-www-form-urlencoded"},
body: qs.encode(input)
}, function(error, response, body) {
if ( typeof callback !== 'undefined') {
if(error) {
return callback(error)
}
parser.parseString(body, function (error, result) {
if(error) return callback(error)
return callback(false, result, body, response)
});
}
});
};
setEndPoint(process.env.NODE_ENV);