<commit_before>//=====================================================================
// This is a Node.js module encapsulating the unofficial Tesla API set
//
// Github: https://github.com/mseminatore/TeslaJS
// NPM: https://www.npmjs.com/package/teslajs
//
// Copyright (c) 2016 <NAME>
//
// Refer to included LICENSE file for usage rights and restrictions
//=====================================================================

var request = require('request');
var colors = require('colors');

//=======================
// Streaming API portal
//=======================
var streamingPortal = "https://streaming.vn.teslamotors.com/stream/";
exports.streamingPortal = streamingPortal;

//===========================
// New OAuth-based API portal
//===========================
var portal = "https://owner-api.teslamotors.com/";
exports.portal = portal;

var portalBaseURI = process.env.TESLAJS_SERVER || portal;

//=======================
// Log levels
//=======================
var API_CALL_LEVEL = 1;
exports.API_CALL_LEVEL = API_CALL_LEVEL;

var API_RETURN_LEVEL = 2;
exports.API_RETURN_LEVEL = API_RETURN_LEVEL;

var API_BODY_LEVEL = 3;
exports.API_BODY_LEVEL = API_BODY_LEVEL;

var API_REQUEST_LEVEL = 4;
exports.API_REQUEST_LEVEL = API_REQUEST_LEVEL;

var API_RESPONSE_LEVEL = 5;
exports.API_RESPONSE_LEVEL = API_RESPONSE_LEVEL;

var API_LOG_ALL = 255;	// this value must be the last
exports.API_LOG_ALL = API_LOG_ALL;

var logLevel = process.env.TESLAJS_LOG || 0;

//===========================
// Adjustable console logging
//===========================
function log(level, str) {
    if (logLevel <= level)
        return;
    console.log(str);
}

//==========================
// Set/get the logging level
//==========================
exports.setLogLevel = function setLogLevel(level) {
    logLevel = level;
}

exports.getLogLevel = function getLogLevel(level) {
    return logLevel;
}

//============================
// set/get the portal base URI
//============================
exports.setPortalBaseURI = function setPortalBaseURI(uri) {
    if (!uri)
        portalBaseURI = portal; // reset to the default Tesla servers
    else
        portalBaseURI = uri;
}

exports.getPortalBaseURI = function getPortalBaseURI() {
    return portalBaseURI;
}

//==================================
// Log error messages to the console
//==================================
function err(str) {
    console.error(str.red);
}

//===============================================
// Login to the server and receive an OAuth token
//===============================================
exports.login = function login(username, password, callback) {
    log(API_CALL_LEVEL, "TeslaJS.login()".cyan);

    if (!callback)
        callback = function (result) { /* do nothing! */}

    var req = {
        method: 'POST',
        url: portalBaseURI + '/oauth/token',
        form: {
            "grant_type": "password",
            "client_id": c_id,
            "client_secret": c_sec,
            "email": username,
            "password": password
        }
    };

    log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req).green);

    request(req, function (error, response, body) {

        log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(body).magenta);

        var authToken;

        try {
            var authdata = JSON.parse(body);
            authToken = authdata.access_token;
        } catch (e) {
            err('Error parsing response to oauth token request');
        }

        callback({ error: error, response: response, body: body, authToken: authToken });

        log(API_RETURN_LEVEL, "TeslaJS.login() completed.".cyan);
    });
}

//==================================
// Invalidate the current auth token
//==================================
exports.logout = function logout(authToken, callback) {
    log(API_CALL_LEVEL, "TeslaJS.logout()".cyan);

    if (!callback)
        callback = function (result) { /* do nothing! */ }

    callback({ error: "Not implemented!", response: "Not implemented!", body: "Not implemented!" });

    log(API_RETURN_LEVEL, "TeslaJS.logout() completed.".cyan);

/*
    request({
        method: 'DELETE',
        url: portalBaseURI + 'logout',
        headers: { Authorization: "Bearer " + authToken, 'Content-Type': 'application/json; charset=utf-8' }
    }, function (error, response, body) {

        callback({ error: error, response: response, body: body });

        log(API_RETURN_LEVEL, "TeslaJS.logout() completed.");
    });
*/
}

