Repository navigation
Expand file tree
/
Copy pathdns.js
More file actions
112 lines (87 loc) · 2.19 KB
/
Copy pathdns.js
File metadata and controls
112 lines (87 loc) · 2.19 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var https = require('https');
if(!process.env.dnsimple_api_token) {
console.error('DNSimple API token not set, exiting');
return;
}
if(!process.env.dnsimple_email) {
console.error('DNSimple email not set, exiting');
return;
}
var config = {
token: process.env.dnsimple_api_token,
email: process.env.dnsimple_email,
baseHost: 'api.dnsimple.com',
basePath: '/v1/',
basePort: '443',
}
authenticate(function(response) {
console.log('User with id', response.user.id, 'authenticated');
getDomains(function(response) {
console.log('Domains:', response);
getRecords('eagerbeaver.io', function(response) {
console.log('Records:', response);
})
});
});
function createOptions(path) {
var options = {
host: config.baseHost,
port: config.basePort,
path: config.basePath + path,
headers: {
'X-DNSimple-Token': config.email + ':' + config.token,
Accept: 'application/json'
}
};
return options;
}
function doRequest(options, callback) {
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
callback(null, chunk);
});
});
req.on('error', function(e) {
callback(err, null);
});
req.end();
}
function authenticate(callback) {
console.log('Authenticating...');
var options = createOptions('user');
doRequest(options, function(err, response) {
if(err) {
console.err('Error authenticating:', err.message);
return;
}
callback(JSON.parse(response));
});
}
function getDomains(callback) {
console.log('Getting list of domains...');
var options = createOptions('domains');
doRequest(options, function(err, response) {
if(err) {
console.err('Error getting domains:', err.message);
return;
}
callback(JSON.parse(response));
});
}
function getRecords(domainName, callback) {
if(typeof(domainName) !== 'string') {
console.error('Domain name not set');
};
console.log('Getting list of records for domain "' + domainName + '"...')
var options = createOptions('domains/' + domainName + '/records');
doRequest(options, function(err, response) {
if(err) {
console.err('Error getting records for domain:', err.message);
return;
}
if(callback) {
callback(JSON.parse(response));
}
});
}