Skip to content

Instantly share code, notes, and snippets.

@mkhoeini
Forked from HosseinNikpour/request
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhoeini/51efa949029be35868b3 to your computer and use it in GitHub Desktop.
Save mkhoeini/51efa949029be35868b3 to your computer and use it in GitHub Desktop.
import fetch from 'node-fetch';
import btoa from 'btoa';
function serverCallMaker(user, pass, baseUrl) {
const hash = btoa(`${user}:${pass}`);
const Authorization = `Basic ${hash}`;
const Accept = 'application/json; odata=verbose';
const params = {
method: 'get',
headers: {Authorization, Accept}
};
return (url) => fetch(baseUrl + url, params).then(r => r.json());
}
const USERNAME = 'nikpour';
const PASSWORD = '123QWE!@#';
const baseUrl = 'http://78.111.2.149:8030/nikpour/_api/Web';
export const getData = serverCallMaker(USERNAME, PASSWORD, baseUrl);
const TaskListURL = "/Lists(guid'E37AAEB7-AAAD-422A-9566-15F3DC07CC94')/items";
const ProjectListURL = "/Lists(guid'DEBF6BEF-A3CF-4272-80CC-46287AE98E32')/items";
export const getAllTasks = getData.bind(null, TaskListURL);
export const getAllProjects = getData.bind(null, ProjectListURL);
import {getAllTasks, getAllProjects} from './request.js';
const log = console::console.log;
const err = console::console.error;
async function test() {
try {
const tasks = await getAllTasks();
log(tasks.d.results);
const projs = await getAllProjects();
log(projs.d.results);
}
catch(e) {
err(e.stack);
}
}
test();
@mkhoeini
Copy link
Author

Fun with ES2015

First do:

$ npm i node-fetch btoa babel

Then you can run it like this:

$ ./node_modules/.bin/babel-node --stage 0 ./test.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment