From 40df7d265b47a049b2afb3e0faeaff4e1692465b Mon Sep 17 00:00:00 2001 From: Adam Joseph Arling Date: Mon, 29 Apr 2024 11:08:52 -0500 Subject: [PATCH] Remove API access token and implement local cache for Github API requests to get around their rate limit --- .github/workflows/deploy.yml | 1 - lib/github.js | 41 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index fa892b053..59e1fbdd0 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -16,7 +16,6 @@ jobs: env: NEXT_PUBLIC_CONTENTFUL_SPACE_ID: ${{ secrets.NEXT_PUBLIC_CONTENTFUL_SPACE_ID }} NEXT_PUBLIC_CONTENTFUL_ACCESS_TOKEN: ${{ secrets.NEXT_PUBLIC_CONTENTFUL_ACCESS_TOKEN }} - NEXT_PUBLIC_GITHUB_PERSONAL_ACCESS_TOKEN: ${{ secrets.NEXT_PUBLIC_GITHUB_PERSONAL_ACCESS_TOKEN }} steps: - name: Get files diff --git a/lib/github.js b/lib/github.js index ea7487083..ae8d1539e 100644 --- a/lib/github.js +++ b/lib/github.js @@ -1,3 +1,24 @@ +// Create a cache to store the data we fetch from GitHub +// Github API has a rate limit of 60 requests per hour for unauthenticated users, so when developing locally, we can cache the data to avoid hitting the rate limit. +let cache = {}; + +async function fetchData(url) { + if (cache[url]) { + console.log("cached", cache[url]); + return cache[url]; + } + + const res = await fetch(url, { + headers: { + Accept: "application/vnd.github.v3+json", + // "User-Agent": "samvera/samvera.org", + }, + }); + const data = await res.json(); + cache[url] = data; + return data; +} + export async function getGithubRepoData(featuredApps) { const apiRootUrl = "https://api.github.com/repos/"; @@ -11,23 +32,9 @@ export async function getGithubRepoData(featuredApps) { try { // Fetch the data - const repoDataResponses = await Promise.all( - urls.map((url) => - fetch(url, { - headers: { - Accept: "application/vnd.github.v3+json", - // "User-Agent": "samvera/samvera.org", - Authorization: `token ${process.env.NEXT_PUBLIC_GITHUB_PERSONAL_ACCESS_TOKEN}`, - }, - }).then((res) => { - return res.json(); - }) - ) - ); - - const releaseResponses = await Promise.all( - releaseUrls.map((url) => fetch(url).then((res) => res.json())) - ); + const repoDataResponses = await Promise.all(urls.map(fetchData)); + + const releaseResponses = await Promise.all(releaseUrls.map(fetchData)); // Update the featuredApps with the data we fetched const updateFeaturedApps = featuredApps.map((featuredApp, index) => {