Skip to content

Commit

Permalink
Remove unauth GitHub api (#1221)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chunweii authored Oct 26, 2023
1 parent e8cab15 commit 7ebb845
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/app/core/services/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,29 @@ export class GithubService {
);
}

private fetchSettingsFromRawUrl(): Observable<SessionData> {
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<SessionData> representing session information.
*/
fetchSettingsFile(): Observable<SessionData> {
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();
})
);
}

Expand Down

0 comments on commit 7ebb845

Please sign in to comment.