-
-
Save myndzi/ddbdfd5207a547807f09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'), | |
request = Promise.promisifyAll(require('request')), | |
extend = require('extend'); | |
// this would be more appropriately imported from some config file | |
var defaultOptions = { | |
/* whatever request options you are using go here */ | |
url: 'http://jira-url/' | |
}; | |
// this function ONLY accumulates the list of issues via request batching | |
function getIssues(batchSize, requestDelay, _options) { | |
var options = extend(true, defaultOptions, { | |
body: { | |
startAt: 0, | |
maxResults: batchSize | |
} | |
}, _options); // _options can be used to override anything you want | |
// If I knew up front how many requests I'd be making, I could use Promise.reduce | |
// but I don't, so I'm using a recursively calling-itself promise instead | |
var issues = [ ]; // this scope variable is specific to this batch of requests | |
return (function next() { | |
return request(options) | |
.spread(function (r, body) { | |
if (r.statusCode !== 200) { | |
// this is a breaking error, we don't want or need to continue our loop | |
throw new Error("Failed POST: " + r.statusCode + " " + r.statusMessage); | |
} | |
// add this batch to the array we're working with | |
issues = isssues.concat(body.issues); | |
// see if we need to get another batch | |
if (issues.length < body.total) { | |
// get the next batch | |
// numToRequest should be passed into getIssues most likely | |
options.body.startAt += batchSize; | |
return Promise.delay(requestDelay).then(next); | |
} | |
// return the accumulated result -- this ends the recursive promise loop thing | |
return issues; | |
}); | |
})(); // this empty array initializes 'issues' in the 'next' function | |
} | |
// this function ONLY transforms the http issues response into whatever | |
// format you are trying to put in your database | |
function listify(issue) { | |
return { | |
/* whatever transformation you're doing here */ | |
}; | |
} | |
// this function ONLY inserts the data | |
function putIssuesInDatabase(issues) { | |
return knex.insert(issues).into('table'); | |
} | |
// this function composes all the pieces above to present your final api | |
function doYourStuff(_opts) { | |
var opts = extend({ | |
numToRequest: 50, // default value | |
timeToWaitAfterRequest: 1000 // default value | |
}, _opts); // overrides values if specified | |
return getIssues(opts.numToRequest, opts.timeToWaitAfterRequest) | |
.map(listifyIssues) // .map is specific to bluebird | |
.then(putIssuesInDatabase); | |
} | |
// any errors in the above functions will get propagated to here, at the top | |
// level where we're actually calling the stuff. | |
doYourStuff().then(function (result) { | |
// result is the return from knex | |
}).catch(function (err) { | |
console.error(err); | |
// there was some problem | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment