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