Last active
January 17, 2020 11:38
-
-
Save ElectricImpSampleCode/f0fb7f8bd04be009beeb5ee86dbd406a to your computer and use it in GitHub Desktop.
impCentral API Example Code: Retrieve Products and their Device Groups
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 | |
const USERNAME = '...' | |
const PASSWORD = '...' | |
const API_URL = 'api.electricimp.com' | |
const API_VER = '/v5/' | |
// GLOBALS | |
var https = require('https'); | |
var products = []; | |
var accessToken = '...'; | |
var refreshToken = '...'; | |
var expiryDate = '...'; | |
// FUNCTIONS | |
function setLoginOptions() { | |
// Returns an HTTPS request options object primed for API login | |
return { | |
hostname: API_URL, | |
path: API_VER + 'auth', | |
method: 'POST', | |
headers: {'Content-Type': 'application/json'} | |
}; | |
} | |
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)}`); | |
} | |
// RUNTIME | |
let req = https.request(setLoginOptions(), (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) { | |
accessToken = data.access_token; | |
expiryDate = data.expires_at; | |
refreshToken = data.refresh_token; | |
// Create a request to get the account's products | |
let req = https.request(setOptions('GET', 'products', accessToken), (resp) => { | |
let body = ''; | |
resp.setEncoding('utf8'); | |
resp.on('data', (chunk) => { body += chunk; }); | |
resp.on('end', () => { | |
if (body.length > 0) { | |
try { | |
let data = JSON.parse(body); | |
data = data.data; | |
if (resp.statusCode === 200) { | |
// 'data' is an array of product objects | |
for (let item of data) { | |
let product = {}; | |
product.id = item.id; | |
product.name = item.attributes.name; | |
product.devicegroups = []; | |
products.push(product); | |
} | |
console.log(`Got ${products.length} product(s)`); | |
for (let item of products) { | |
// For each product in 'products', get its device groups using product ID filtering | |
let req = https.request(setOptions('GET', 'devicegroups?filter[product.id]=' + item.id, accessToken), (resp) => { | |
let body = ''; | |
resp.setEncoding('utf8'); | |
resp.on('data', (chunk) => { body += chunk; }); | |
resp.on('end', () => { | |
if (body.length > 0) { | |
try { | |
let data = JSON.parse(body); | |
data = data.data; | |
if (resp.statusCode === 200) { | |
console.log(`Got ${data.length} device group(s)`); | |
for (let item of data) { | |
let dg = {}; | |
dg.id = item.id; | |
dg.name = item.attributes.name; | |
let pid = item.relationships.product.id; | |
for (let product of details) { | |
if (product.id === pid) { | |
// Add the device group to its parent product in our list, 'details' | |
product.devicegroups.push(dg); | |
break; | |
} | |
} | |
} | |
} | |
} catch (err) { | |
// JSON Error | |
console.error(err.message); | |
} | |
} else { | |
// HTTP Error | |
showError(resp); | |
} | |
}); | |
}); | |
// Send the request | |
req.end(); | |
} | |
setTimeout(function() { | |
// After 10s display the data | |
console.log("Products:"); | |
let pcount = 0; | |
for (let product of products) { | |
console.log(` ${pcount + 1}. ${product.name} (ID: ${product.id})`); | |
if (product.devicegroups.length > 0) { | |
console.log(" Device Groups:"); | |
let dcount = 0; | |
for (let dg of product.devicegroups) { | |
console.log(` ${dcount + 1}. ${dg.name} (ID: ${dg.id})`); | |
dcount += 1; | |
} | |
} else { | |
console.log(" No Device Groups"); | |
} | |
pcount += 1; | |
} | |
}, 10000); | |
} else { | |
// API Error | |
console.log('API ERROR: ' + data.code + ' (' + data.message + ')'); | |
} | |
} catch (err) { | |
// JSON Error | |
console.error(err.message); | |
} | |
} else { | |
// HTTP Error | |
showError(resp); | |
} | |
}); | |
}); | |
// Send the request | |
req.end(); | |
} 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}`); }); | |
// Begin by setting your credentials as the request body | |
req.write(JSON.stringify({ | |
'id': USERNAME, 'password': PASSWORD | |
})); | |
// Send the request | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment