feat: support channel type AIGC2D (#220)

* feat: add AIGC2D Channel

* chore: remove console logging & update balance rendering

---------

Co-authored-by: Alone88 <im@aloen88.cn>
Co-authored-by: JustSong <songquanpeng@foxmail.com>
This commit is contained in:
Alone88
2023-06-29 21:34:17 +08:00
committed by GitHub
parent 8f6bd51f58
commit b1b3651e84
4 changed files with 31 additions and 1 deletions

View File

@@ -61,6 +61,14 @@ type API2GPTUsageResponse struct {
TotalRemaining float64 `json:"total_remaining"`
}
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"`
}
// GetAuthHeader get auth header
func GetAuthHeader(token string) http.Header {
h := http.Header{}
@@ -150,6 +158,21 @@ func updateChannelAPI2GPTBalance(channel *model.Channel) (float64, error) {
return response.TotalRemaining, nil
}
func updateChannelAIGC2DBalance(channel *model.Channel) (float64, error) {
url := "https://api.aigc2d.com/dashboard/billing/credit_grants"
body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
if err != nil {
return 0, err
}
response := APGC2DGPTUsageResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
return 0, err
}
channel.UpdateBalance(response.TotalAvailable)
return response.TotalAvailable, nil
}
func updateChannelBalance(channel *model.Channel) (float64, error) {
baseURL := common.ChannelBaseURLs[channel.Type]
switch channel.Type {
@@ -167,6 +190,8 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
return updateChannelAIProxyBalance(channel)
case common.ChannelTypeAPI2GPT:
return updateChannelAPI2GPTBalance(channel)
case common.ChannelTypeAIGC2D:
return updateChannelAIGC2DBalance(channel)
default:
return 0, errors.New("尚未实现")
}