Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 18, 2021
1 parent 233faed commit 40e7371
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 61 deletions.
78 changes: 46 additions & 32 deletions taskRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,78 @@ require('dotenv').config();
/**
* This file will be responsible for managing the watcher
* as well as ensuring the credentials are avaiable
* */
*/

const projectID = "a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng";
const projectID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng';

var credentials = null;
var initInterval = null;
var updateInterval = null;
let credentials = null;
let initInterval = null;
let updateInterval = null;

/**
* Sends GET request to get token from auth server
*/
const fetchToken = () => {
console.log(
"\x1b[34mtaskRunner.js::fetchToken:",
"\x1b[0mAttempting to fetch token");
'\x1b[96mtaskRunner.js::fetchToken:',
'\x1b[0mAttempting to fetch token'
);
axios({
method: 'GET',
url: 'http://localhost:3000/credentials'
})
.then(res => {
.then(res => {
// console.log(res.data);
if (Object.keys(res.data).length == 5) {
credentials = res.data;
clearInterval(initInterval);
console.log(
"\x1b[32mtaskRunner.js::fetchToken:",
"\x1b[0mCredentials received");
} else {
if (Object.keys(res.data).length == 5) {
credentials = res.data;
clearInterval(initInterval);
console.log(
'\x1b[92mtaskRunner.js::fetchToken:',
'\x1b[0mCredentials received'
);
} else {
console.log(
'\x1b[96mtaskRunner.js::fetchToken:',
'\x1b[0mAuthenticate through localhost:3000/auth'
);
}
})
.catch(err => {
console.log(
"\x1b[34mtaskRunner.js::fetchToken:",
"\x1b[0mAuthenticate through localhost:3000/auth")
}
})
.catch(err => {
console.log(
"\x1b[31mtaskRunner.js::fetchToken:",
"\x1b[0mError accessing localhost:3000/credentials"
);
});
}
'\x1b[91mtaskRunner.js::fetchToken:',
'\x1b[0mError accessing localhost:3000/credentials'
);
});
};

/**
* Manages intervals involving getting token from auth server
*/
const getCredentials = () => {
fetchToken();
initInterval = setInterval(() => {
fetchToken();
console.log(
"\x1b[33mtaskRunner.js::getCredentials:",
"\x1b[0mWaiting for initial auth")
'\x1b[93mtaskRunner.js::getCredentials:',
'\x1b[0mWaiting for initial auth'
);
}, 10000);
updateInterval = setInterval(fetchToken, 3300 * 1000);
}
};

/**
* Function to dispatch the watcher
*/
const dispatchWatcher = () => {
if (credentials == null) {
console.log(
"\x1b[33mtaskRunner.js::dispatchWatcher:",
"\x1b[0mNo credentials");
'\x1b[93mtaskRunner.js::dispatchWatcher:',
'\x1b[0mNo credentials'
);
} else {
watcher.watch(projectID, 'urn:adsk.wipprod:fs.folder:co.ltuYad4ZTtieJLm6j3MGuA', credentials, 5);
}
}
};

getCredentials();
setInterval(dispatchWatcher, 5000);
60 changes: 31 additions & 29 deletions watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,48 @@ const axios = require('axios');
* if an interval is not passed, watcher will loop through all files in the folder
*/
exports.watch = (projectID, folderID, credentials, interval) => {

var timeFilter = {};

let timeFilter = {};

if (interval) {
var dateNow = new Date();
var dateXSecondsAgo = new Date(dateNow.getTime() - interval * 1000);
timeFilter = {'filter[lastModifiedTime]-ge': dateXSecondsAgo.toISOString()}
timeFilter = { 'filter[lastModifiedTime]-ge': dateXSecondsAgo.toISOString() };
}

axios({
method: 'GET',
url: `https://developer.api.autodesk.com//data/v1/projects/${projectID}/folders/${folderID}/contents`,
headers: {
'Authorization': `Bearer ${credentials.access_token}`
Authorization: `Bearer ${credentials.access_token}`
},
params: timeFilter,
})
.then(res => {
return res.data.included;
params: timeFilter
})
.then(data => {
if (data === undefined) { // no new files changed, return
console.log(
"\x1b[34mwatcher.js::watch:",
"\x1b[0mNo new files modified since " + dateXSecondsAgo.toISOString());
return;
}
.then(res => {
return res.data.included;
})
.then(data => {
if (data === undefined) { // no new files changed, return
console.log(
'\x1b[96mwatcher.js::watch:',
'\x1b[0mNo new files modified since ' + dateXSecondsAgo.toISOString());
return;
}

for (var index in data) {
// console.log(data[index]);
var displayName = data[index].attributes.displayName;
var lastModifiedTime = data[index].attributes.lastModifiedTime;
console.log("\x1b[34mwatcher.js::watch: \x1b[0m" + displayName + " was modified at " + lastModifiedTime);
for (var index in data) {
// console.log(data[index]);
const displayName = data[index].attributes.displayName;
const lastModifiedTime = data[index].attributes.lastModifiedTime;
console.log('\x1b[92mwatcher.js::watch: \x1b[0m' + displayName + ' was modified at ' + lastModifiedTime);

// TODO: if item is not in db, add to db and then download
// TODO: if item is not in db, add to db and then download

// TODO: if item is in db, check and compare lastModifiedTime
}
})
.catch(err => {
console.log(err);
});
}
// TODO: if item is in db, check and compare lastModifiedTime
}
})
.catch(err => {
console.log(
'\x1b[91mwatcher.js::watch:',
'\x1b[0mError accessing Forge folder contents API'
);
});
};

0 comments on commit 40e7371

Please sign in to comment.