diff --git a/README.md b/README.md index 9276b01..1c74348 100644 --- a/README.md +++ b/README.md @@ -233,3 +233,16 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { }); }); ``` + +##plotly.deletePlot(fid[, callback]) +`fid` is a String, the id of the plot you wish you delete +`callback` is a function with `err` and `plot` as parameters. `err`, if present, is the error message returned from the request. `plot` is the plot that was deleted. + +```javascript +var plotly = require('../.')('username','apiKey'); + +plotly.deletePlot('88', function (err, plot) { + if (err) console.log(err) + else console.log(plot); +}); +``` diff --git a/examples/delete-plot.js b/examples/delete-plot.js new file mode 100644 index 0000000..a3d1afc --- /dev/null +++ b/examples/delete-plot.js @@ -0,0 +1,8 @@ +'use strict'; + +var plotly = require('../.')('alexander.daniel','u1jactdk3m'); + +plotly.deletePlot('2718', function (err, plot) { + if (err) console.log(err); + else console.log(plot); +}); diff --git a/index.js b/index.js index 94a510d..fb7186a 100644 --- a/index.js +++ b/index.js @@ -236,6 +236,55 @@ Plotly.prototype.getImage = function (figure, opts, callback) { req.end(); }; +Plotly.prototype.deletePlot = function (fid, callback) { + if (!callback) callback = function () {}; + + var self = this; + + // Create the base64 authstring from buffer + var encodedAPIAuth = new Buffer(this.username + ':' + this.apiKey).toString('base64'); + + var options = { + host: 'api.plot.ly', + port: this.port, + path: '/v2/files/' + this.username + ':' + fid + '/trash', + method: 'POST', + agent: false, + withCredentials: true, + headers: { + 'Plotly-Client-Platform': 'nodejs ' + this.version, + 'authorization': 'Basic ' + encodedAPIAuth + } + }; + + var req = https.request(options, function (res) { + parseRes(res, function (err, body) { + + if (res.statusCode === 200) { + + callback(null, body); + + } else { + + var errObj = { + statusCode: res.statusCode, + err: body, + statusMessage: res.statusMessage + }; + + callback(errObj); // Pass out the error message from the backend + } + + }); + }); + + req.on('error', function (err) { + callback(err); + }); + + req.end(); +}; + // response parse helper fn function parseRes (res, cb) { var body = ''; diff --git a/package.json b/package.json index efb913f..2bff6b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.4", + "version": "1.0.5", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": {