From 031a2347706ddaa1764c91146b1891549a772656 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 28 Dec 2019 13:20:00 -0500 Subject: [PATCH] Created new route for fetching all user repositories and minimizes/cahces the json results (closes #26) --- public/js/githubAPI.js | 8 +++++ public/js/profileTimeLine.js | 3 +- routes/api.js | 68 +++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/public/js/githubAPI.js b/public/js/githubAPI.js index beb8030..ea12950 100644 --- a/public/js/githubAPI.js +++ b/public/js/githubAPI.js @@ -67,6 +67,14 @@ function getFriendsAPI(userName, suc, err) } +function getUserRepositories(userName, suc, err) +{ + //ex: http://localhost:7000/api/repositories/jwflory + const urlpath = APIROOT + "/repositories/" + userName; + runAjax(urlpath, suc, err); +} + + /** * Queries github API end points with the backend * proxy server for github graphs. diff --git a/public/js/profileTimeLine.js b/public/js/profileTimeLine.js index e5dfcb3..96405ec 100644 --- a/public/js/profileTimeLine.js +++ b/public/js/profileTimeLine.js @@ -15,7 +15,7 @@ function addRepositories(userName, groupID) { return new Promise(function(resolve, reject) { - queryAPIByUser(API_REPOSITORIES, userName, + getUserRepositories(userName, function(data) { repositoryData = data; @@ -76,7 +76,6 @@ function createProfileTimeLine(username, containerName) { var container = document.getElementById(containerName); - var prom = [addRepositories(username, 1)]; var groups = new vis.DataSet([ diff --git a/routes/api.js b/routes/api.js index 393ea85..3ec93af 100644 --- a/routes/api.js +++ b/routes/api.js @@ -190,10 +190,12 @@ function queryFriends(user) }).catch((err)=> { console.log(err); + reject("API ERROR"); }) }).catch((error)=> { console.log(error); + resolve("API Error"); }) } else @@ -217,7 +219,71 @@ routes.get("/friends/:name", (request, result)=> .json({error: 'API error fetching friends'}) .end(); }); -}) +}); + + +function copyWithProperties(props, obj) +{ + var newO = new Object(); + for(var i =0; i < props.length; i++) + { + newO[props[i]] = obj[props[i]]; + } + return newO; +} + +function minimizeRepositories(repositories) +{ + var rList = []; + + for(var i = 0; i < repositories.length; i++) + { + rList.push(copyWithProperties(["name", "created_at", "homepage", + "description", "language", "forks", "watchers", + "open_issues_count", "license"], + repositories[i])); + } + return rList; +} + +const REPOS_PATH = "/repos"; + +function queryRepositories(user) +{ + const cacheHit = cache.get(user + REPOS_PATH); + return new Promise((resolve, reject)=> + { + if(cacheHit == null) + { + fetchAllUsers(user, REPOS_PATH, 1, []).then((repos)=> + { + var minimized = minimizeRepositories(repos); + resolve(minimized); + cache.put(user + REPOS_PATH, minimized); + }); + } + else + { + console.log("Repositories cache hit"); + resolve(cacheHit); + } + }); +} + + +routes.get("/repositories/:name", (request, result)=> +{ + queryRepositories(request.params.name).then(repos=> + { + result.json(repos) + .end(); + }).catch(error=> + { + result.status(500) + .json({error: 'API error fetching friends'}) + .end(); + }); +}); routes.get('/*', (request, result) =>