add setup.js and add getRecord, getRecords, fix api logs
This commit is contained in:
parent
6e0d4164c1
commit
24ff180ea6
31
OvhApi.js
31
OvhApi.js
@ -3,8 +3,9 @@ import sha1 from 'sha1';
|
||||
import readline from 'readline';
|
||||
|
||||
export class OvhApi{
|
||||
constructor({credentials}){
|
||||
constructor({credentials, logs = true}){
|
||||
this.credentials = credentials;
|
||||
this.log = logs ? console.log : ()=>{};
|
||||
this.baseUrl = this.credentials.apiUrl;
|
||||
this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'};
|
||||
this.rl = readline.createInterface({
|
||||
@ -18,17 +19,17 @@ export class OvhApi{
|
||||
getMethod=rest=>rest.get,
|
||||
header={accept: 'json'}
|
||||
}){
|
||||
console.log(`api request at ${path} =>`);
|
||||
console.log(header);
|
||||
console.log(body);
|
||||
this.log(`api request at ${path} =>`);
|
||||
this.log(header);
|
||||
this.log(body);
|
||||
let request = getMethod(rest)(`${this.baseUrl}${path}`);
|
||||
await Object.keys(header).forEach(key => request.set(key, header[key]));
|
||||
return await request.send(body).then(res => {
|
||||
console.log('api response =>');
|
||||
console.log(JSON.parse(res.text));
|
||||
this.log('api response =>');
|
||||
this.log(JSON.parse(res.text));
|
||||
return JSON.parse(res.text);
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
console.error(err.response.text || err);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
@ -101,4 +102,20 @@ export class OvhApi{
|
||||
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}`
|
||||
});
|
||||
}
|
||||
}
|
24
setup.js
Normal file
24
setup.js
Normal file
@ -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…
Reference in New Issue
Block a user