From 7ebb84526b60f96af4864ac388b8167b8a3d67c9 Mon Sep 17 00:00:00 2001 From: Lee Chun Wei <47494777+chunweii@users.noreply.github.com> Date: Thu, 26 Oct 2023 18:28:06 +0800 Subject: [PATCH] Remove unauth GitHub api (#1221) Use rest api first, then raw githubusercontent REST API should be more reliable, but it has a rate limit for unauthenticated requests. Let's use REST API call for the fetching of settings.json, with a failsafe being raw.githubusercontent.com. --- src/app/core/services/github.service.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/app/core/services/github.service.ts b/src/app/core/services/github.service.ts index adfc74aa3..0e954e113 100644 --- a/src/app/core/services/github.service.ts +++ b/src/app/core/services/github.service.ts @@ -396,14 +396,29 @@ export class GithubService { ); } + private fetchSettingsFromRawUrl(): Observable { + return from(fetch(getSettingsUrl(MOD_ORG, DATA_REPO))).pipe( + mergeMap((res) => res.json()), + catchError((err) => throwError('Failed to fetch settings file.')) + ); + } + /** * Fetches the data file that is regulates session information. * @return Observable representing session information. */ fetchSettingsFile(): Observable { - return from(fetch(getSettingsUrl(MOD_ORG, DATA_REPO))).pipe( - mergeMap((res) => res.json()), - catchError((err) => throwError('Failed to fetch settings file.')) + return from( + octokit.repos.getContents({ owner: MOD_ORG, repo: DATA_REPO, path: 'settings.json', headers: GithubService.IF_NONE_MATCH_EMPTY }) + ).pipe( + map((rawData) => JSON.parse(atob(rawData['data']['content']))), + catchError((err) => { + this.logger.error( + 'GithubService: Failed to fetch settings file via REST API. Trying to fetch using raw.githubusercontent.com: ', + err + ); + return this.fetchSettingsFromRawUrl(); + }) ); }