From 1e6aeda945d2d232f29d59583875c37c11b251f0 Mon Sep 17 00:00:00 2001 From: karashiiro <49822414+karashiiro@users.noreply.github.com> Date: Mon, 31 May 2021 11:03:21 -0700 Subject: [PATCH] Retry XIVAPI requests once if they fail --- create-card.js | 8 +++++++- index.js | 10 ++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/create-card.js b/create-card.js index 5951633..1863365 100644 --- a/create-card.js +++ b/create-card.js @@ -290,7 +290,13 @@ class CardCreator { * @returns {Promise} A promise representating the construction of the card's image data. */ async createCard(charaId, customImage) { - const response = await fetch(`https://xivapi.com/character/${charaId}?extended=1&data=FC,mimo`); + const characterInfoUrl = `https://xivapi.com/character/${charaId}?extended=1&data=FC,mimo`; + const response = await fetch(characterInfoUrl); + if (!response.ok) { + // Retry once if the request fails + response = await fetch(characterInfoUrl); + } + const data = await response.json(); const canvasSize = this.canvasSize; diff --git a/index.js b/index.js index 2c9149a..910fdc3 100644 --- a/index.js +++ b/index.js @@ -24,12 +24,14 @@ var diskCache = cacheManager.caching({ } }); -async function getCharIdByName(world, name) { - var response = await fetch(`https://xivapi.com/character/search?name=${name}&server=${world}`); - var data = await response.json(); +async function getCharIdByName(world, name, retries = 1) { + if (retries === -1) return undefined; + + const response = await fetch(`https://xivapi.com/character/search?name=${name}&server=${world}`); + const data = await response.json(); if (data.Results[0] === undefined) - return undefined; + return getCharIdByName(world, name, --retries); return data.Results[0].ID; }