add setup.js and add getRecord, getRecords, fix api logs

master
adb 4 years ago
parent 6e0d4164c1
commit 24ff180ea6

@ -3,8 +3,9 @@ import sha1 from 'sha1';
import readline from 'readline'; import readline from 'readline';
export class OvhApi{ export class OvhApi{
constructor({credentials}){ constructor({credentials, logs = true}){
this.credentials = credentials; this.credentials = credentials;
this.log = logs ? console.log : ()=>{};
this.baseUrl = this.credentials.apiUrl; this.baseUrl = this.credentials.apiUrl;
this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'}; this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'};
this.rl = readline.createInterface({ this.rl = readline.createInterface({
@ -18,17 +19,17 @@ export class OvhApi{
getMethod=rest=>rest.get, getMethod=rest=>rest.get,
header={accept: 'json'} header={accept: 'json'}
}){ }){
console.log(`api request at ${path} =>`); this.log(`api request at ${path} =>`);
console.log(header); this.log(header);
console.log(body); this.log(body);
let request = getMethod(rest)(`${this.baseUrl}${path}`); let request = getMethod(rest)(`${this.baseUrl}${path}`);
await Object.keys(header).forEach(key => request.set(key, header[key])); await Object.keys(header).forEach(key => request.set(key, header[key]));
return await request.send(body).then(res => { return await request.send(body).then(res => {
console.log('api response =>'); this.log('api response =>');
console.log(JSON.parse(res.text)); this.log(JSON.parse(res.text));
return JSON.parse(res.text); return JSON.parse(res.text);
}).catch(err => { }).catch(err => {
console.error(err); console.error(err.response.text || err);
return false; return false;
}); });
} }
@ -101,4 +102,20 @@ export class OvhApi{
path: '/auth/time' path: '/auth/time'
}); });
} }
async getRecords({domain, fieldType = undefined, subDomain = undefined}){
return await this.sendSignedRequest({
path: `/domain/zone/${domain}/record`,
body: {
fieldType,
subDomain
}
});
}
async getRecord({domain, id}){
return await this.sendSignedRequest({
path: `/domain/zone/${domain}/record/${id}`
});
}
} }

@ -0,0 +1,24 @@
import {JsonDataStore} from './JsonDataStore.js';
import {OvhApi} from "./OvhApi.js";
let store = new JsonDataStore('config.json').getSyncedData();
if (!store.ovhCredentials){
console.log('ovhCredentials are undefined!');
process.exit();
}
if (!store.ovhCredentials.applicationKey || !store.ovhCredentials.applicationSecret){
console.log('applicationKey and/or applicationSecret are missing');
process.exit();
}
let dns = new OvhApi({credentials: store.ovhCredentials, logs: false});
if (!store.ovhCredentials.consumerKey) await dns.getConsumerKey();
console.log('list all available records for defined domains =>');
Object.keys(store.records).forEach(domain => {
dns.getRecords({domain}).then(ids => ids.forEach(id => {
dns.getRecord({domain, id}).then(console.log);
}));
});
Loading…
Cancel
Save