add sendSignedRequest, getConsumerKey, getSignature
This commit is contained in:
parent
1b193248ed
commit
bb383094f7
93
OvhApi.js
93
OvhApi.js
@ -1,37 +1,73 @@
|
|||||||
import rest from 'superagent';
|
import rest from 'superagent';
|
||||||
|
import sha1 from 'sha1';
|
||||||
|
import readline from 'readline';
|
||||||
|
|
||||||
export class OvhApi{
|
export class OvhApi{
|
||||||
async constructor({credentials}){
|
constructor({credentials}){
|
||||||
this.credentials = credentials;
|
this.credentials = credentials;
|
||||||
this.baseUrl = credentials.apiUrl;
|
this.baseUrl = this.credentials.apiUrl;
|
||||||
this.apiToken = await this.getToken({applicationKey: credentials.applicationKey});
|
this.methods = {get: 'GET', post: 'POST', put: 'PUT', delete: 'DELETE', path: 'PATCH'};
|
||||||
|
this.rl = readline.createInterface({
|
||||||
|
input: process.stdin
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRequest({
|
async sendRequest({
|
||||||
path,
|
path,
|
||||||
obj={},
|
body={},
|
||||||
method=rest=>rest.get,
|
getMethod=rest=>rest.get,
|
||||||
header={accept: 'json'}
|
header={accept: 'json'}
|
||||||
}){
|
}){
|
||||||
let request = method(rest)(`${this.baseUrl}${path}`);
|
console.log(`api request at ${path} =>`);
|
||||||
|
console.log(header);
|
||||||
|
console.log(body);
|
||||||
|
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(obj).then(res => {
|
return await request.send(body).then(res => {
|
||||||
|
console.log('api response =>');
|
||||||
|
console.log(JSON.parse(res.text));
|
||||||
return JSON.parse(res.text);
|
return JSON.parse(res.text);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log(err);
|
console.error(err);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getToken({applicationKey}){
|
async sendSignedRequest({
|
||||||
return await this.sendRequest({
|
path,
|
||||||
|
body={},
|
||||||
|
getMethod=rest=>rest.get,
|
||||||
|
header={accept: 'json'}
|
||||||
|
}){
|
||||||
|
if (!this.credentials.consumerKey) await this.getConsumerKey();
|
||||||
|
let timestamp = await this.getApiTime();
|
||||||
|
header['X-Ovh-Timestamp'] = timestamp;
|
||||||
|
header['X-Ovh-Signature'] = await this.getSignature({
|
||||||
|
method: getMethod(this.methods),
|
||||||
|
query: this.baseUrl+path,
|
||||||
|
body, timestamp
|
||||||
|
});
|
||||||
|
header['X-Ovh-Consumer'] = this.credentials.consumerKey;
|
||||||
|
await this.sendRequest({path, body, getMethod, header});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSignature({method = 'GET', query, body='', timestamp}){
|
||||||
|
return '$1$' + sha1(
|
||||||
|
this.credentials.applicationSecret+'+'+
|
||||||
|
this.credentials.consumerKey+'+'+
|
||||||
|
method+'+'+query+'+'+body+'+'+timestamp
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getConsumerKey(){
|
||||||
|
let res = await this.sendRequest({
|
||||||
path: '/auth/credential',
|
path: '/auth/credential',
|
||||||
method: rest=>rest.post,
|
getMethod: rest=>rest.post,
|
||||||
header: {
|
header: {
|
||||||
'X-Ovh-Application': applicationKey,
|
'X-Ovh-Application': this.credentials.applicationKey,
|
||||||
'Content-type': 'application/json'
|
'Content-type': 'application/json'
|
||||||
},
|
},
|
||||||
obj: {
|
body: {
|
||||||
accessRules: [
|
accessRules: [
|
||||||
{method: 'GET', path: '/domain/zone/*'},
|
{method: 'GET', path: '/domain/zone/*'},
|
||||||
{method: 'POST', path: '/domain/zone/*'},
|
{method: 'POST', path: '/domain/zone/*'},
|
||||||
@ -39,17 +75,28 @@ export class OvhApi{
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
this.credentials.consumerKey = res.consumerKey;
|
||||||
|
console.log('please validate on ovh site:');
|
||||||
async updateRecord({domain, subDomain, recordId, target, ttl = 3600}){
|
console.log(res.validationUrl);
|
||||||
return await this.sendRequest({
|
await this.rl.question('continue? (Y/n)', () => {
|
||||||
path: `/domain/zone/${domain}/record/${recordId}`,
|
switch (input) {
|
||||||
method: rest=>rest.put,
|
case 'n': process.exit(); break;
|
||||||
obj: {
|
default: return res.consumerKey;
|
||||||
subDomain,
|
|
||||||
target,
|
|
||||||
ttl
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateRecord({domain, subDomain, recordId, target, ttl = 3600}){
|
||||||
|
return await this.sendSignedRequest({
|
||||||
|
path: `/domain/zone/${domain}/record/${recordId}`,
|
||||||
|
getMethod: rest=>rest.put,
|
||||||
|
body: {subDomain, target, ttl}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getApiTime(){
|
||||||
|
return await this.sendRequest({
|
||||||
|
path: '/auth/time'
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,6 +10,7 @@
|
|||||||
}],
|
}],
|
||||||
"ovhCredentials": {
|
"ovhCredentials": {
|
||||||
"applicationKey": "",
|
"applicationKey": "",
|
||||||
|
"applicationSecret": "",
|
||||||
"apiUrl": "https://ca.api.ovh.com/1.0"
|
"apiUrl": "https://ca.api.ovh.com/1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,6 +11,7 @@
|
|||||||
"author": "adb",
|
"author": "adb",
|
||||||
"license": "",
|
"license": "",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"sha1": "^1.1.1",
|
||||||
"superagent": "^6.1.0"
|
"superagent": "^6.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
Loading…
Reference in New Issue
Block a user