Skip to content

Refactored all HTTP requests to use axios #1763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 19, 2021
Merged
24 changes: 9 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@
"redux-devtools-dock-monitor": "^1.1.3",
"redux-devtools-log-monitor": "^1.4.0",
"redux-thunk": "^2.3.0",
"request": "^2.88.2",
"request-promise": "^4.2.5",
"reselect": "^4.0.0",
"s3-policy-v4": "0.0.3",
"sass-extract": "^2.1.0",
Expand Down
78 changes: 39 additions & 39 deletions server/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import format from 'date-fns/format';
import isUrl from 'is-url';
import jsdom, { serializeDocument } from 'jsdom';
import isAfter from 'date-fns/isAfter';
import request from 'request';
import axios from 'axios';
import slugify from 'slugify';
import Project from '../models/project';
import User from '../models/user';
Expand Down Expand Up @@ -125,7 +125,7 @@ export function getProjectsForUserId(userId) {
export function getProjectAsset(req, res) {
Project.findById(req.params.project_id)
.populate('user', 'username')
.exec((err, project) => { // eslint-disable-line
.exec(async (err, project) => { // eslint-disable-line
if (err) {
return res
.status(404)
Expand All @@ -145,15 +145,15 @@ export function getProjectAsset(req, res) {
if (!resolvedFile.url) {
return res.send(resolvedFile.content);
}
request(
{ method: 'GET', url: resolvedFile.url, encoding: null },
(innerErr, response, body) => {
if (innerErr) {
return res.status(404).send({ message: 'Asset does not exist' });
}
return res.send(body);
}
);

try {
const { data } = await axios.get(resolvedFile.url, {
responseType: 'arraybuffer'
});
res.send(data);
} catch (error) {
res.status(404).send({ message: 'Asset does not exist' });
}
});
}

Expand Down Expand Up @@ -198,7 +198,7 @@ function bundleExternalLibs(project, zip, callback) {
let numScriptsResolved = 0;
let numScriptTags = 0;

function resolveScriptTagSrc(scriptTag, document) {
async function resolveScriptTagSrc(scriptTag, document) {
const path = scriptTag.src.split('/');
const filename = path[path.length - 1];
const { src } = scriptTag;
Expand All @@ -212,23 +212,21 @@ function bundleExternalLibs(project, zip, callback) {
return;
}

request(
{ method: 'GET', url: src, encoding: null },
(err, response, body) => {
if (err) {
console.log(err);
} else {
zip.append(body, { name: filename });
scriptTag.src = filename;
}
try {
const { data } = await axios.get(src, {
responseType: 'arraybuffer'
});
zip.append(data, { name: filename });
scriptTag.src = filename;
} catch (err) {
console.log(err);
}

numScriptsResolved += 1;
if (numScriptsResolved === numScriptTags) {
indexHtml.content = serializeDocument(document);
callback();
}
}
);
numScriptsResolved += 1;
if (numScriptsResolved === numScriptTags) {
indexHtml.content = serializeDocument(document);
callback();
}
}

jsdom.env(indexHtml.content, (innerErr, window) => {
Expand Down Expand Up @@ -264,7 +262,7 @@ function buildZip(project, req, res) {
);
zip.pipe(res);

function addFileToZip(file, path) {
async function addFileToZip(file, path) {
if (file.fileType === 'folder') {
const newPath = file.name === 'root' ? path : `${path}${file.name}/`;
file.children.forEach((fileId) => {
Expand All @@ -274,16 +272,18 @@ function buildZip(project, req, res) {
})();
});
} else if (file.url) {
request(
{ method: 'GET', url: file.url, encoding: null },
(err, response, body) => {
zip.append(body, { name: `${path}${file.name}` });
numCompletedFiles += 1;
if (numCompletedFiles === numFiles) {
zip.finalize();
}
}
);
try {
const { data } = await axios.get(file.url, {
responseType: 'arraybuffer'
});
zip.append(data, { name: `${path}${file.name}` });
} catch (err) {
console.log(err);
}
numCompletedFiles += 1;
if (numCompletedFiles === numFiles) {
zip.finalize();
}
} else {
zip.append(file.content, { name: `${path}${file.name}` });
numCompletedFiles += 1;
Expand Down
96 changes: 46 additions & 50 deletions server/scripts/examples-gg-latest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import rp from 'request-promise';
import axios from 'axios';
import Q from 'q';
import mongoose from 'mongoose';
import objectID from 'bson-objectid';
Expand Down Expand Up @@ -106,7 +106,7 @@ const insert = function insert(_mainString, _insString, _pos) {
/* --- data processing --- */
// 1. first get the top level directories P and M
// https://api.github.com/repos/generative-design/Code-Package-p5.js/contents?ref=pre-release
function getCodePackage() {
async function getCodePackage() {
const sketchRootList = [];
const options = {
// url: 'https://api.github.com/repos/generative-design/Code-Package-p5.js/contents',
Expand All @@ -118,34 +118,31 @@ function getCodePackage() {
Authorization: `Basic ${Buffer.from(
`${clientId}:${clientSecret}`
).toString('base64')}`
},
json: true
}
};

return rp(options)
.then((res) => {
res.forEach((metadata) => {
if (
metadata.name.endsWith('P') === true ||
metadata.name.endsWith('M') === true
) {
sketchRootList.push(metadata);
}
});

return sketchRootList;
})
.catch((err) => {
throw err;
try {
const { data } = await axios.request(options);
data.forEach((metadata) => {
if (
metadata.name.endsWith('P') === true ||
metadata.name.endsWith('M') === true
) {
sketchRootList.push(metadata);
}
});
return sketchRootList;
} catch (err) {
throw err;
}
}

// 2. get the list of all the top-level sketch directories in P and M
function getSketchDirectories(sketchRootList) {
// console.log(sketchRootList);

return Q.all(
sketchRootList.map((sketches) => {
sketchRootList.map(async (sketches) => {
// console.log(sketches)
const options = {
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`,
Expand All @@ -155,19 +152,16 @@ function getSketchDirectories(sketchRootList) {
Authorization: `Basic ${Buffer.from(
`${clientId}:${clientSecret}`
).toString('base64')}`
},
json: true
}
};

return rp(options)
.then((res) => {
const sketchDirs = flatten(res);

return sketchDirs;
})
.catch((err) => {
throw err;
});
try {
const { data } = await axios.request(options);
const sketchDirs = flatten(data);
return sketchDirs;
} catch (err) {
throw err;
}
})
).then((output) => {
const sketchList = [];
Expand All @@ -186,7 +180,7 @@ function getSketchDirectories(sketchRootList) {
// 3. For each sketch item in the sketchList, append the tree contents to each item
function appendSketchItemLinks(sketchList) {
return Q.all(
sketchList.map((sketches) => {
sketchList.map(async (sketches) => {
const options = {
// url: `${sketches.url}?client_id=${clientId}&client_secret=${clientSecret}`,
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`,
Expand All @@ -196,15 +190,16 @@ function appendSketchItemLinks(sketchList) {
Authorization: `Basic ${Buffer.from(
`${clientId}:${clientSecret}`
).toString('base64')}`
},
json: true
}
};

return rp(options).then((res) => {
sketches.tree = res;

try {
const { data } = await axios.request(options);
sketches.tree = data;
return sketchList;
});
} catch (err) {
throw err;
}
})
);
}
Expand All @@ -214,24 +209,24 @@ function getSketchItems(sketchList) {
// const completeSketchPkg = [];

/* eslint-disable */
return Q.all(sketchList[0].map(sketch => Q.all(sketch.tree.map((item) => {
return Q.all(sketchList[0].map(async sketch => Q.all(sketch.tree.map((item) => {
if (item.name === 'data') {
const options = {
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${item.path}${branchRef}`,
method: 'GET',
headers: {
...headers,
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
},
json: true
}
};

return rp(options).then((res) => {
sketch.data = res;
try {
const { data } = axios.request(options);
sketch.data = data;
return sketch;
}).catch((err) => {
} catch (err) {
throw err;
});
}
}
// pass
})))).then(() => sketchList[0]);
Expand Down Expand Up @@ -399,7 +394,7 @@ function formatAllSketches(sketchList) {
// get all the sketch data content and download to the newProjects array
function getAllSketchContent(newProjectList) {
/* eslint-disable */
return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map((sketchFile, i) => {
return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map(async (sketchFile, i) => {
/*
sketchFile.name.endsWith(".mp4") !== true &&
sketchFile.name.endsWith(".ogg") !== true &&
Expand Down Expand Up @@ -427,12 +422,13 @@ function getAllSketchContent(newProjectList) {
};

// console.log("CONVERT ME!")
return rp(options).then((res) => {
newProject.files[i].content = res;
try {
const { data } = await axios.request(options);
newProject.files[i].content = data;
return newProject;
}).catch((err) => {
} catch (err) {
throw err;
});
}
}
if (newProject.files[i].url) {
return new Promise((resolve, reject) => {
Expand Down
Loading