Created
January 9, 2018 02:33
-
-
Save ChadMoran/b2472b2b063065adf162428960cb1c10 to your computer and use it in GitHub Desktop.
Parse Tesla API
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
'use strict'; | |
const https = require('https'); | |
const AWS = require('aws-sdk'); | |
const AUTH_TOKEN = 'xxx'; | |
const VEHICLE_ID = 'yyy'; | |
const S3_BUCKET = 'zzz'; | |
function putObjectToS3(bucket, key, data) { | |
var s3 = new AWS.S3(); | |
var params = { | |
Bucket : bucket, | |
Key : key, | |
Body : data | |
} | |
s3.putObject(params, function(err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
else console.log(data); // successful response | |
}); | |
} | |
let reqOptions = { | |
host: 'owner-api.teslamotors.com', | |
path: '/api/1/vehicles/' + VEHICLE_ID + '/data', | |
headers: { | |
'Authorization': 'Bearer ' + AUTH_TOKEN | |
}, | |
method: 'GET' | |
}; | |
exports.handler = (event, context, callback) => { | |
const req = https.request(reqOptions, (res) => { | |
let body = ''; | |
res.setEncoding('utf8'); | |
res.on('data', (chunk) => body += chunk); | |
res.on('end', () => { | |
putObjectToS3(S3_BUCKET, (+new Date()).toString() + '.json', body); | |
callback(null, body); | |
}); | |
}); | |
req.on('error', callback); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment