-
Notifications
You must be signed in to change notification settings - Fork 2
/
resellerclub.js
99 lines (90 loc) · 2.49 KB
/
resellerclub.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var querystring = require("querystring");
var https = require('https');
var ResellerClub = function(userId, key, test) {
this.userId = userId;
this.key = key;
this.test = (test)?true:false;
//var self = this;
this.domains = new Domains(this);
}
Domains = function(parent){
this.parent = parent;
}
ResellerClub.prototype._request = function(method, path, data, callback, args) {
if (data) {
contentLength = data.length;
} else {
contentLength = 0;
}
var options = {
host: ((this.test)?'test.':'')+'httpapi.com',
path: path,
method: method,
headers: {
'User-Agent': 'Mozilla/4.0 (Node.js ResellerClub client)',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': contentLength
}
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
var buffer = '';
res.on('data', function(data) {
buffer += data;
});
res.on('end', function() {
try {
var json = JSON.parse(buffer);
} catch (err) {
return callback(err);
}
if (json.error) {
callback(json.error);
} else {
if (json.status == 'ERROR'){
callback(json.message);
} else {
callback(null, json);
}
}
});
});
req.on('error', function(err) {
callback(err);
});
req.setTimeout(60000, function() {
req.abort();
});
req.end(data);
}
ResellerClub.prototype._get = function(apiClass, method, callback, args) {
var path = '/api/' + apiClass + '/' + method + '.json';
args['auth-userid'] = this.userId;
args['api-key'] = this.key;
path += '?' + querystring.stringify(args);
this._request('get', path, undefined, callback, args);
}
ResellerClub.prototype._post = function(apiClass, method, callback, args) {
var path = '/api/' + apiClass + '/' + method + '.json';
args['auth-userid'] = this.userId;
args['api-key'] = this.key;
var data = querystring.stringify(args);
this._request('post', path, data, callback, args);
}
//
// Domains API
//
Domains.prototype.available = function(args, callback) {
this.parent._get('domains', 'available', callback, args);
}
Domains.prototype.register = function(args, callback) {
if (args['contacts']){
var carr = ['reg-contact-id','admin-contact-id','tech-contact-id','billing-contact-id'];
for(f in carr){
args[carr[f]] = args['contacts'];
}
delete args['contacts'];
}
this.parent._post('domains', 'register', callback, args);
}
module.exports = ResellerClub;