-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhanced fetching of Definitions via radar-self-enrolment-definitions #15
Closed
this-Aditya
wants to merge
12
commits into
RADAR-base:feat/initial-components
from
this-Aditya:fetch-definitions
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2a1ad16
Added configurations for github data fetching
this-Aditya 2791e0b
Added utility class to cache the data
this-Aditya 6b61852
Work on Github client and service
this-Aditya bf1fdd1
Modified github client and service to fetch the definition file data
this-Aditya 91ec90a
Modified data cache
this-Aditya b37925c
Added file for caching definitions file name url map
this-Aditya 112f3d3
Added structure for definition type
this-Aditya 1ad79be
Configured caches and github service
this-Aditya feb7867
Merge branch 'feat/initial-components' into fetch-definitions
this-Aditya 2f6ce55
Added docs for classes and functions with final touches
this-Aditya 704bd07
Now pages will fetch data from github
this-Aditya 9fa6243
Removed unused import
this-Aditya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const GITHUB_CONFIG = { | ||
ORGANIZATION_NAME: 'RADAR-base', | ||
REPOSITORY_NAME: process.env.GITHUB_REPO_NAME || 'radar-self-enrolment-definitions', | ||
DEFINITIONS_BRANCH: process.env.GITHUB_REPO_BRANCH_NAME || 'test', | ||
MAX_CONTENT_LENGTH: parseInt(process.env.GITHUB_RESPONSE_CONTENT_LENGTH || '1000000', 10), | ||
CACHE_DURATION: parseInt(process.env.GITHUB_RESPONSE_CACHE_DURATION || '180000', 10), | ||
CACHE_SIZE: parseInt(process.env.GITHUB_RESPONSE_CACHE_SIZE || '50', 10), | ||
API_URL: 'https://api.github.com', | ||
ACCEPT_HEADER: 'application/vnd.github.v3+json', | ||
} | ||
|
||
export const GITHUB_AUTH_CONFIG = { | ||
GITHUB_AUTH_TOKEN: process.env.GITHUB_AUTH_TOKEN || '', | ||
} | ||
|
||
export const REMOTE_DEFINITIONS_CONFIG = { | ||
CONSENT_VERSION: 'v1', | ||
ELIGIBILITY_VERSION: 'v1', | ||
STUDY_INFO_VERSION: 'v1', | ||
|
||
CONSENT_DEFINITION_FILE_NAME_CONTENT: 'consent', | ||
ELIGIBILITY_DEFINITION_FILE_NAME_CONTENT: 'eligibility', | ||
STUDY_INFO_DEFINITION_FILE_NAME_CONTENT: 'study_info' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { GITHUB_AUTH_CONFIG, GITHUB_CONFIG } from "../config/github-config"; | ||
|
||
/** | ||
* A client for interacting with the GitHub API. | ||
* Manages authentication, request headers, content length validation, and error handling. | ||
*/ | ||
class GithubClient { | ||
private readonly authorizationHeader: string; | ||
private readonly maxContentLength: number; | ||
|
||
constructor() { | ||
this.authorizationHeader = GITHUB_AUTH_CONFIG.GITHUB_AUTH_TOKEN ? `token ${GITHUB_AUTH_CONFIG.GITHUB_AUTH_TOKEN}` : ""; | ||
this.maxContentLength = GITHUB_CONFIG.MAX_CONTENT_LENGTH; | ||
} | ||
|
||
/** | ||
* Fetches data from a specified GitHub API URL. | ||
* | ||
* @param url The GitHub API endpoint URL. | ||
* @returns A promise that resolves to the fetched data in JSON format. | ||
* @throws Error if the request is unauthorized, forbidden, or if the response content is too large. | ||
*/ | ||
getData: (url: string) => Promise<any> = async (url: string) => { | ||
const headers = { | ||
Accept: GITHUB_CONFIG.ACCEPT_HEADER, | ||
Authorization: this.authorizationHeader | ||
}; | ||
const response = await fetch(url, { | ||
headers, | ||
method: 'GET', | ||
}); | ||
|
||
if (response.status === 401) { | ||
throw new Error("Unauthorized: Please check your GitHub token."); | ||
} | ||
|
||
if (response.status === 403) { | ||
throw new Error(`Forbidden: ${await response.text()}`); | ||
} | ||
|
||
if (!response.ok) { | ||
const errorText = await response.text(); | ||
throw new Error(`Failed to fetch content from GitHub: ${errorText}`); | ||
} | ||
|
||
this.checkContentLength(parseInt(response.headers.get('Content-Length') || '0', 10)); | ||
|
||
return await response.json(); | ||
} | ||
|
||
/** | ||
* Validates the content length of the API response. | ||
* | ||
* @param contentLength The length of the content received from the API. | ||
* @throws Error if the content length exceeds the maximum allowed limit. | ||
*/ | ||
private checkContentLength(contentLength: number) { | ||
if (contentLength >= this.maxContentLength) { | ||
throw new Error('Data received is too large to process'); | ||
} | ||
} | ||
} | ||
|
||
export default new GithubClient() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find a way to use
sessionStorage
ingetServerSideProps
, so I am also utilizing projectId in both the eligibility and study-consent pages.