now reacting to rate limiting
This commit is contained in:
@ -1,73 +1,76 @@
|
||||
import "typings-global";
|
||||
import plugins = require("./cflare.plugins");
|
||||
import helpers = require("./cflare.classes.helpers");
|
||||
import * as interfaces from './cflare.interfaces'
|
||||
|
||||
export class CflareAccount {
|
||||
private authEmail:string;
|
||||
private authKey:string;
|
||||
private authCheck(){
|
||||
private authEmail: string;
|
||||
private authKey: string;
|
||||
private authCheck() {
|
||||
return (this.authEmail && this.authKey); //check if auth is available
|
||||
}
|
||||
constructor(){
|
||||
|
||||
constructor() {
|
||||
|
||||
};
|
||||
auth(optionsArg:{email:string,key:string}){
|
||||
auth(optionsArg: { email: string, key: string }) {
|
||||
this.authEmail = optionsArg.email;
|
||||
this.authKey = optionsArg.key;
|
||||
this.authKey = optionsArg.key;
|
||||
}
|
||||
getZoneId(domainName:string){
|
||||
getZoneId(domainName: string) {
|
||||
let done = plugins.q.defer();
|
||||
this.listZones(domainName)
|
||||
.then((responseArg) => {
|
||||
let filteredResponse = responseArg.result.filter((zoneArg)=>{
|
||||
.then(zoneArrayArg => {
|
||||
let filteredResponse = zoneArrayArg.filter((zoneArg) => {
|
||||
return zoneArg.name === domainName;
|
||||
});
|
||||
if (filteredResponse.length >= 1){
|
||||
if (filteredResponse.length >= 1) {
|
||||
done.resolve(filteredResponse[0].id);
|
||||
} else {
|
||||
plugins.beautylog.error("the domain " + domainName.blue + " does not appear to be in this account!");
|
||||
plugins.beautylog.error(`the domain ${domainName} does not appear to be in this account!`);
|
||||
done.reject(undefined);
|
||||
}
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
getRecord(domainNameArg:string,typeArg:string){
|
||||
getRecord(domainNameArg: string, typeArg: string): Promise<interfaces.ICflareRecord> {
|
||||
let done = plugins.q.defer();
|
||||
let result: interfaces.ICflareRecord
|
||||
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
this.listRecords(domain.zoneName)
|
||||
.then((responseArg) => {
|
||||
let filteredResponse = responseArg.result.filter((recordArg) => {
|
||||
return (recordArg.type == typeArg && recordArg.name == domainNameArg);
|
||||
.then((recordArrayArg) => {
|
||||
let filteredResponse = recordArrayArg.filter((recordArg) => {
|
||||
return (recordArg.type == typeArg && recordArg.name == domainNameArg);
|
||||
})
|
||||
done.resolve(filteredResponse[0]);
|
||||
})
|
||||
return done.promise;
|
||||
};
|
||||
createRecord(domainNameArg:string,typeArg:string,contentArg:string){
|
||||
createRecord(domainNameArg: string, typeArg: string, contentArg: string) {
|
||||
let done = plugins.q.defer();
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
this.getZoneId(domain.zoneName)
|
||||
.then((domainIdArg)=>{
|
||||
.then((domainIdArg) => {
|
||||
let dataObject = {
|
||||
name: domain.fullName,
|
||||
type: typeArg,
|
||||
content: contentArg
|
||||
};
|
||||
this.request("POST","/zones/" + domainIdArg + "/dns_records",dataObject)
|
||||
.then(function(responseArg){
|
||||
this.request("POST", "/zones/" + domainIdArg + "/dns_records", dataObject)
|
||||
.then(function (responseArg) {
|
||||
done.resolve(responseArg);
|
||||
});
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
removeRecord(domainNameArg:string,typeArg:string){
|
||||
removeRecord(domainNameArg: string, typeArg: string) {
|
||||
let done = plugins.q.defer();
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
this.getRecord(domain.fullName,typeArg)
|
||||
this.getRecord(domain.fullName, typeArg)
|
||||
.then((responseArg) => {
|
||||
if(responseArg){
|
||||
let requestRoute:string = "/zones/" + responseArg.zone_id + "/dns_records/" + responseArg.id;
|
||||
this.request("DELETE",requestRoute)
|
||||
if (responseArg) {
|
||||
let requestRoute: string = "/zones/" + responseArg.zone_id + "/dns_records/" + responseArg.id;
|
||||
this.request("DELETE", requestRoute)
|
||||
.then((responseArg) => {
|
||||
done.resolve(responseArg);
|
||||
});
|
||||
@ -77,59 +80,88 @@ export class CflareAccount {
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
updateRecord(domainNameArg:string,typeArg:string,valueArg){
|
||||
updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
||||
let done = plugins.q.defer();
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
return done.promise;
|
||||
};
|
||||
listRecords(domainNameArg:string){
|
||||
let done = plugins.q.defer();
|
||||
listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
||||
let done = plugins.q.defer<interfaces.ICflareRecord[]>();
|
||||
let result: interfaces.ICflareRecord[] = []
|
||||
|
||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
||||
this.getZoneId(domain.zoneName)
|
||||
.then((domainIdArg)=>{
|
||||
this.request("GET","/zones/" + domainIdArg + "/dns_records?per_page=100")
|
||||
.then(function(responseArg){
|
||||
done.resolve(responseArg);
|
||||
.then((domainIdArg) => {
|
||||
this.request("GET", "/zones/" + domainIdArg + "/dns_records?per_page=100")
|
||||
.then(function (responseArg: any) {
|
||||
result = responseArg.result
|
||||
done.resolve(result);
|
||||
});
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
listZones(domainName?:string){ // TODO: handle pagination
|
||||
let done = plugins.q.defer();
|
||||
listZones(domainName?: string): Promise<interfaces.ICflareZone[]> { // TODO: handle pagination
|
||||
let done = plugins.q.defer<interfaces.ICflareZone[]>();
|
||||
let requestRoute = "/zones?per_page=50"
|
||||
if(domainName) requestRoute = requestRoute + "&name=" + domainName;
|
||||
let result = {};
|
||||
this.request("GET",requestRoute)
|
||||
.then(function(responseArg){
|
||||
result = responseArg;
|
||||
if (domainName) requestRoute = requestRoute + "&name=" + domainName;
|
||||
let result = [];
|
||||
this.request("GET", requestRoute)
|
||||
.then((responseArg: any) => {
|
||||
result = responseArg.result;
|
||||
done.resolve(result);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
request(methodArg:string,routeArg:string,dataArg = {}){
|
||||
request(methodArg: string, routeArg: string, dataArg = {}) {
|
||||
let done = plugins.q.defer();
|
||||
let jsonArg:string = JSON.stringify(dataArg);
|
||||
let jsonArg: string = JSON.stringify(dataArg);
|
||||
let options = {
|
||||
method:methodArg,
|
||||
url:"https://api.cloudflare.com/client/v4" + routeArg,
|
||||
headers:{
|
||||
"Content-Type":"application/json",
|
||||
"X-Auth-Email":this.authEmail,
|
||||
"X-Auth-Key":this.authKey
|
||||
method: methodArg,
|
||||
url: "https://api.cloudflare.com/client/v4" + routeArg,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-Auth-Email": this.authEmail,
|
||||
"X-Auth-Key": this.authKey
|
||||
},
|
||||
body:jsonArg
|
||||
body: jsonArg
|
||||
};
|
||||
//console.log(options);
|
||||
plugins.request(options,function(err, res, body){
|
||||
if (!err && res.statusCode == 200) {
|
||||
var responseObj = JSON.parse(body);
|
||||
done.resolve(responseObj);
|
||||
} else {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
done.reject(err);
|
||||
};
|
||||
});
|
||||
let retryCount = 0
|
||||
|
||||
let makeRequest = () => {
|
||||
plugins.request(options, function (err, res, body) {
|
||||
let responseObj
|
||||
try {
|
||||
responseObj = JSON.parse(body);
|
||||
} catch (err) {
|
||||
console.log(res.statusCode)
|
||||
retryRequest()
|
||||
return
|
||||
}
|
||||
if (!err && res.statusCode === 200) {
|
||||
done.resolve(responseObj);
|
||||
} else if (!err && res.statusCode === 429) {
|
||||
console.log('rate limited! Waiting for retry!')
|
||||
retryRequest()
|
||||
return
|
||||
} else {
|
||||
console.log(res.statusCode)
|
||||
console.log(responseObj.messages);
|
||||
console.log(responseObj.errors);
|
||||
done.reject(err);
|
||||
};
|
||||
});
|
||||
}
|
||||
let retryRequest = async (delayTimeArg = 15000) => {
|
||||
console.log(`retry started and waiting for ${delayTimeArg} ms`)
|
||||
await plugins.smartdelay.delayFor(delayTimeArg)
|
||||
if (retryCount < 3) {
|
||||
retryCount++
|
||||
makeRequest()
|
||||
return
|
||||
}
|
||||
}
|
||||
makeRequest()
|
||||
return done.promise;
|
||||
}
|
||||
};
|
59
ts/cflare.interfaces.ts
Normal file
59
ts/cflare.interfaces.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import * as plugins from './cflare.plugins'
|
||||
|
||||
export interface ICflareZone {
|
||||
"id": string
|
||||
"name": string
|
||||
"development_mode": number
|
||||
"original_name_servers": string[]
|
||||
"original_registrar": string
|
||||
"original_dnshost": string
|
||||
"created_on": string
|
||||
"modified_on": string
|
||||
"name_servers": string[]
|
||||
"owner": {
|
||||
"id": string
|
||||
"email": string
|
||||
"owner_type": string
|
||||
},
|
||||
"permissions": string []
|
||||
"plan": {
|
||||
"id": string
|
||||
"name": string
|
||||
"price": number
|
||||
"currency": string
|
||||
"frequency": string
|
||||
"legacy_id": string
|
||||
"is_subscribed": boolean
|
||||
"can_subscribe": boolean
|
||||
},
|
||||
"plan_pending": {
|
||||
"id": string
|
||||
"name": string
|
||||
"price": number
|
||||
"currency": string
|
||||
"frequency": string
|
||||
"legacy_id": string
|
||||
"is_subscribed": string
|
||||
"can_subscribe": string
|
||||
},
|
||||
"status": string
|
||||
"paused": boolean
|
||||
"type": string
|
||||
"checked_on": string
|
||||
}
|
||||
|
||||
export interface ICflareRecord {
|
||||
"id": string
|
||||
"type": string
|
||||
"name": string
|
||||
"content": string
|
||||
"proxiable": boolean
|
||||
"proxied": boolean
|
||||
"ttl": number
|
||||
"locked": boolean
|
||||
"zone_id": string
|
||||
"zone_name": string
|
||||
"created_on": string
|
||||
"modified_on": string
|
||||
"data": any
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import "typings-global";
|
||||
export let beautylog = require("beautylog");
|
||||
export let q = require("q");
|
||||
export import q = require("smartq");
|
||||
export let request = require("request");
|
||||
export import smartstring = require("smartstring");
|
||||
export import smartstring = require("smartstring");
|
||||
export import smartdelay = require('smartdelay');
|
Reference in New Issue
Block a user