添加余额查询方法

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,29 @@
package api2d
import (
"errors"
"one-api/common"
"one-api/model"
)
func (p *Api2dProvider) Balance(channel *model.Channel) (float64, error) {
fullRequestURL := "https://api.aigc2d.com/dashboard/billing/credit_grants"
headers := p.GetRequestHeaders()
client := common.NewClient()
req, err := client.NewRequest("GET", fullRequestURL, common.WithHeader(headers))
if err != nil {
return 0, err
}
// 发送请求
var response APGC2DGPTUsageResponse
_, errWithCode := common.SendRequest(req, &response, false)
if errWithCode != nil {
return 0, errors.New(errWithCode.OpenAIError.Message)
}
channel.UpdateBalance(response.TotalAvailable)
return response.TotalAvailable, nil
}

9
providers/api2d/type.go Normal file
View File

@@ -0,0 +1,9 @@
package api2d
type APGC2DGPTUsageResponse struct {
//Grants interface{} `json:"grants"`
Object string `json:"object"`
TotalAvailable float64 `json:"total_available"`
TotalGranted float64 `json:"total_granted"`
TotalUsed float64 `json:"total_used"`
}