refactor: codex usage ui (#5595)
* refactor: codex usage ui * feat: show Codex reset credit details * feat: add Codex usage reset flow
This commit is contained in:
+111
-6
@@ -1,11 +1,15 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func FetchCodexWhamUsage(
|
||||
@@ -35,12 +39,7 @@ func FetchCodexWhamUsage(
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+at)
|
||||
req.Header.Set("chatgpt-account-id", aid)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if req.Header.Get("originator") == "" {
|
||||
req.Header.Set("originator", "codex_cli_rs")
|
||||
}
|
||||
setCodexWhamRequestHeaders(req, at, aid)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
@@ -54,3 +53,109 @@ func FetchCodexWhamUsage(
|
||||
}
|
||||
return resp.StatusCode, body, nil
|
||||
}
|
||||
|
||||
func FetchCodexWhamRateLimitResetCredits(
|
||||
ctx context.Context,
|
||||
client *http.Client,
|
||||
baseURL string,
|
||||
accessToken string,
|
||||
accountID string,
|
||||
) (statusCode int, body []byte, err error) {
|
||||
if client == nil {
|
||||
return 0, nil, fmt.Errorf("nil http client")
|
||||
}
|
||||
bu := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
if bu == "" {
|
||||
return 0, nil, fmt.Errorf("empty baseURL")
|
||||
}
|
||||
at := strings.TrimSpace(accessToken)
|
||||
aid := strings.TrimSpace(accountID)
|
||||
if at == "" {
|
||||
return 0, nil, fmt.Errorf("empty accessToken")
|
||||
}
|
||||
if aid == "" {
|
||||
return 0, nil, fmt.Errorf("empty accountID")
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, bu+"/backend-api/wham/rate-limit-reset-credits", nil)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
setCodexWhamRequestHeaders(req, at, aid)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp.StatusCode, nil, err
|
||||
}
|
||||
return resp.StatusCode, body, nil
|
||||
}
|
||||
|
||||
func ConsumeCodexWhamRateLimitResetCredit(
|
||||
ctx context.Context,
|
||||
client *http.Client,
|
||||
baseURL string,
|
||||
accessToken string,
|
||||
accountID string,
|
||||
) (statusCode int, body []byte, err error) {
|
||||
if client == nil {
|
||||
return 0, nil, fmt.Errorf("nil http client")
|
||||
}
|
||||
bu := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
if bu == "" {
|
||||
return 0, nil, fmt.Errorf("empty baseURL")
|
||||
}
|
||||
at := strings.TrimSpace(accessToken)
|
||||
aid := strings.TrimSpace(accountID)
|
||||
if at == "" {
|
||||
return 0, nil, fmt.Errorf("empty accessToken")
|
||||
}
|
||||
if aid == "" {
|
||||
return 0, nil, fmt.Errorf("empty accountID")
|
||||
}
|
||||
|
||||
requestBody, err := common.Marshal(map[string]string{
|
||||
"redeem_request_id": uuid.NewString(),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
bu+"/backend-api/wham/rate-limit-reset-credits/consume",
|
||||
bytes.NewReader(requestBody),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
setCodexWhamRequestHeaders(req, at, aid)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp.StatusCode, nil, err
|
||||
}
|
||||
return resp.StatusCode, body, nil
|
||||
}
|
||||
|
||||
func setCodexWhamRequestHeaders(req *http.Request, accessToken string, accountID string) {
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("chatgpt-account-id", accountID)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if req.Header.Get("originator") == "" {
|
||||
req.Header.Set("originator", "codex_cli_rs")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user