Skip to content

Commit

Permalink
Merge branch 'main' into wallet-connection-info-alert-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska authored Dec 12, 2024
2 parents bcddd26 + 6d42980 commit 1573f56
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
65 changes: 53 additions & 12 deletions sdk/src/lib/api/HttpApi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
import { backoffRetrier, RetryOptions } from "../utils"

/**
* Represents an abstract HTTP API.
*/
export default abstract class HttpApi {
#apiUrl: string

constructor(apiUrl: string) {
/**
* Retry options for API requests.
*/
#retryOptions: RetryOptions

constructor(
apiUrl: string,
retryOptions: RetryOptions = {
retries: 5,
backoffStepMs: 1000,
},
) {
this.#apiUrl = apiUrl
this.#retryOptions = retryOptions
}

/**
* Makes an HTTP request with retry logic.
* @param requestFn Function that returns a Promise of the HTTP response.
* @returns The HTTP response.
* @throws Error if the request fails after all retries.
*/
async #requestWithRetry(
requestFn: () => Promise<Response>,
): Promise<Response> {
return backoffRetrier<Response>(
this.#retryOptions.retries,
this.#retryOptions.backoffStepMs,
)(async () => {
const response = await requestFn()

if (!response.ok) {
throw new Error(`Request failed: ${await response.text()}`)
}

return response
})
}

/**
Expand All @@ -18,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 @@ -36,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,
}),
)
}
}
28 changes: 13 additions & 15 deletions sdk/src/lib/api/TbtcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ export default class TbtcApi extends HttpApi {
* otherwise.
*/
async saveReveal(revealData: SaveRevealRequest): Promise<boolean> {
const response = await this.postRequest("reveals", revealData)

if (!response.ok)
throw new Error(
`Reveal not saved properly in the database, response: ${response.status}`,
)
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 @@ -60,11 +59,11 @@ export default class TbtcApi extends HttpApi {
depositStatus: DepositStatus
fundingOutpoint: BitcoinTxOutpoint
}> {
const response = await this.postRequest("deposits", depositData)
if (!response.ok)
throw new Error(
`Bitcoin deposit creation failed, response: ${response.status}`,
)
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 @@ -85,10 +84,9 @@ export default class TbtcApi extends HttpApi {
async getDepositsByOwner(depositOwner: ChainIdentifier): Promise<Deposit[]> {
const response = await this.getRequest(
`deposits/${depositOwner.identifierHex}`,
)

if (!response.ok)
throw new Error(`Failed to fetch deposits: ${response.status}`)
).catch((error) => {
throw new Error(`Failed to fetch deposits: ${error}`)
})

const responseData = (await response.json()) as Deposit[]

Expand Down

0 comments on commit 1573f56

Please sign in to comment.