-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (47 loc) · 1.48 KB
/
index.js
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
const https = require('https');
const url = require('url');
const bluebird = require('bluebird');
function API(username, password){
if (!(this instanceof API)){
return new API(username, password);
}
this.baseUrl = `https://${username}:${password}@authjet.com`;
this.checkAuth()
.then(({authenticated}) => {
if (!authenticated){
console.log(`${username} is NOT properly authenticated to AuthJet\nPlease check your username and password.`);
}
})
.error(err => console.log('Error authenticating to AuthJet\n', err));
}
const request = (href, cb) => {
let out = '';
const req = https.get(url.parse(href), res => {
res.on('data', data => out += data);
});
req.on('close', () => {
try {
return cb(null, JSON.parse(out));
} catch (e){
return cb(null, out);
}
});
req.on('error', cb);
};
API.prototype.send = bluebird.promisify(function(appId, recipient, cb){
const sendUrl = `${this.baseUrl}/api/send?appId=${appId}&recipient=${recipient}`;
return request(sendUrl, cb);
});
API.prototype.validate = bluebird.promisify(function(recipient, code, cb){
const sendUrl = `${this.baseUrl}/api/validate?code=${code}&recipient=${recipient}`;
return request(sendUrl, cb);
});
API.prototype.checkAuth = bluebird.promisify(function(cb){
const sendUrl = `${this.baseUrl}/api/check-auth`;
return request(sendUrl, cb);
});
API.prototype.balance = bluebird.promisify(function(cb){
const sendUrl = `${this.baseUrl}/api/balance`;
return request(sendUrl, cb);
});
module.exports = API;