Skip to content
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

fix: Add long await to the client (#822) #823

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/api/autoacmg/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('AutoACMGClient', () => {
ins: 'A',
userRepr: 'GRCh37-17-41245466-G-A'
})
).rejects.toThrow('HTTP error! status: 404')
).rejects.toThrow('Failed to fetch auto-acmg.')
})

it('should throw an error for invalid JSON response', async () => {
Expand Down
53 changes: 42 additions & 11 deletions frontend/src/api/autoacmg/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ export class AutoACMGClient {
}
}

/**
* Helper function for long-time requests
*
* @param url The url to request
* @param retries Number of retries
* @param delay The delay time
* @returns The Promise response
*/
async fetchWithRetry(url: string, retries: number = 10, delay: number = 5000): Promise<Response> {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
if (!response.ok && retries > 0) {
console.log(`Retrying... ${retries} retries left`)
await new Promise((resolve) => setTimeout(resolve, delay))
return this.fetchWithRetry(url, retries - 1, delay)
}
return response
} catch (error) {
if (retries > 0) {
console.log(`Error occurred, retrying... ${retries} retries left`)
await new Promise((resolve) => setTimeout(resolve, delay))
return this.fetchWithRetry(url, retries - 1, delay)
}
throw error
}
}

/**
* Classify sequence variant using AutoACMG.
*
Expand All @@ -36,21 +68,20 @@ export class AutoACMGClient {
async classifySequenceVariant(seqvar: Seqvar): Promise<AutoACMGSeqVarResult> {
const seqvarName = `chr${seqvar.chrom}:${seqvar.pos}:${seqvar.del}:${seqvar.ins}`

console.log('REQUESTING', this.apiBaseUrl)
const url =
`${this.apiBaseUrl}/predict/seqvar?variant_name=${seqvarName}` +
`&genome_release=${seqvar.genomeBuild}`
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
try {
const response = await this.fetchWithRetry(url)

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
return data['prediction']
} catch (error) {
throw new Error('Failed to fetch auto-acmg.')
}
const data = await response.json()
return data['prediction']
}
}
Loading