From c6be51f15fac458fc6821c90246913c1af4026de Mon Sep 17 00:00:00 2001 From: jrtechs Date: Thu, 26 Dec 2019 21:36:27 -0500 Subject: [PATCH] Added route in backend to fetch all friends(followers and following). --- routes/api.js | 150 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 128 insertions(+), 22 deletions(-) diff --git a/routes/api.js b/routes/api.js index 6fba983..7b3d667 100644 --- a/routes/api.js +++ b/routes/api.js @@ -6,50 +6,155 @@ const GITHUB_API = "https://api.github.com"; const authenticate = `client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}`; +function queryGithubAPIRaw(requestURL) +{ + return new Promise((resolve, reject)=> + { + var queryURL; + if(requestURL.includes("?page=")) + { + queryURL = GITHUB_API + requestURL + "&" + authenticate; + } + else + { + queryURL = GITHUB_API + requestURL + "?" + authenticate; + } + console.log(queryURL); + + got(queryURL, { json: true }).then(response => + { + resolve(response.body); + cache.put(requestURL, response.body); + }).catch(error => + { + resolve(error); + cache.put(requestURL, error); + }); + }); +} + function queryGitHubAPI(requestURL) { const apiData = cache.get(requestURL); - return new Promise(function(reject, resolve) + return new Promise(function(resolve, reject) { if(apiData == null) { - var queryURL; - if(requestURL.includes("?page=")) + queryGithubAPIRaw(requestURL).then((dd)=> + { + resolve(dd); + }).catch((err)=> { - queryURL = GITHUB_API + requestURL + "&" + authenticate; + resolve(err); + }) + } + else + { + console.log("Fetched From Cache"); + resolve(apiData); + } + }) +} + + + +const API_FOLLOWING = "/following"; +const API_FOLLOWERS = "/followers"; +const API_USER_PATH = "/users/"; + +function fetchAllUsers(username, apiPath, page, lst) +{ + return new Promise((resolve, reject)=> + { + queryGitHubAPI(API_USER_PATH + username + apiPath + "?page=" + page).then((data)=> + { + if(data.hasOwnProperty("length")) + { + lst = lst.concat(data) + if(page < 5 && data.length === 30) + { + fetchAllUsers(username, apiPath, page + 1, lst).then((l)=> + { + resolve(l); + }); + } + else + { + resolve(lst); + } } else { - queryURL = GITHUB_API + requestURL + "?" + authenticate; + reject("Malformed data"); } - console.log(queryURL); + }).catch((err)=> + { + reject("error with api request"); + }); + }, + (error)=> + { + if(error.hasOwnProperty("length")) + { + lst.concat(data); + resolve(lst); + } + }); +} - got(queryURL, { json: true }).then(response => +function queryFriends(user) +{ + return new Promise((resolve, reject)=> + { + fetchAllUsers(user, API_FOLLOWERS, 1, []).then((followers)=> + { + fetchAllUsers(user, API_FOLLOWING, 1, []).then((following)=> { - resolve(response.body); - cache.put(requestURL, response.body); - }).catch(error => + resolve(following.concat(followers)); + }).catch((err)=> { - resolve(error); - cache.put(requestURL, error); - }); - } - else + console.log(err); + }) + }).catch((error)=> { - console.log("Fetched From Cahce"); - resolve(apiData); - } - }) + console.log(error); + }) + }); } +routes.get("/friends", (request, result)=> +{ + if(request.query.hasOwnProperty("name")) + { + result.setHeader('Content-Type', 'application/json'); + queryFriends(request.query.name).then(friends=> + { + result.write(JSON.stringify(friends)); + result.end(); + }).catch(error=> + { + result.status(500); + result.write("API error fetching friends.") + result.end(); + }); + } + else + { + result.status(400); + result.write("Must provide the name get parameter"); + result.end(); + } +}) + + routes.get('/*', (request, result) => { var gitHubAPIURL = request.url; result.setHeader('Content-Type', 'application/json'); - queryGitHubAPI(gitHubAPIURL).then(function(data) + queryGitHubAPI(gitHubAPIURL).then((data)=> { if(data.hasOwnProperty("id") || data[0].hasOwnProperty("id")) { @@ -60,7 +165,7 @@ routes.get('/*', (request, result) => result.write("[]"); } result.end(); - }).catch(function(error) + }).catch((error)=> { try { @@ -74,7 +179,8 @@ routes.get('/*', (request, result) => } } - catch(error) { + catch(error) + { result.write("[]"); }; result.end();