From b0c8bb062147d71a3ab288a94b65f887cfb9fbf5 Mon Sep 17 00:00:00 2001 From: josc146 Date: Mon, 12 Jun 2023 16:32:52 +0800 Subject: [PATCH] compatible balance query with third-party services --- src/popup/sections/GeneralPart.jsx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/popup/sections/GeneralPart.jsx b/src/popup/sections/GeneralPart.jsx index 2dad111..27b0d1b 100644 --- a/src/popup/sections/GeneralPart.jsx +++ b/src/popup/sections/GeneralPart.jsx @@ -81,9 +81,18 @@ export function GeneralPart({ config, updateConfig }) { const [balance, setBalance] = useState(null) const getBalance = async () => { - const billing = await checkBilling(config.apiKey, config.customOpenAiApiUrl) - if (billing && billing.length > 2 && billing[2]) setBalance(`${billing[2].toFixed(2)}`) - else openUrl('https://platform.openai.com/account/usage') + const response = await fetch(`${config.customOpenAiApiUrl}/dashboard/billing/credit_grants`, { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${config.apiKey}`, + }, + }) + if (response.ok) setBalance((await response.json()).total_available.toFixed(2)) + else { + const billing = await checkBilling(config.apiKey, config.customOpenAiApiUrl) + if (billing && billing.length > 2 && billing[2]) setBalance(`${billing[2].toFixed(2)}`) + else openUrl('https://platform.openai.com/account/usage') + } } return (