forked from ConnectAI-E/feishu-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5751762
commit 14c3aa4
Showing
8 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package openai | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
//https://api.openai.com/dashboard/billing/credit_grants | ||
type Billing struct { | ||
Object string `json:"object"` | ||
TotalGranted float64 `json:"total_granted"` | ||
TotalUsed float64 `json:"total_used"` | ||
TotalAvailable float64 `json:"total_available"` | ||
Grants struct { | ||
Object string `json:"object"` | ||
Data []struct { | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
GrantAmount float64 `json:"grant_amount"` | ||
UsedAmount float64 `json:"used_amount"` | ||
EffectiveAt float64 `json:"effective_at"` | ||
ExpiresAt float64 `json:"expires_at"` | ||
} `json:"data"` | ||
} `json:"grants"` | ||
} | ||
|
||
type BalanceResponse struct { | ||
TotalGranted float64 `json:"total_granted"` | ||
TotalUsed float64 `json:"total_used"` | ||
TotalAvailable float64 `json:"total_available"` | ||
EffectiveAt time.Time `json:"effective_at"` | ||
ExpiresAt time.Time `json:"expires_at"` | ||
} | ||
|
||
func (gpt *ChatGPT) GetBalance() (*BalanceResponse, error) { | ||
var data Billing | ||
err := gpt.sendRequestWithBodyType( | ||
gpt.ApiUrl+"/dashboard/billing/credit_grants", | ||
http.MethodGet, | ||
nilBody, | ||
nil, | ||
&data, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get billing data: %v", err) | ||
} | ||
|
||
balance := &BalanceResponse{ | ||
TotalGranted: data.TotalGranted, | ||
TotalUsed: data.TotalUsed, | ||
TotalAvailable: data.TotalAvailable, | ||
ExpiresAt: time.Now(), | ||
EffectiveAt: time.Now(), | ||
} | ||
|
||
if len(data.Grants.Data) > 0 { | ||
balance.EffectiveAt = time.Unix(int64(data.Grants.Data[0].EffectiveAt), 0) | ||
balance.ExpiresAt = time.Unix(int64(data.Grants.Data[0].ExpiresAt), 0) | ||
} | ||
|
||
return balance, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters