ai-gateway/controller/billing.go

61 lines
1.2 KiB
Go
Raw Normal View History

package controller
import (
"github.com/gin-gonic/gin"
2023-06-20 12:09:17 +00:00
"one-api/common"
"one-api/model"
)
func GetSubscription(c *gin.Context) {
userId := c.GetInt("id")
quota, err := model.GetUserQuota(userId)
if err != nil {
openAIError := OpenAIError{
Message: err.Error(),
Type: "one_api_error",
}
c.JSON(200, gin.H{
"error": openAIError,
})
return
}
2023-06-20 12:09:17 +00:00
amount := float64(quota)
if common.DisplayInCurrencyEnabled {
amount /= common.QuotaPerUnit
}
subscription := OpenAISubscriptionResponse{
Object: "billing_subscription",
HasPaymentMethod: true,
2023-06-20 12:09:17 +00:00
SoftLimitUSD: amount,
HardLimitUSD: amount,
SystemHardLimitUSD: amount,
}
c.JSON(200, subscription)
return
}
func GetUsage(c *gin.Context) {
2023-06-20 12:09:17 +00:00
userId := c.GetInt("id")
quota, err := model.GetUserUsedQuota(userId)
if err != nil {
openAIError := OpenAIError{
Message: err.Error(),
Type: "one_api_error",
}
c.JSON(200, gin.H{
"error": openAIError,
})
return
}
amount := float64(quota)
2023-06-20 13:05:07 +00:00
if common.DisplayInCurrencyEnabled {
amount /= common.QuotaPerUnit
}
usage := OpenAIUsageResponse{
Object: "list",
2023-06-20 12:09:17 +00:00
TotalUsage: amount,
}
c.JSON(200, usage)
return
}