Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

ADD deletePlot method and example #34

Merged
merged 9 commits into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
```
8 changes: 8 additions & 0 deletions examples/delete-plot.js
Original file line number Diff line number Diff line change
@@ -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);
});
49 changes: 49 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down