Created
June 18, 2019 20:04
-
-
Save ashleyng/6f3c3c8fad256ccdf31efb37f65badab to your computer and use it in GitHub Desktop.
IoT Toggl Code
This file contains hidden or 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
| 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) | |
| }) | |
| } | |
| }; | |
This file contains hidden or 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
| 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(); | |
| }) | |
| } | |
| } |
This file contains hidden or 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
| 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