Skip to content

Commit

Permalink
Add vehicleById and vehicleByIdAsync function
Browse files Browse the repository at this point in the history
These functions use `options.vehicleID` to make a call to fetch a specific
Vehicle from the Tesla API, rather than fetching all vehicles and using
`carIndex` to select the requested vehicle.

Fixes mseminatore#273
  • Loading branch information
jonahwh committed Nov 25, 2021
1 parent 42bd2b0 commit 05762df
Show file tree
Hide file tree
Showing 3 changed files with 1,806 additions and 1,724 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ setPortalBaseURI() | sets the server for testing, pass null to reset
login() | authenticate with Tesla servers and retrieve the OAuth token
logout() | delete the current OAuth token
vehicle() | return information on the requested vehicle defined by `carIndex` in `options`
vehicleById() | return information on the requested vehicle defined by `vehicleID` in `options`
vehicles() | return information and option data for all vehicles
getModel(vehicle) | returns the Tesla model as a string from vehicle object
getPaintColor(vehicle) | returns the paint color as a string from vehicle object
Expand Down
56 changes: 56 additions & 0 deletions teslajs.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,62 @@ exports.vehicle = function vehicle(options, callback) {
*/
exports.vehicleAsync = Promise.denodeify(exports.vehicle);

/**
* Return vehicle information on the requested vehicle. Uses options.vehicleID
* to determine which vehicle to fetch data for.
* @function vehicleById
* @param {optionsType} options - options object
* @param {nodeBack} callback - Node-style callback
* @returns {Vehicle} vehicle JSON data
*/
exports.vehicleById = function vehicle(options, callback) {
log(API_CALL_LEVEL, "TeslaJS.vehicleById()");

callback = callback || function (err, vehicle) { /* do nothing! */ }

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

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

request(req, function (error, response, body) {
if (error) {
log(API_ERR_LEVEL, error);
return callback(error, null);
}

if (response.statusCode != 200) {
return callback(response.statusMessage, null);
}

log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body));
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));

try {
body = body.response;

callback(null, body);
} catch (e) {
log(API_ERR_LEVEL, 'Error parsing vehicle response');
callback(e, null);
}

log(API_RETURN_LEVEL, "\nGET request: " + "/vehicles/" + options.vehicleID + " completed.");
});
}

/**
* Return vehicle information on the requested vehicle. Uses options.vehicleID
* to determine which vehicle to fetch data for.
* @function vehicleByIdAsync
* @param {optionsType} options - options object
* @returns {Promise} vehicle JSON data
*/
exports.vehicleByIdAsync = Promise.denodeify(exports.vehicleById);

/**
* Return vehicle information on ALL vehicles
* @function vehicles
Expand Down
Loading

0 comments on commit 05762df

Please sign in to comment.