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

Commit eae498a

Browse files
Merge pull request #34 from plotly/delete-plot
ADD deletePlot method and example
2 parents be1c327 + d3d48d3 commit eae498a

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,16 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) {
233233
});
234234
});
235235
```
236+
237+
##plotly.deletePlot(fid[, callback])
238+
`fid` is a String, the id of the plot you wish you delete
239+
`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.
240+
241+
```javascript
242+
var plotly = require('../.')('username','apiKey');
243+
244+
plotly.deletePlot('88', function (err, plot) {
245+
if (err) console.log(err)
246+
else console.log(plot);
247+
});
248+
```

examples/delete-plot.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var plotly = require('../.')('alexander.daniel','u1jactdk3m');
4+
5+
plotly.deletePlot('2718', function (err, plot) {
6+
if (err) console.log(err);
7+
else console.log(plot);
8+
});

index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,55 @@ Plotly.prototype.getImage = function (figure, opts, callback) {
236236
req.end();
237237
};
238238

239+
Plotly.prototype.deletePlot = function (fid, callback) {
240+
if (!callback) callback = function () {};
241+
242+
var self = this;
243+
244+
// Create the base64 authstring from buffer
245+
var encodedAPIAuth = new Buffer(this.username + ':' + this.apiKey).toString('base64');
246+
247+
var options = {
248+
host: 'api.plot.ly',
249+
port: this.port,
250+
path: '/v2/files/' + this.username + ':' + fid + '/trash',
251+
method: 'POST',
252+
agent: false,
253+
withCredentials: true,
254+
headers: {
255+
'Plotly-Client-Platform': 'nodejs ' + this.version,
256+
'authorization': 'Basic ' + encodedAPIAuth
257+
}
258+
};
259+
260+
var req = https.request(options, function (res) {
261+
parseRes(res, function (err, body) {
262+
263+
if (res.statusCode === 200) {
264+
265+
callback(null, body);
266+
267+
} else {
268+
269+
var errObj = {
270+
statusCode: res.statusCode,
271+
err: body,
272+
statusMessage: res.statusMessage
273+
};
274+
275+
callback(errObj); // Pass out the error message from the backend
276+
}
277+
278+
});
279+
});
280+
281+
req.on('error', function (err) {
282+
callback(err);
283+
});
284+
285+
req.end();
286+
};
287+
239288
// response parse helper fn
240289
function parseRes (res, cb) {
241290
var body = '';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "plotly",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Simple node.js wrapper for the plot.ly API",
55
"main": "index.js",
66
"devDependencies": {

0 commit comments

Comments
 (0)