Last active
September 21, 2017 09:29
-
-
Save ElectricImpSampleCode/3bb72231e2ffaa1893453e8d1860872a to your computer and use it in GitHub Desktop.
impCentral API Example Code: Uploading Device and Agent Code
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 fs = require('fs'); | |
var details = []; | |
// 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 uploadCode() { | |
// Use the existing token to create a new deployment to upload code | |
let req = https.request(setOptions('POST', 'deployments', ACCESS_TOKEN), (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 (resp.statusCode === 201) { | |
// Success - deployment created | |
console.log('Code uploaded successfully'); | |
} else { | |
// Decode the API error | |
try { | |
// Is the returned data valid JSON? | |
const data = JSON.parse(body); | |
console.error('API ERROR: ' + data.code + ' (' + data.message + ')'); | |
} catch (err) { | |
// JSON error | |
console.error(err.message); | |
} | |
} | |
}); | |
}); | |
// Error callback | |
req.on('error', (err) => { console.error(`REQUEST ERROR: ${err.message}`); }); | |
// Set up the request body for a new deployment. We create an object and a set of | |
// nested 'attributes' and 'relationships' objects | |
let dep = {}; | |
dep.type = 'deployment'; | |
let attributes = {}; | |
attributes.device_code = fs.readFileSync('device.code.nut').toString('utf8'); | |
attributes.agent_code = fs.readFileSync('agent.code.nut').toString('utf8'); | |
dep.attributes = attributes; | |
// NOTE This assumes 'details' array is populated with at least one Product, | |
// which in turn has at least one Device Group assigned to it | |
let relationships = {}; | |
let devicegroup = {}; | |
devicegroup.type = 'development_devicegroup'; | |
devicegroup.id = details[0].devicegroups[0].id; | |
relationships.devicegroup = devicegroup; | |
dep.relationships = relationships; | |
// Add deployment to required 'data' object | |
let data = {}; | |
data.data = dep; | |
// Write the data into the request as a string | |
req.write(JSON.stringify(data)); | |
// Send the request | |
req.end(); | |
} | |
// RUNTIME | |
uploadCode(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment