Skip to content

Commit

Permalink
Remove API access token and implement local cache for Github API requ…
Browse files Browse the repository at this point in the history
…ests to get around their rate limit
  • Loading branch information
adamjarling committed Apr 29, 2024
1 parent d0a50be commit 40df7d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
1 change: 0 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 24 additions & 17 deletions lib/github.js
Original file line number Diff line number Diff line change
@@ -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/";

Expand All @@ -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) => {
Expand Down

0 comments on commit 40df7d2

Please sign in to comment.