Skip to content

Instantly share code, notes, and snippets.

@ashleyng
Created June 18, 2019 20:04
Show Gist options
  • Select an option

  • Save ashleyng/6f3c3c8fad256ccdf31efb37f65badab to your computer and use it in GitHub Desktop.

Select an option

Save ashleyng/6f3c3c8fad256ccdf31efb37f65badab to your computer and use it in GitHub Desktop.
IoT Toggl Code
const https = require('https');
const startTimer = require('./StartTimer');
const stopTimer = require('./StopTimer')
exports.handler = (event, context, callback) => {
if (event.clickType === 'SINGLE') {
startTimer.request()
.then(function(body) {
process.env.LAST_START_ID = body.data.id
callback(null, body);
})
} else {
console.log(event.clickType)
stopTimer.request()
.then(function(body) {
callback(null, body)
})
}
};
const https = require('https')
module.exports = {
request : function() {
return new Promise(function(resolve, reject) {
var options = {
host:'www.toggl.com',
path:'/api/v8/time_entries/start',
method: 'POST',
headers: {
'Authorization': 'Basic ' + process.env.TOGGL_ENCODED_KEY
}
};
var reqBody = JSON.stringify({
time_entry: {
created_with: 'AWS'
}
});
const req = https.request(options, (res) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
body = JSON.parse(body);
resolve(body);
});
});
req.on('error', function(err) {
reject(err)
});
req.write(reqBody);
req.end();
})
}
}
const https = require('https')
module.exports = {
request : function() {
return new Promise(function(resolve, reject) {
var options = {
host:'www.toggl.com',
path:'/api/v8/time_entries/' + process.env.LAST_START_ID + '/stop',
method: 'PUT',
headers: {
'Authorization': 'Basic ' + process.env.TOGGL_ENCODED_KEY
}
};
const req = https.request(options, (res) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
body = JSON.parse(body);
resolve(body);
});
});
req.on('error', function(err) {
reject(err)
});
req.end();
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment