添加余额查询方法

This commit is contained in:
MartialBE
2023-12-02 17:51:28 +08:00
parent d8b13b2c07
commit 5e08cc8719
8 changed files with 116 additions and 5 deletions

View File

@@ -0,0 +1,35 @@
package aiproxy
import (
"errors"
"fmt"
"one-api/common"
"one-api/model"
)
func (p *AIProxyProvider) Balance(channel *model.Channel) (float64, error) {
fullRequestURL := "https://aiproxy.io/api/report/getUserOverview"
headers := make(map[string]string)
headers["Api-Key"] = channel.Key
client := common.NewClient()
req, err := client.NewRequest("GET", fullRequestURL, common.WithHeader(headers))
if err != nil {
return 0, err
}
// 发送请求
var response AIProxyUserOverviewResponse
_, errWithCode := common.SendRequest(req, &response, false)
if errWithCode != nil {
return 0, errors.New(errWithCode.OpenAIError.Message)
}
if !response.Success {
return 0, fmt.Errorf("code: %d, message: %s", response.ErrorCode, response.Message)
}
channel.UpdateBalance(response.Data.TotalPoints)
return response.Data.TotalPoints, nil
}

18
providers/aiproxy/base.go Normal file
View File

@@ -0,0 +1,18 @@
package aiproxy
import (
"one-api/providers/openai"
"github.com/gin-gonic/gin"
)
type AIProxyProvider struct {
*openai.OpenAIProvider
}
// 创建 CreateAIProxyProvider
func CreateAIProxyProvider(c *gin.Context) *AIProxyProvider {
return &AIProxyProvider{
OpenAIProvider: openai.CreateOpenAIProvider(c, "https://api.aiproxy.io"),
}
}

10
providers/aiproxy/type.go Normal file
View File

@@ -0,0 +1,10 @@
package aiproxy
type AIProxyUserOverviewResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
ErrorCode int `json:"error_code"`
Data struct {
TotalPoints float64 `json:"totalPoints"`
} `json:"data"`
}