-- wrapped request to api call. Modified Authors

This commit is contained in:
Aneem 2019-06-14 11:19:52 +05:45
parent 333957ca3d
commit 96a1f55a08
2 changed files with 27 additions and 46 deletions

View File

@ -1 +1,2 @@
AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/) AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)
Aneem Patrabansha <aneempp@gmail.com> (https://aneem.com.np)

View File

@ -12,20 +12,23 @@ module.exports.create = function(config) {
var baseUrl = config.baseUrl || defaults.baseUrl; var baseUrl = config.baseUrl || defaults.baseUrl;
var authtoken = config.token; var authtoken = config.token;
function api(method, path, form) {
return request({
method: method,
url: baseUrl + path,
headers: {
Authorization: 'Bearer ' + authtoken,
'Content-Type': 'application/json'
},
json: true,
form: form
});
}
var helpers = { var helpers = {
getZonenames: function(/*opts*/) { getZonenames: function(/*opts*/) {
// { dnsHosts: [ xxxx.foo.example.com ] } // { dnsHosts: [ xxxx.foo.example.com ] }
var url = baseUrl + '/v2/domains/'; return api('GET', '/v2/domains/').then(function(resp) {
return request({
method: 'GET',
url: url,
headers: {
Authorization: 'Bearer ' + authtoken,
'Content-Type': 'application/json'
},
json: true
}).then(function(resp) {
return resp.body.domains.map(function(x) { return resp.body.domains.map(function(x) {
return x.name; return x.name;
}); });
@ -35,20 +38,13 @@ module.exports.create = function(config) {
// data:{dnsPrefix:"_88-acme-challenge-0e.foo",zone:"example.com",txt:"_cdZWaclIbkP1qYpMkZIURTK--ydQIK6d9axFmftWz0"} // data:{dnsPrefix:"_88-acme-challenge-0e.foo",zone:"example.com",txt:"_cdZWaclIbkP1qYpMkZIURTK--ydQIK6d9axFmftWz0"}
var dnsPrefix = data.dnsPrefix; var dnsPrefix = data.dnsPrefix;
var txt = data.txt; var txt = data.txt;
var url = baseUrl + '/v2/domains/' + data.zone + '/records';
// Digital ocean provides the api to fetch records by ID. Since we do not have id, we fetch all the records, // Digital ocean provides the api to fetch records by ID. Since we do not have id, we fetch all the records,
// filter the required TXT record // filter the required TXT record
return request({ return api('GET', '/v2/domains/' + data.zone + '/records').then(function(
method: 'GET', resp
url: url, ) {
json: true,
headers: {
Authorization: 'Bearer ' + authtoken,
'Content-Type': 'application/json'
}
}).then(function(resp) {
resp = resp.body; resp = resp.body;
var entries = var entries =
resp && resp &&
@ -69,25 +65,13 @@ module.exports.create = function(config) {
set: function(data) { set: function(data) {
var ch = data.challenge; var ch = data.challenge;
var txt = ch.dnsAuthorization; var txt = ch.dnsAuthorization;
var url = baseUrl + '/v2/domains/' + ch.dnsZone + '/records';
// console.info('Adding TXT', data); // console.info('Adding TXT', data);
return request({ return api('POST', '/v2/domains/' + ch.dnsZone + '/records', {
method: 'POST', type: 'TXT',
url: url, name: ch.dnsPrefix,
headers: { data: txt,
Authorization: 'Bearer ' + authtoken, ttl: 300
'Content-Type': 'application/json'
},
json: {
type: 'TXT',
// Note: dnsPrefix != dnsHost.split('.')[0]
// _greenlock-dryrun-2277.bar.foo.example.com => _greenlock-dryrun-2277.bar.foo
name: ch.dnsPrefix,
data: txt,
// Note: set a LOW ttl so that responses are not cached so long
ttl: 300
}
}).then(function(resp) { }).then(function(resp) {
resp = resp.body; resp = resp.body;
if (resp && resp.domain_record && resp.domain_record.data === txt) { if (resp && resp.domain_record && resp.domain_record.data === txt) {
@ -112,14 +96,10 @@ module.exports.create = function(config) {
var url = var url =
baseUrl + '/v2/domains/' + ch.dnsZone + '/records/' + txtRecord.id; baseUrl + '/v2/domains/' + ch.dnsZone + '/records/' + txtRecord.id;
return request({ return api(
method: 'DELETE', 'DELETE',
url: url, '/v2/domains/' + ch.dnsZone + '/records/' + txtRecord.id
headers: { ).then(function(resp) {
Authorization: 'Bearer ' + authtoken,
'Content-Type': 'application/json'
}
}).then(function(resp) {
resp = resp.body; resp = resp.body;
return true; return true;
}); });