Skip to content

Instantly share code, notes, and snippets.

@ubi-gists
Last active June 1, 2017 02:16
Show Gist options
  • Save ubi-gists/80e213a68de14b15ed45e470502f8118 to your computer and use it in GitHub Desktop.
Save ubi-gists/80e213a68de14b15ed45e470502f8118 to your computer and use it in GitHub Desktop.
Manages data between Ubidots and PubNub
// https://ubidots.com/docs/api/index.html
export default (request) => {
const xhr = require('xhr');
const queryStringCodec = require('codec/query_string');
const token = request.message.token || '';
const apiKey = request.message.apiKey || '';
const action = request.message.action;
const payload = request.message.payload;
const labelDevice = request.message.labelDevice;
const variableLabel = request.message.variableLabel;
const variableId = request.message.variableId;
const force = request.message.force;
const start = request.message.start;
const end = request.message.end;
const deviceId = request.message.deviceId;
const figure = request.message.figure;
function makeRequest(apiKey, token, method, url, parameters) {
console.log(parameters);
let body = '';
let urlParams = '';
let headers = {};
headers['Content-Type'] = 'application/json';
if (apiKey) headers['X-Ubidots-ApiKey'] = apiKey;
if (token) headers['X-Auth-Token'] = token;
if (method == 'POST' || method == 'PUT' || method == 'DELETE') {
body = JSON.stringify(parameters);
} else {
urlParams = queryStringCodec.stringify(parameters);
}
const requestOptions = {
method: method,
headers: headers,
body: body
};
url = url + (urlParams ? `?${urlParams}` : '');
return xhr.fetch(url, requestOptions).then((response) => {
return response.json();
});
}
switch (action) {
// ↓ TOKEN ↓
case 'get-token':
return makeRequest(
apiKey,
null,
'POST',
'https://things.ubidots.com/api/v1.6/auth/token',
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
// ↑ TOKEN ↑
// ↓ VALUES ↓
case 'send-value':
return makeRequest(
null,
token,
'POST',
`https://things.ubidots.com/api/v1.6/devices/${labelDevice}`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'send-value-single-variable':
return makeRequest(
null,
token,
'POST',
`https://things.ubidots.com/api/v1.6/devices/${labelDevice}/${variableLabel}/values`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'send-value-multiple-variable':
return makeRequest(
null,
token,
'POST',
'http://things.ubidots.com/api/v1.6/collections/values' + (force ? '?force=true' : ''),
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'get-value':
return makeRequest(
null,
token,
'GET',
`http://things.ubidots.com/api/v1.6/devices/${labelDevice}/${variableLabel}/values`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'get-value-by-id':
return makeRequest(
null,
token,
'GET',
`http://things.ubidots.com/api/v1.6/variables/${variableId}/values`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'delete-values':
return makeRequest(
null,
token,
'DELETE',
`https://things.ubidots.com/api/v1.6/variables/${variableId}/values/${start}/${end}/`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
// ↑ VALUES ↑
// ↓ VARIABLES ↓
case 'create-variable':
return makeRequest(
null,
token,
'POST',
`http://things.ubidots.com/api/v1.6/datasources/${deviceId}/variables`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'get-variable':
return makeRequest(
null,
token,
'GET',
`http://things.ubidots.com/api/v1.6/devices/${labelDevice}/${variableLabel}/`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'list-variables':
return makeRequest(
null,
token,
'GET',
'http://things.ubidots.com/api/v1.6/variables/',
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'list-variables-for-device':
return makeRequest(
null,
token,
'GET',
`http://things.ubidots.com/api/v1.6/datasources/${deviceId}/variables/`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'delete-variable':
return makeRequest(
null,
token,
'DELETE',
`http://things.ubidots.com/api/v1.6/variables/${variableId}/`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
// ↑ VARIABLES ↑
// ↓ DEVICES ↓
case 'create-device':
return makeRequest(
null,
token,
'POST',
'http://things.ubidots.com/api/v1.6/datasources/',
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'get-device':
return makeRequest(
null,
token,
'GET',
`http://things.ubidots.com/api/v1.6/datasources/${deviceId}/`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'get-devices':
return makeRequest(
null,
token,
'GET',
'http://things.ubidots.com/api/v1.6/datasources/',
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
case 'delete-device':
return makeRequest(
null,
token,
'DELETE',
`http://things.ubidots.com/api/v1.6/datasources/${deviceId}/`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
// ↑ DEVICES ↑
// ↓ STATISTICS ↓
case 'statistics':
return makeRequest(
null,
token,
'GET',
`http://things.ubidots.com/api/v1.6/variables/${variableId}/statistics/${figure}/${start}/${end}/`,
payload
).then((json) => {
request.message.ubidotsResponse = json;
return request.ok();
});
// ↑ STATISTICS ↑
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment