mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-14 20:23:46 +08:00
✨ 添加余额查询方法
This commit is contained in:
35
providers/aiproxy/balance.go
Normal file
35
providers/aiproxy/balance.go
Normal 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
18
providers/aiproxy/base.go
Normal 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
10
providers/aiproxy/type.go
Normal 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"`
|
||||
}
|
||||
Reference in New Issue
Block a user