From 916411ca2598ab5a5adf32c6ea0e46f55d0b87c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Fri, 5 Apr 2024 08:24:32 +0200 Subject: [PATCH] feat: Retry msgraph API calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gustaf Räntilä --- .../src/microsoftGraph/client.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts b/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts index f1ccc44a99..1233b1e2f9 100644 --- a/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts +++ b/plugins/catalog-backend-module-msgraph/src/microsoftGraph/client.ts @@ -216,6 +216,7 @@ export class MicrosoftGraphClient { async requestRaw( url: string, headers?: Record, + retryCount = 2, ): Promise { // Make sure that we always have a valid access token (might be cached) const urlObj = new URL(url); @@ -227,12 +228,19 @@ export class MicrosoftGraphClient { throw new Error('Failed to obtain token from Azure Identity'); } - return await fetch(url, { - headers: { - ...headers, - Authorization: `Bearer ${token.token}`, - }, - }); + try { + return await fetch(url, { + headers: { + ...headers, + Authorization: `Bearer ${token.token}`, + }, + }); + } catch (e: any) { + if (e?.code === 'ETIMEDOUT' && retryCount > 0) { + return this.requestRaw(url, headers, retryCount - 1); + } + throw e; + } } /**