//====================================================
// Return vehicle information on the requested vehicle
//====================================================
exports.vehicles = function vehicles(options, callback) {
    log(API_CALL_LEVEL, "TeslaJS.vehicles()".cyan);

    if (!callback)
        callback = function (vehicle) { /* do nothing! */ }

    var req = {
        method: 'GET',
        url: portalBaseURI + '/api/1/vehicles',
        headers: { Authorization: "Bearer " + options.authToken, 'Content-Type': 'application/json; charset=utf-8' }
    };

    log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req).green);

    request(req, function (error, response, body) {
        if (error)
            err(error);

        try {
            var data = JSON.parse(body);
        } catch (e) {
            err('Error parsing vehicles response');
        }
<commit_msg>attempt to fix vehicle response parse issue<commit_after>//=====================================================================
// This is a Node.js module encapsulating the unofficial Tesla API set
//
// Github: https://github.com/mseminatore/TeslaJS
// NPM: https://www.npmjs.com/package/teslajs
//
// Copyright (c) 2016 <NAME>
//
// Refer to included LICENSE file for usage rights and restrictions
//=====================================================================

var request = require('request');
var colors = require('colors');

//=======================
// Streaming API portal
//=======================
var streamingPortal = "https://streaming.vn.teslamotors.com/stream/";
exports.streamingPortal = streamingPortal;

//===========================
// New OAuth-based API portal
//===========================
var portal = "https://owner-api.teslamotors.com/";
exports.portal = portal;

var portalBaseURI = process.env.TESLAJS_SERVER || portal;

//=======================
// Log levels
//=======================
var API_CALL_LEVEL = 1;
exports.API_CALL_LEVEL = API_CALL_LEVEL;

var API_RETURN_LEVEL = 2;
exports.API_RETURN_LEVEL = API_RETURN_LEVEL;

var API_BODY_LEVEL = 3;
exports.API_BODY_LEVEL = API_BODY_LEVEL;

var API_REQUEST_LEVEL = 4;
exports.API_REQUEST_LEVEL = API_REQUEST_LEVEL;

var API_RESPONSE_LEVEL = 5;
exports.API_RESPONSE_LEVEL = API_RESPONSE_LEVEL;

var API_LOG_ALL = 255;	// this value must be the last
exports.API_LOG_ALL = API_LOG_ALL;

var logLevel = process.env.TESLAJS_LOG || 0;

//===========================
// Adjustable console logging
//===========================
function log(level, str) {
    if (logLevel <= level)
        return;
    console.log(str);
}

//==========================
// Set/get the logging level
//==========================
exports.setLogLevel = function setLogLevel(level) {
    logLevel = level;
}

exports.getLogLevel = function getLogLevel(level) {
    return logLevel;
}

//============================
// set/get the portal base URI
//============================
exports.setPortalBaseURI = function setPortalBaseURI(uri) {
    if (!uri)
        portalBaseURI = portal; // reset to the default Tesla servers
    else
        portalBaseURI = uri;
}

exports.getPortalBaseURI = function getPortalBaseURI() {
    return portalBaseURI;
}

//==================================
// Log error messages to the console
//==================================
function err(str) {
    console.error(str.red);
}

//===============================================
// Login to the server and receive an OAuth token
//===============================================
exports.login = function login(username, password, callback) {
    log(API_CALL_LEVEL, "TeslaJS.login()".cyan);

    if (!callback)
        callback = function (result) { /* do nothing! */}

    var req = {
        method: 'POST',
        url: portalBaseURI + '/oauth/token',
        form: {
            "grant_type": "password",
            "client_id": c_id,
            "client_secret": c_sec,
            "email": username,
            "password": password
        }
    };

    log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req).green);

    request(req, function (error, response, body) {

        log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(body).magenta);

        var authToken;

        try {
            var authdata = JSON.parse(body);
            authToken = authdata.access_token;
        } catch (e) {
            err('Error parsing response to oauth token request');
        }

        callback({ error: error, response: response, body: body, authToken: authToken });

        log(API_RETURN_LEVEL, "TeslaJS.login() completed.".cyan);
    });
}

//==================================
// Invalidate the current auth token
//==================================
exports.logout = function logout(authToken, callback) {
    log(API_CALL_LEVEL, "TeslaJS.logout()".cyan);

    if (!callback)
        callback = function (result) { /* do nothing! */ }

    callback({ error: "Not implemented!", response: "Not implemented!", body: "Not implemented!" });

    log(API_RETURN_LEVEL, "TeslaJS.logout() completed.".cyan);

/*
    request({
        method: 'DELETE',
        url: portalBaseURI + 'logout',
        headers: { Authorization: "Bearer " + authToken, 'Content-Type': 'application/json; charset=utf-8' }
    }, function (error, response, body) {

        callback({ error: error, response: response, body: body });

        log(API_RETURN_LEVEL, "TeslaJS.logout() completed.");
    });
*/
}

//====================================================
// Return vehicle information on the requested vehicle
//====================================================
exports.vehicles = function vehicles(options, callback) {
    log(API_CALL_LEVEL, "TeslaJS.vehicles()".cyan);

    if (!callback)
        callback = function (vehicle) { /* do nothing! */ }

    var req = {
        method: 'GET',
        url: portalBaseURI + '/api/1/vehicles',
        headers: { Authorization: "Bearer " + options.authToken, 'Content-Type': 'application/json; charset=utf-8' }
    };

    log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req).green);

    request(req, function (error, response, body) {
        if (error)
            err(error);

        var data = {};

        try {
            data = JSON.parse(body);
        } catch (e) {
            err('Error parsing vehicles response');
        }

{'fraction_non_alphanumeric': 0.26053215077605324, 'fraction_numerical': 0.0029563932002956393, 'mean_word_length': 3.4712928541924826, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2293943', 'n_tokens_mistral': 3160, 'n_tokens_neox': 2973, 'n_words': 713}