Skip to content

Instantly share code, notes, and snippets.

@lmeyerov
Created February 14, 2021 17:26
Show Gist options
  • Save lmeyerov/b31c291b00fb5365bfbf149a7510a286 to your computer and use it in GitHub Desktop.
Save lmeyerov/b31c291b00fb5365bfbf149a7510a286 to your computer and use it in GitHub Desktop.
//Written with intent for moving to JS client
import axios from 'axios';
/*
var gFile = new GraphistryFileUploader(null, {'jwtToken': 'xyz123'})
gFile.createFile({
'file_type': 'csv',
'name': 'adsf',
'description': 'asdf',
'file_compression': 'asdf',
'parser_options': {}
}).then(v => {
return gFile.uploadFileData(data, {'file_id': v.file_id});
})
*/
//Clientside within Graphistry (not node)
//Handles JWT header for `Authorization: Bearer xyz123`
//Chainable with createFile info passed for uploadFileData via this._data
export class GraphistryFileUploader {
constructor(opts = {}, session={}, authMode = 'jwt', axiosOverride = null) {
this.opts = {
...opts
};
this._session = session;
this._data = null;
this._authMode = authMode;
this._axios = axiosOverride || axios;
}
headers() {
return {
'Authorization': `Bearer ${this._session.jwtToken}`,
'Content-Type': 'application/json'
};
}
//?dict => Promise dict
//set this._data with file container upon success
createFile(opts = {}) {
return new Promise((resolve) => resolve(true))
.then((v) => {
const fileCompressionObj = {'compression': opts.compression || ''};
const preCombinedOpts = {...this.opts, ...opts};
const combinedOpts = {
...preCombinedOpts,
//csrfmiddlewaretoken: this._session.csrfmiddlewaretoken,
parser_options: {
...(this.opts.parser_options || {}),
...(opts.parser_options || {}),
}
};
const axiosCfg = {
'method': 'POST',
'url': '/api/v2/files/',
'data': combinedOpts,
'headers': this.headers(),
'withCredentials': true,
'timeout': 5000,
'validateStatus': (status) => status === 200 || status === 201
};
return this._axios(axiosCfg);
})
.then((resp) => {
this._data = resp.data;
return resp.data;
})
.catch((e) => {
const msg = e && e.response && e.response.data ? e.response.data : ((e || '').toString() || 'unknown response');
throw new Error(`Error (code ${ e && e.response && e.response.status ? e.response.status : 'n/a' }) Failed creating file container:\n ${ msg }`);
});
}
uploadFileData(data, opts = {}) {
const { erase, fileID, progress } = opts;
const axiosCfg = {
'method': 'POST',
'url': `/api/v2/upload/files/${ fileID || this._data.file_id }`,
'params': {
'erase': erase === undefined ? true : erase,
},
'headers': this.headers(),
'withCredentials': true,
'data': data,
...(progress ? {'onUploadProgress': progress} : {}),
'validateStatus': (status) => status === 200
};
return this._axios(axiosCfg)
.then((resp) => {
this._data = resp.data;
return resp.data;
})
.catch(e => {
let err;
try {
const data = e.response.data;
err = data.message;
} catch (e) {}
if (!err) {
err = (e||'').toString();
}
throw new Error(`Failed file transfer:\n ${err}`);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment