-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbalance.go
59 lines (47 loc) · 1.7 KB
/
balance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package deepseek
import (
"context"
"encoding/json"
"fmt"
"io"
utils "github.com/cohesion-org/deepseek-go/utils"
)
// BalanceInfo represents the balance information for a specific currency.
type BalanceInfo struct {
Currency string `json:"currency"` //The currency of the balance.
TotalBalance string `json:"total_balance"` //The total available balance, including the granted balance and the topped-up balance.
GrantedBalance string `json:"granted_balance"` //The total not expired granted balance.
ToppedUpBalance string `json:"topped_up_balance"` //The total topped-up balance.
}
// BalanceResponse represents the response from the API endpoint.
type BalanceResponse struct {
IsAvailable bool `json:"is_available"` //Whether the user's balance is sufficient for API calls.
BalanceInfos []BalanceInfo `json:"balance_infos"` //List of Balance infos
}
// GetBalance sends a request to the API to get the user's balance.
func GetBalance(c *Client, ctx context.Context) (*BalanceResponse, error) {
req, err := utils.NewRequestBuilder(c.AuthToken).
SetBaseURL("https://api.deepseek.com/").
SetPath("user/balance").
BuildGet(ctx)
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := HandleNormalRequest(*c, req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, HandleAPIError(resp)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var balance BalanceResponse
if err := json.Unmarshal(body, &balance); err != nil {
return nil, fmt.Errorf("failed to parse response JSON: %w", err)
}
return &balance, nil
}