Created
February 6, 2017 04:02
-
-
Save TaylorAckley/3469cf12c59b038928b783b660c37cb1 to your computer and use it in GitHub Desktop.
Retrieve all tickets from the Zendesk API.
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
"use strict"; | |
const request = require('request'); | |
const fs = require('fs'); | |
const req = require('request-promise'); | |
const async = require('asyncawait/async'); | |
const await = require('asyncawait/await'); | |
const appConfig = require('../config.js'); | |
//hoist | |
var tickets = []; | |
var _pgCount; //Total number of pages | |
var _pgsRetreived = 0; //Pages retrieved. | |
function getTickets() { //this initial function calls the Zendsk API and returns a count of how many pages of tickets we need to loop through. | |
return new Promise(function (resolve, reject) { | |
var options = { | |
method: 'GET', | |
url: 'https://obscura.zendesk.com/api/v2/search.json', | |
qs: { query: 'type:ticket tags:escalation' }, | |
headers: { | |
authorization: 'Basic xxxx', | |
'User-Agent': 'Request-Promise' | |
}, | |
json: true | |
}; | |
req(options) | |
.then((res) => { | |
console.log(`Returned ${res.count} tickets`); | |
resolve(Math.ceil(res.count / 100)); //get count of records, divide by 100 (# of tickets per page), round up. | |
}); | |
}); | |
} | |
var getPage = async(function (pg) { //Each time this function is looped through, the request will run async in parellel with the other requests. Pages may get done out of order. | |
var bucket = await (function () { | |
var options = { | |
method: 'GET', | |
url: 'https://obscura.zendesk.com/api/v2/search.json', | |
qs: { page: pg, query: 'type:ticket tags:xxx' }, //Search query. This is optional, you can elminate this line and change the endpoing to ticket.json instead of search.json | |
headers: { | |
authorization: 'Basic xxxxxxxx', | |
'User-Agent': 'Request-Promise' | |
}, | |
json: true | |
}; | |
return new Promise(function (resolve, reject) { | |
req(options) | |
.then((res) => { | |
let _tickets = res.results; // Array of objects representing tickets | |
for(let i = 0; i < _tickets.length; i++) { // Loop through the array and push the objects to the tickets array. | |
tickets.push(_tickets[i]); | |
} | |
_pgsRetreived += 1; //increment the page counter so we know when we're done | |
console.log(`Page ${pg} of ${_pgCount} done. (${_pgsRetreived})`); | |
if(_pgsRetreived === _pgCount) { //Check the page count and see when we're done. We use the _pgsRetrieved variable instead of the pg variable because the requests will finish out of order. | |
console.log(`Retrieved ${_pgsRetreived} of ${_pgCount} (${tickets.length}). Writing data to file.`); | |
fs.writeFile('./lib/data/ticket.data.json', JSON.stringify(tickets), (err) => { //write the results to a file for later use. | |
if (err) { | |
console.log(err); | |
} | |
} ); | |
} | |
resolve(true); | |
}) | |
.catch((err) => console.log(err)); | |
}); | |
}); | |
}); | |
getTickets() | |
.then((pgCount) => { | |
_pgCount = pgCount; //assign pagecount | |
console.log(`Retreiving ${_pgCount} pages of ticket data`); | |
}) | |
.then(() => { | |
for (var i = 1; i < (_pgCount + 1); i++) { //Once we know how many pages of tickets we have, we can loop through | |
console.log(`Getting page ${i} of ${_pgCount}`); | |
getPage(i) | |
.catch((err) => console.log(err)); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment