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

Commit aef733d

Browse files
ADD deletePlot method and example
1 parent 562132d commit aef733d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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('../.')('username','apiKey');
4+
5+
plotly.deletePlot('888', function (err, msg) {
6+
if (err) console.log(err);
7+
else console.log(msg);
8+
});

index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,47 @@ 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+
var msg = 'Successfully deleted plot: ' + self.username + ':' + fid;
265+
callback(null, msg);
266+
} else {
267+
callback(body); // Pass out the error message from the backend
268+
}
269+
270+
});
271+
});
272+
273+
req.on('error', function (err) {
274+
callback(err);
275+
});
276+
277+
req.end();
278+
};
279+
239280
// response parse helper fn
240281
function parseRes (res, cb) {
241282
var body = '';

0 commit comments

Comments
 (0)