Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
starting wip
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Mar 28, 2017
1 parent 000e865 commit f6553b1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -31,3 +31,5 @@ node_modules

# Optional REPL history
.node_repl_history

tmp
32 changes: 21 additions & 11 deletions lib/Adaptor.js
Expand Up @@ -8,7 +8,7 @@ exports.lastReferenceValue = exports.dataValue = exports.dataPath = exports.merg
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.execute = execute;
exports.postData = postData;
exports.addRow = addRow;

var _languageCommon = require('language-common');

Expand Down Expand Up @@ -115,7 +115,7 @@ function execute() {
* @param {object} params - data to write to the row
* @returns {Operation}
*/
function postData(params) {
function addRow(params) {

return function (state) {

Expand All @@ -129,19 +129,25 @@ function postData(params) {
}

var _expandReferences = (0, _languageCommon.expandReferences)(params)(state),
url = _expandReferences.url,
body = _expandReferences.body,
headers = _expandReferences.headers;
spreadsheet_id = _expandReferences.spreadsheet_id,
values = _expandReferences.values;

var url = 'https://sheets.googleapis.com/v4/spreadsheets/' + spreadsheet_id + '/values/range:append?valueInputOption=valueInputOption';

return new Promise(function (resolve, reject) {
console.log("Request body:");
console.log("\n" + JSON.stringify(body, null, 4) + "\n");
console.log("\n" + JSON.stringify(values, null, 4) + "\n");
_request2.default.post({
url: url,
json: body,
headers: headers
'url': url,
'json': values,
'auth': {
'bearer': state.idToken
}
}, function (error, response, body) {
error = assembleError({ error: error, response: response });
error = assembleError({
error: error,
response: response
});
if (error) {
reject(error);
console.log(response);
Expand All @@ -153,7 +159,11 @@ function postData(params) {
}
});
}).then(function (data) {
var nextState = _extends({}, state, { response: { body: data } });
var nextState = _extends({}, state, {
response: {
body: data
}
});
return nextState;
});
};
Expand Down
70 changes: 51 additions & 19 deletions src/Adaptor.js
@@ -1,6 +1,11 @@
import { execute as commonExecute, expandReferences } from 'language-common';
import {
execute as commonExecute,
expandReferences
} from 'language-common';
import request from 'request';
import { resolve as resolveUrl } from 'url';
import {
resolve as resolveUrl
} from 'url';

/** @module Adaptor */

Expand All @@ -23,7 +28,9 @@ export function execute(...operations) {
}

return state => {
return commonExecute(...operations)({ ...initialState, ...state })
return commonExecute(...operations)({ ...initialState,
...state
})
};

}
Expand All @@ -39,28 +46,41 @@ export function execute(...operations) {
* @param {object} params - data to write to the row
* @returns {Operation}
*/
export function postData(params) {
export function addRow(params) {

return state => {

function assembleError({ response, error }) {
if (response && ([200,201,202].indexOf(response.statusCode) > -1)) return false;
function assembleError({
response,
error
}) {
if (response && ([200, 201, 202].indexOf(response.statusCode) > -1)) return false;
if (error) return error;
return new Error(`Server responded with ${response.statusCode}`)
}

const { url, body, headers } = expandReferences(params)(state);
const {
spreadsheet_id,
values
} = expandReferences(params)(state);

const url = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheet_id}/values/range:append?valueInputOption=valueInputOption`

return new Promise((resolve, reject) => {
console.log("Request body:");
console.log("\n" + JSON.stringify(body, null, 4) + "\n");
request.post ({
url: url,
json: body,
headers
}, function(error, response, body){
error = assembleError({error, response})
if(error) {
console.log("\n" + JSON.stringify(values, null, 4) + "\n");
request.post({
'url': url,
'json': values,
'auth': {
'bearer': state.idToken
}
}, function(error, response, body) {
error = assembleError({
error,
response
})
if (error) {
reject(error);
console.log(response);
} else {
Expand All @@ -71,7 +91,11 @@ export function postData(params) {
}
})
}).then((data) => {
const nextState = { ...state, response: { body: data } };
const nextState = { ...state,
response: {
body: data
}
};
return nextState;
})

Expand All @@ -80,6 +104,14 @@ export function postData(params) {
}

export {
field, fields, sourceValue, alterState, each,
merge, dataPath, dataValue, lastReferenceValue
} from 'language-common';
field,
fields,
sourceValue,
alterState,
each,
merge,
dataPath,
dataValue,
lastReferenceValue
}
from 'language-common';

0 comments on commit f6553b1

Please sign in to comment.