Skip to content

Commit

Permalink
Enable retries for all HTTP requests in SDK
Browse files Browse the repository at this point in the history
Previously we were only retrying HTTP requests in the SDK for tBTC API
calls. We can generalize this solution and apply it to all HTTP requests.
  • Loading branch information
nkuba committed Dec 6, 2024
1 parent 37b718a commit 2196fe3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
28 changes: 16 additions & 12 deletions sdk/src/lib/api/HttpApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default abstract class HttpApi {
* @returns The HTTP response.
* @throws Error if the request fails after all retries.
*/
async requestWithRetry(
async #requestWithRetry(
requestFn: () => Promise<Response>,
): Promise<Response> {
return backoffRetrier<Response>(
Expand All @@ -55,10 +55,12 @@ export default abstract class HttpApi {
endpoint: string,
requestInit?: RequestInit,
): Promise<Response> {
return fetch(new URL(endpoint, this.#apiUrl), {
credentials: "include",
...requestInit,
})
return this.#requestWithRetry(async () =>
fetch(new URL(endpoint, this.#apiUrl), {
credentials: "include",
...requestInit,
}),
)
}

/**
Expand All @@ -73,12 +75,14 @@ export default abstract class HttpApi {
body: unknown,
requestInit?: RequestInit,
): Promise<Response> {
return fetch(new URL(endpoint, this.#apiUrl), {
method: "POST",
body: JSON.stringify(body),
credentials: "include",
headers: { "Content-Type": "application/json" },
...requestInit,
})
return this.#requestWithRetry(async () =>
fetch(new URL(endpoint, this.#apiUrl), {
method: "POST",
body: JSON.stringify(body),
credentials: "include",
headers: { "Content-Type": "application/json" },
...requestInit,
}),
)
}
}
24 changes: 12 additions & 12 deletions sdk/src/lib/api/TbtcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export default class TbtcApi extends HttpApi {
* otherwise.
*/
async saveReveal(revealData: SaveRevealRequest): Promise<boolean> {
const response = await this.requestWithRetry(async () =>
this.postRequest("reveals", revealData),
).catch((error) => {
throw new Error(`Failed to save reveal: ${error}`)
})
const response = await this.postRequest("reveals", revealData).catch(
(error) => {
throw new Error(`Failed to save reveal: ${error}`)
},
)

const { success } = (await response.json()) as { success: boolean }

Expand All @@ -59,11 +59,11 @@ export default class TbtcApi extends HttpApi {
depositStatus: DepositStatus
fundingOutpoint: BitcoinTxOutpoint
}> {
const response = await this.requestWithRetry(async () =>
this.postRequest("deposits", depositData),
).catch((error) => {
throw new Error(`Failed to create deposit: ${error}`)
})
const response = await this.postRequest("deposits", depositData).catch(
(error) => {
throw new Error(`Failed to create deposit: ${error}`)
},
)

const responseData = (await response.json()) as CreateDepositResponse

Expand All @@ -82,8 +82,8 @@ export default class TbtcApi extends HttpApi {
* @returns All owner deposits, including queued deposits.
*/
async getDepositsByOwner(depositOwner: ChainIdentifier): Promise<Deposit[]> {
const response = await this.requestWithRetry(async () =>
this.getRequest(`deposits/${depositOwner.identifierHex}`),
const response = await this.getRequest(
`deposits/${depositOwner.identifierHex}`,
).catch((error) => {
throw new Error(`Failed to fetch deposits: ${error}`)
})
Expand Down

0 comments on commit 2196fe3

Please sign in to comment.