Skip to content

Instantly share code, notes, and snippets.

@ChadMoran
Created January 9, 2018 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChadMoran/b2472b2b063065adf162428960cb1c10 to your computer and use it in GitHub Desktop.
Save ChadMoran/b2472b2b063065adf162428960cb1c10 to your computer and use it in GitHub Desktop.
Parse Tesla API
'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