advancedrequest
ES6 JS class for intervaled requests with retries and timeouts
Installation
npm install advancedrequest --save
Usage
Manager for sending HTTP requests with multiple tries, wait intervals, and timeouts
Uses node module 'request'
This can be configured by adding names as keys and values with requiredInterval
in order to WAIT before sending another request of that name. If you wanted to
only send one request with name DevAPI_SendFriendRequest per minute, set the
requiredInterval to 1000*60 and the minute will automatically be enforced
All 'lastruns' start with the moment of script start
to make sure script restarts don't accidentally disobey the interval
Simple GET request example
var advancedrequest = require('advancedrequest');
new advancedrequest.AdvancedRequest({
url: "http://api.somefriendsite.com/addFriend",
callback: function (dataString) {
console.log("[+] API response received!", dataString);
}
}).run();
Inheritance example
Here you can define our own logic to determine if a request was successful
or needs to be retried by subclassing advancedrequest.AdvancedRequest
class DevApiRequest extends advancedrequest.AdvancedRequest {
constructor (args) {
super(args);
}
postProcess () {
if (!this.data) {
this.fail(10, "Request was blank! Is connection to internet broken?");
} else if (this.data.indexOf('Too many requests in the last hour!') != -1) {
this.fail(60*60, "Hit rate limit. Waiting 1 hour to retry");
} else {
this.onFinish(this.data);
}
}
};
function sendFriendRequestTo(targetId, callback) {
var sendFriendRequestReq = new DevApiRequest({
url: "http://api.somefriendsite.com/addFriend",
method: "POST",
name: "sendFriendRequest",
postData: { targetId: targetId, auth_token: "24t4534token42i5h2"},
callback: callback
});
sendFriendRequestReq.run();
}
sendFriendRequestTo(252452, function(json) {
console.log("[+] API response received!", json);
});
Intervaled request example
Enforce a defined interval between requests with the same 'name' passed into them.
Using the DevApiRequest class, we first set lastRunHash with the proper interval
advancedrequest.setLastRunHash({
'sendFriendRequest': { requiredInterval: 1000*15 },
'removeFriend': { requiredInterval: 1000*5, lastReqTime: new Date() }
});
sendFriendRequestTo(34, function(json) {
sendFriendRequestTo(35, function(json) {
sendFriendRequestTo(36, function(json) {
console.log("[+] 3 friend requests sent successfully with 15 seconds between them");
});
});
});
Credits
http://c-cfalcon.rhcloud.com