diff --git a/src/content/pricing/pricestf.ts b/src/content/pricing/pricestf.ts index bf56fa0..1d6989d 100644 --- a/src/content/pricing/pricestf.ts +++ b/src/content/pricing/pricestf.ts @@ -1,5 +1,6 @@ declare function GM_fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise import '../GM_fetch' +import { logDebug } from '../utils/log' async function getPricesToken(): Promise { return new Promise((resolve, reject) => { @@ -37,7 +38,7 @@ class PricesResponse { * @returns {Promise} Object containing 'keys' and 'metal' prices * @throws When authentication fails or API returns non-200 status code */ -async function priceUsingPricesTF(token: string, sku: string): Promise { +async function priceUsingPricesTF(token: string, sku: string, retries: number = 3): Promise { // prices.tf // https://api2.prices.tf/prices/${sku} // Authorization: Bearer ${token} @@ -65,11 +66,37 @@ async function priceUsingPricesTF(token: string, sku: string): Promise 0) { + logDebug(`Cloudflare rate limit exceeded, trying again after 2 seconds, ${retries} retries left`) + await new Promise(resolve => setTimeout(resolve, 2000)); + try { + const retryResult = await priceUsingPricesTF(token, sku, retries - 1); + resolve(retryResult); + } catch (err) { + reject(err); + } + } else { + reject("Cloudflare rate limit exceeded, stopping") + } + break; + default: + // Something went wrong + logDebug(`Received ${response.status} error while pricing ${sku}`) + logDebug(`${JSON.stringify(response.headers)}`) + reject("Unknown error") + break; } }) }