MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / updateChannelBalance

Function updateChannelBalance

controller/channel-billing.go:346–409  ·  view source on GitHub ↗
(channel *model.Channel)

Source from the content-addressed store, hash-verified

344}
345
346func updateChannelBalance(channel *model.Channel) (float64, error) {
347 baseURL := constant.ChannelBaseURLs[channel.Type]
348 if channel.GetBaseURL() == "" {
349 channel.BaseURL = &baseURL
350 }
351 switch channel.Type {
352 case constant.ChannelTypeOpenAI:
353 if channel.GetBaseURL() != "" {
354 baseURL = channel.GetBaseURL()
355 }
356 case constant.ChannelTypeAzure:
357 return 0, errors.New("尚未实现")
358 case constant.ChannelTypeCustom:
359 baseURL = channel.GetBaseURL()
360 //case common.ChannelTypeOpenAISB:
361 // return updateChannelOpenAISBBalance(channel)
362 case constant.ChannelTypeAIProxy:
363 return updateChannelAIProxyBalance(channel)
364 case constant.ChannelTypeAPI2GPT:
365 return updateChannelAPI2GPTBalance(channel)
366 case constant.ChannelTypeAIGC2D:
367 return updateChannelAIGC2DBalance(channel)
368 case constant.ChannelTypeSiliconFlow:
369 return updateChannelSiliconFlowBalance(channel)
370 case constant.ChannelTypeDeepSeek:
371 return updateChannelDeepSeekBalance(channel)
372 case constant.ChannelTypeOpenRouter:
373 return updateChannelOpenRouterBalance(channel)
374 case constant.ChannelTypeMoonshot:
375 return updateChannelMoonshotBalance(channel)
376 default:
377 return 0, errors.New("尚未实现")
378 }
379 url := fmt.Sprintf("%s/v1/dashboard/billing/subscription", baseURL)
380
381 body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
382 if err != nil {
383 return 0, err
384 }
385 subscription := OpenAISubscriptionResponse{}
386 err = json.Unmarshal(body, &subscription)
387 if err != nil {
388 return 0, err
389 }
390 now := time.Now()
391 startDate := fmt.Sprintf("%s-01", now.Format("2006-01"))
392 endDate := now.Format("2006-01-02")
393 if !subscription.HasPaymentMethod {
394 startDate = now.AddDate(0, 0, -100).Format("2006-01-02")
395 }
396 url = fmt.Sprintf("%s/v1/dashboard/billing/usage?start_date=%s&end_date=%s", baseURL, startDate, endDate)
397 body, err = GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
398 if err != nil {
399 return 0, err
400 }
401 usage := OpenAIUsageResponse{}
402 err = json.Unmarshal(body, &usage)
403 if err != nil {

Callers 2

UpdateChannelBalanceFunction · 0.70
updateAllChannelsBalanceFunction · 0.70

Calls 11

GetResponseBodyFunction · 0.85
GetAuthHeaderFunction · 0.85
GetBaseURLMethod · 0.80
UpdateBalanceMethod · 0.80

Tested by

no test coverage detected