Skip to content

Commit 6c6b54e

Browse files
committed
[#975] Updated examples-ml5.js
1 parent 5801586 commit 6c6b54e

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

server/scripts/examples-ml5.js

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import rp from 'request-promise';
2+
import axios from 'axios';
33
import Q from 'q';
44
import { ok } from 'assert';
55

@@ -31,8 +31,7 @@ const githubRequestOptions = {
3131
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString(
3232
'base64'
3333
)}`
34-
},
35-
json: true
34+
}
3635
};
3736

3837
const editorRequestOptions = {
@@ -43,8 +42,7 @@ const editorRequestOptions = {
4342
Authorization: `Basic ${Buffer.from(
4443
`${editorUsername}:${personalAccessToken}`
4544
).toString('base64')}`
46-
},
47-
json: true
45+
}
4846
};
4947

5048
/**
@@ -78,7 +76,12 @@ async function fetchFileContent(item) {
7876
options.url !== null ||
7977
options.url !== ''
8078
) {
81-
file.content = await rp(options);
79+
try {
80+
const { data } = await axios.request(options);
81+
file.content = data;
82+
} catch (err) {
83+
throw err;
84+
}
8285
// NOTE: remove the URL property if there's content
8386
// Otherwise the p5 editor will try to pull from that url
8487
if (file.content !== null) delete file.url;
@@ -105,9 +108,9 @@ async function getCategories() {
105108
try {
106109
const options = Object.assign({}, githubRequestOptions);
107110
options.url = `${options.url}/p5js${branchRef}`;
108-
const results = await rp(options);
111+
const { data } = await axios.request(options);
109112

110-
return results;
113+
return data;
111114
} catch (err) {
112115
return err;
113116
}
@@ -126,10 +129,10 @@ async function getCategoryExamples(sketchRootList) {
126129
const options = Object.assign({}, githubRequestOptions);
127130
options.url = `${options.url}${categories.path}${branchRef}`;
128131
// console.log(options)
129-
const sketchDirs = await rp(options);
130132

131133
try {
132-
const result = flatten(sketchDirs);
134+
const { data } = await axios.request(options);
135+
const result = flatten(data);
133136

134137
return result;
135138
} catch (err) {
@@ -162,7 +165,12 @@ async function traverseSketchTree(parentObject) {
162165
const options = Object.assign({}, githubRequestOptions);
163166
options.url = `${options.url}${parentObject.path}${branchRef}`;
164167

165-
output.tree = await rp(options);
168+
try {
169+
const { data } = await axios.request(options);
170+
output.tree = data;
171+
} catch (err) {
172+
throw err;
173+
}
166174

167175
output.tree = output.tree.map((file) => traverseSketchTree(file));
168176

@@ -274,9 +282,12 @@ async function getProjectsList() {
274282
const options = Object.assign({}, editorRequestOptions);
275283
options.url = `${options.url}/sketches`;
276284

277-
const results = await rp(options);
278-
279-
return results.sketches;
285+
try {
286+
const { data } = await axios.request(options);
287+
return data.sketches;
288+
} catch (err) {
289+
throw err;
290+
}
280291
}
281292

282293
/**
@@ -287,24 +298,26 @@ async function deleteProject(project) {
287298
options.method = 'DELETE';
288299
options.url = `${options.url}/sketches/${project.id}`;
289300

290-
const results = await rp(options);
291-
292-
return results;
301+
try {
302+
const { data } = await axios.request(options);
303+
return data;
304+
} catch (err) {
305+
throw err;
306+
}
293307
}
294308

295309
/**
296310
* Create a new project
297311
*/
298312
async function createProject(project) {
299-
try {
300-
const options = Object.assign({}, editorRequestOptions);
301-
options.method = 'POST';
302-
options.url = `${options.url}/sketches`;
303-
options.body = project;
304-
305-
const results = await rp(options);
313+
const options = Object.assign({}, editorRequestOptions);
314+
options.method = 'POST';
315+
options.url = `${options.url}/sketches`;
316+
options.body = project;
306317

307-
return results;
318+
try {
319+
const { data } = await axios.request(options);
320+
return data;
308321
} catch (err) {
309322
throw err;
310323
}

0 commit comments

Comments
 (0)