Skip to content

Commit 9419abc

Browse files
committed
Add axios retry with exponential backoff to certs API helpers
1 parent 85112eb commit 9419abc

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

integration-tests/helpers/certs-helpers.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,35 @@ export function getCertsApiToken(): string {
2020
return certsApiToken;
2121
}
2222

23+
async function axiosWithRetry(requestFn: () => Promise<any>, maxRetries = 3) {
24+
for (let attempt = 0; attempt < maxRetries; attempt++) {
25+
try {
26+
return await requestFn();
27+
} catch (err) {
28+
const httpStatus = err?.response?.status;
29+
const isRetryable = !httpStatus || httpStatus === 429 || httpStatus >= 500;
30+
31+
if (!isRetryable || attempt === maxRetries - 1) {
32+
throw err;
33+
}
34+
const delay = Math.min(1000 * 2 ** attempt, 8000);
35+
console.log(`Request failed with status ${httpStatus ?? 'network error'}, retrying in ${delay}ms (attempt ${attempt + 1}/${maxRetries})`);
36+
await new Promise(resolve => setTimeout(resolve, delay));
37+
}
38+
}
39+
}
40+
2341
export async function createProofRequest(templateId: string) {
2442
console.log('Requesting proof from Certs API');
25-
const {data} = await axios.post(
43+
const {data} = await axiosWithRetry(() => axios.post(
2644
`${getCertsApiURL()}/proof-templates/${templateId}/request`,
2745
{},
2846
{
2947
headers: {
3048
'DOCK-API-TOKEN': getCertsApiToken(),
3149
},
3250
},
33-
);
51+
));
3452

3553
if (!data.request.id) {
3654
data.request.id = data.id;
@@ -46,7 +64,7 @@ export async function createProofRequest(templateId: string) {
4664
export function issueCredential({subjectDID}) {
4765
console.log('Issuing credential for DID', subjectDID);
4866

49-
return axios.post(
67+
return axiosWithRetry(() => axios.post(
5068
`${certsApiURL}/credentials`,
5169
{
5270
anchor: false,
@@ -70,5 +88,5 @@ export function issueCredential({subjectDID}) {
7088
'Content-Type': 'application/json',
7189
},
7290
},
73-
);
91+
));
7492
}

0 commit comments

Comments
 (0)