Last active
September 21, 2017 09:29
-
-
Save ElectricImpSampleCode/413f4681a240ddd4aba8dbb351b669b9 to your computer and use it in GitHub Desktop.
impCentral API Example Code: Data Pagination
This file contains 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
// CONSTANTS | |
// Replace the 'USERNAME' and 'PASSWORD' values with your own | |
// Replace the 'ACCESS_TOKEN' value with one retrieved using 'products.js' at | |
// https://gist.github.com/ElectricImpSampleCode/f0fb7f8bd04be009beeb5ee86dbd406a | |
const USERNAME = '...' | |
const PASSWORD = '...' | |
const ACCESS_TOKEN = '...' | |
const API_URL = 'api.electricimp.com' | |
const API_VER = '/v5/' | |
// GLOBALS | |
var https = require('https'); | |
var deployments = []; | |
var next = null; | |
// FUNCTIONS | |
function setOptions(verb, path, token) { | |
// Returns an HTTPS request options object primed for API usage | |
return { | |
hostname: API_URL, | |
path: API_VER + path, | |
method: verb, | |
headers: { | |
'Content-Type': 'application/vnd.api+json', | |
'Authorization': 'Bearer ' + token | |
} | |
}; | |
} | |
function showError(resp) { | |
console.log(`STATUS: ${resp.statusCode}`); | |
console.log(`HEADERS: ${JSON.stringify(resp.headers)}`); | |
} | |
function getDeplopyments() { | |
// Any previous iteration of getDeployments() will have updated the variable 'next' | |
// with the URL of the next page of data, so make use of it - or default to the | |
// first page if 'next' is null | |
let options; | |
if (next) { | |
options = setOptions('GET', 'deployments' + '?' + next.link, ACCESS_TOKEN); | |
} else { | |
options = setOptions('GET', 'deployments', ACCESS_TOKEN); | |
} | |
let req = https.request(options, (resp) => { | |
// Construct the returned response body | |
let body = ''; | |
resp.setEncoding('utf8'); | |
resp.on('data', (chunk) => { body += chunk; }); | |
// Code that's called when the body is retrieved | |
resp.on('end', () => { | |
if (body.length > 0) { | |
try { | |
// Is the returned data valid JSON? | |
const data = JSON.parse(body); | |
if (resp.statusCode === 200) { | |
// 'data' is an array of deployment objects | |
for (let item of data.data) { | |
let dep = {}; | |
dep.id = item.id; | |
dep.sha = item.attributes.sha; | |
dep.deviceCode = item.attributes.device_code; | |
dep.agentCode = item.attributes.agent_code; | |
dep.deployed = item.created_at; | |
// Add the deployment to the 'deployments' array | |
deployments.push(dep); | |
} | |
// Check for paginated data | |
if ('links' in data) { | |
if ('next' in data.links) { | |
// Split the link URL at the ? | |
let narray = data.links.next.split('?'); | |
// If we have no 'next' | |
if (next === null) { | |
next = {}; | |
next.count = 0; | |
} | |
// Update the next link and count of pages | |
next.link = narray[1]; | |
next.count += 1; | |
console.log(`Getting page ${next.count + 1} of deployments`); | |
// Call the function again to get the next page | |
getDeplopyments(); | |
} else { | |
// We've received all the pages - report the fact | |
next = null; | |
console.log(`${deployments.length} deployments have been made to this account's Products`); | |
} | |
} | |
} else { | |
// API Error | |
console.log('API ERROR: ' + data.code + ' (' + data.message + ')'); | |
} | |
} catch (err) { | |
// JSON error | |
console.error(err.message); | |
} | |
} else { | |
// HTTP Error | |
showError(resp); | |
} | |
}); | |
}); | |
// Error callback | |
req.on('error', (err) => { console.error(`REQUEST ERROR: ${err.message}`); }); | |
// Send the request | |
req.end(); | |
} | |
// RUNTIME | |
getDeplopyments(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment