From 8aac54d458e43c4528e345a6ebd9b6e0609b68fd Mon Sep 17 00:00:00 2001 From: jrtechs Date: Fri, 27 Dec 2019 13:49:27 -0500 Subject: [PATCH] Improved pagination by setting return size to 100 rather than default 20. Improved friend api structure. --- routes/api.js | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/routes/api.js b/routes/api.js index 7b3d667..c7c7158 100644 --- a/routes/api.js +++ b/routes/api.js @@ -62,17 +62,20 @@ function queryGitHubAPI(requestURL) const API_FOLLOWING = "/following"; const API_FOLLOWERS = "/followers"; const API_USER_PATH = "/users/"; +const API_PAGINATION_SIZE = 100; +const API_MAX_PAGES = 3; +const API_PAGINATION = "&per_page=" + API_PAGINATION_SIZE; function fetchAllUsers(username, apiPath, page, lst) { return new Promise((resolve, reject)=> { - queryGitHubAPI(API_USER_PATH + username + apiPath + "?page=" + page).then((data)=> + queryGitHubAPI(API_USER_PATH + username + apiPath + "?page=" + page + API_PAGINATION).then((data)=> { if(data.hasOwnProperty("length")) { lst = lst.concat(data) - if(page < 5 && data.length === 30) + if(page < API_MAX_PAGES && data.length === API_PAGINATION_SIZE) { fetchAllUsers(username, apiPath, page + 1, lst).then((l)=> { @@ -124,28 +127,18 @@ function queryFriends(user) } -routes.get("/friends", (request, result)=> +routes.get("/friends/:name", (request, result)=> { - if(request.query.hasOwnProperty("name")) + queryFriends(request.params.name).then(friends=> { - 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.json(friends) + .end(); + }).catch(error=> { - result.status(400); - result.write("Must provide the name get parameter"); - result.end(); - } + result.status(500) + .json({error: 'API error fetching friends'}) + .end(); + }); })