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

Function updateChannelMoonshotBalance

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

Source from the content-addressed store, hash-verified

310}
311
312func updateChannelMoonshotBalance(channel *model.Channel) (float64, error) {
313 url := "https://api.moonshot.cn/v1/users/me/balance"
314 body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
315 if err != nil {
316 return 0, err
317 }
318
319 type MoonshotBalanceData struct {
320 AvailableBalance float64 `json:"available_balance"`
321 VoucherBalance float64 `json:"voucher_balance"`
322 CashBalance float64 `json:"cash_balance"`
323 }
324
325 type MoonshotBalanceResponse struct {
326 Code int `json:"code"`
327 Data MoonshotBalanceData `json:"data"`
328 Scode string `json:"scode"`
329 Status bool `json:"status"`
330 }
331
332 response := MoonshotBalanceResponse{}
333 err = json.Unmarshal(body, &response)
334 if err != nil {
335 return 0, err
336 }
337 if !response.Status || response.Code != 0 {
338 return 0, fmt.Errorf("failed to update moonshot balance, status: %v, code: %d, scode: %s", response.Status, response.Code, response.Scode)
339 }
340 availableBalanceCny := response.Data.AvailableBalance
341 availableBalanceUsd := decimal.NewFromFloat(availableBalanceCny).Div(decimal.NewFromFloat(setting.Price)).InexactFloat64()
342 channel.UpdateBalance(availableBalanceUsd)
343 return availableBalanceUsd, nil
344}
345
346func updateChannelBalance(channel *model.Channel) (float64, error) {
347 baseURL := constant.ChannelBaseURLs[channel.Type]

Callers 1

updateChannelBalanceFunction · 0.85

Calls 3

GetResponseBodyFunction · 0.85
GetAuthHeaderFunction · 0.85
UpdateBalanceMethod · 0.80

Tested by

no test coverage detected