Retry XIVAPI requests once if they fail

This commit is contained in:
karashiiro 2021-05-31 11:03:21 -07:00
parent e54bffe7fb
commit 1e6aeda945
2 changed files with 13 additions and 5 deletions

View file

@ -290,7 +290,13 @@ class CardCreator {
* @returns {Promise<Buffer>} 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;

View file

@ -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;
}