-
-
Notifications
You must be signed in to change notification settings - Fork 278
Implement API to create a grid to store results of a scheduled query #444
Changes from 1 commit
26ec584
c26fa9d
b3504bf
3aa1c34
728888b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import fetch from 'node-fetch'; | ||
import {getSetting} from '../settings.js'; | ||
import Logger from '../logger'; | ||
import FormData from 'form-data'; | ||
|
||
import Logger from '../logger.js'; | ||
import { | ||
getCredentials, | ||
getSetting | ||
} from '../settings.js'; | ||
|
||
|
||
// Module to access Plot.ly REST API | ||
// | ||
// See API documentation at https://api.plot.ly/v2/ | ||
|
@@ -31,19 +36,15 @@ export function PlotlyAPIRequest(relativeUrl, {body, username, apiKey, accessTok | |
} | ||
|
||
export function newDatacache(payloadJSON, type, requestor) { | ||
const form = new FormData(); | ||
form.append('type', type); | ||
form.append('origin', 'Falcon'); | ||
form.append('payload', payloadJSON); | ||
const body = form; | ||
|
||
const users = getSetting('USERS'); | ||
const user = users.find( | ||
u => u.username === requestor | ||
); | ||
|
||
if (user) { | ||
form.append('username', user.username); | ||
const {username, apiKey, accessToken} = getCredentials(requestor); | ||
|
||
const body = new FormData(); | ||
body.append('type', type); | ||
body.append('origin', 'Falcon'); | ||
body.append('payload', payloadJSON); | ||
|
||
if (username) { | ||
body.append('username', username); | ||
} | ||
|
||
/* | ||
|
@@ -52,17 +53,12 @@ export function newDatacache(payloadJSON, type, requestor) { | |
* to proceed with blank `Authorization` header. | ||
*/ | ||
let authorization = ''; | ||
if (user) { | ||
const apiKey = user.apiKey; | ||
const accessToken = user.accessToken; | ||
|
||
if (apiKey) { | ||
authorization = 'Basic ' + new Buffer( | ||
requestor + ':' + apiKey | ||
).toString('base64'); | ||
} else if (accessToken) { | ||
authorization = `Bearer ${accessToken}`; | ||
} | ||
if (apiKey) { | ||
authorization = 'Basic ' + new Buffer( | ||
requestor + ':' + apiKey | ||
).toString('base64'); | ||
} else if (accessToken) { | ||
authorization = `Bearer ${accessToken}`; | ||
} | ||
|
||
return fetch(`${getSetting('PLOTLY_URL')}/datacache`, { | ||
|
@@ -85,13 +81,7 @@ export function newDatacache(payloadJSON, type, requestor) { | |
} | ||
|
||
export function updateGrid(rows, fid, uids, requestor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fid or filename? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plotly's API uses |
||
const username = requestor; | ||
const users = getSetting('USERS'); | ||
const user = users.find( | ||
u => u.username === username | ||
); | ||
const apiKey = user.apiKey; | ||
const accessToken = user.accessToken; | ||
const {username, apiKey, accessToken} = getCredentials(requestor); | ||
|
||
// TODO - Test case where no rows are returned. | ||
if (uids.length !== rows[0].length) { | ||
|
@@ -129,16 +119,12 @@ export function updateGrid(rows, fid, uids, requestor) { | |
|
||
// Resolve if the requestor has permission to update fid, reject otherwise | ||
export function checkWritePermissions(fid, requestor) { | ||
const owner = fid.split(':')[0]; | ||
const user = getSetting('USERS').find( | ||
u => u.username === requestor | ||
); | ||
const {username, apiKey, accessToken} = getCredentials(requestor); | ||
|
||
// Check if the user even exists | ||
if (!user || !(user.apiKey || user.accessToken)) { | ||
if (!username || !(apiKey || accessToken)) { | ||
/* | ||
* Heads up - the front end looks for "Unauthenticated" in this | ||
* error message. So don't change it! | ||
* Warning: The front end looks for "Unauthenticated" in this error message. Don't change it! | ||
*/ | ||
const errorMessage = ( | ||
`Unauthenticated: Attempting to update grid ${fid} but the ` + | ||
|
@@ -147,7 +133,7 @@ export function checkWritePermissions(fid, requestor) { | |
Logger.log(errorMessage, 0); | ||
throw new Error(errorMessage); | ||
} | ||
const {apiKey, accessToken} = user; | ||
|
||
return PlotlyAPIRequest(`grids/${fid}`, { | ||
username: requestor, | ||
apiKey, | ||
|
@@ -166,14 +152,17 @@ export function checkWritePermissions(fid, requestor) { | |
return res.json(); | ||
} | ||
}).then(function(filemeta) { | ||
if (filemeta.collaborators && | ||
const owner = fid.split(':')[0]; | ||
|
||
if (owner === requestor) { | ||
return Promise.resolve(); | ||
} else if (filemeta.collaborators && | ||
filemeta.collaborators.results && | ||
Boolean(filemeta.collaborators.results.find(collab => requestor === collab.username)) | ||
filemeta.collaborators.results.find(collab => requestor === collab.username) | ||
) { | ||
return new Promise(function(resolve) {resolve();}); | ||
} else if (owner === requestor) { | ||
return new Promise(function(resolve) {resolve();}); | ||
return Promise.resolve(); | ||
} | ||
|
||
throw new Error('Permission denied'); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
|
||
import {concat, contains, has} from 'ramda'; | ||
import YAML from 'yamljs'; | ||
|
||
import {createStoragePath} from './utils/homeFiles'; | ||
import path from 'path'; | ||
import os from 'os'; | ||
|
||
const DEFAULT_SETTINGS = { | ||
HEADLESS: false, | ||
|
@@ -226,3 +228,8 @@ export function saveSetting(settingName, settingValue) { | |
*/ | ||
fs.writeFileSync(getSetting('SETTINGS_PATH'), YAML.stringify(settingsOnFile)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should put an issue on this. If you remember when we tried to pump out the Athena client on this line it would cause an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I sneaked a fix for this in the PR that implemented the CSV connector. See f3df19f . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Thanks |
||
} | ||
|
||
// Get user credentials | ||
export function getCredentials(username) { | ||
return getSetting('USERS').find(u => u.username === username) || {}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Not necessarily in this PR) but we should small-case these filenames as well.. !!
Queries
,Datastore
etc, and remove.js
extensions to be consistent.. !!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to keep the extensions, because:
code-editor.css
andcode-editor.jsx
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is to move to lower case, whenever we have a chance (instead of having a huge big PR). We don't really want to spend time on it.