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
let advancedrequest = ; let requestData = await url: "http://api.somefriendsite.com/addFriend"; console;
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
Calling this.fail(timeoutInMs, messageToLog); will retry the request up to
10 times. This max can be passed in as an integer argument named maxRetries.
Call this.onFinish(this.data); in order to complete the request
AdvancedRequest { superargs; // always call superclass constructor! } // Override the postProcess function to provide your own checks { // this.data is the body of the request. if !thisdata // If the response was blank, call this.fail to retry the same request in 10 seconds this; else if thisdata != -1 // If the API has a limit and tells you about it, you can call this.fail and retry in 1 hour this; else // All was clear! MAKE SURE YOU HAVE this line at the end of your request! this; }; // Then once you've written your DevApiRequest class, maybe you'd call it so: { let sendFriendRequestReq = url: "http://api.somefriendsite.com/addFriend" method: "POST" name: "sendFriendRequest" // NOTE: used as identifier for interval postData: targetId: targetId auth_token: "24t4534token42i5h2" callback: callback ; sendFriendRequestReq;} ;
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
// Set interval timeouts for the requests of a particular nameadvancedrequest; ;