(channel *model.Channel)
| 344 | } |
| 345 | |
| 346 | func 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 { |
no test coverage detected