Skip to content

Commit

Permalink
Avoid time.DateOnly
Browse files Browse the repository at this point in the history
It looks like the github action build environment for this repo uses
a go 1.19.x environment which does not have time.DateOnly. Replace
with local constant until this is upgraded.

(cherry picked from commit 22886dd)
(cherry picked from commit 5225542)
  • Loading branch information
mikeb26 committed Mar 4, 2024
1 parent 26947c0 commit fbaa1f1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type BillingUsageResponse struct {
httpHeader
}

// @todo remove this and replace w/ time.DateOnly once github build
// environment is upgraded to a more recent go toolchain.
const DateOnly = "2006-01-02"

// currently the OpenAI usage API is not publicly documented and will explictly
// reject requests using an API key authorization. however, it can be utilized
// logging into https://platform.openai.com/usage and retrieving your session
Expand All @@ -43,8 +47,8 @@ var (
// GetBillingUsage — API call to Get billing usage details.
func (c *Client) GetBillingUsage(ctx context.Context, startDate time.Time,
endDate time.Time) (response BillingUsageResponse, err error) {
startDateArg := fmt.Sprintf("start_date=%v", startDate.Format(time.DateOnly))
endDateArg := fmt.Sprintf("end_date=%v", endDate.Format(time.DateOnly))
startDateArg := fmt.Sprintf("start_date=%v", startDate.Format(DateOnly))
endDateArg := fmt.Sprintf("end_date=%v", endDate.Format(DateOnly))
queryParams := fmt.Sprintf("%v&%v", startDateArg, endDateArg)
urlSuffix := fmt.Sprintf("%v?%v", billingUsageSuffix, queryParams)

Expand Down
10 changes: 5 additions & 5 deletions billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestBillingUsageAPIKey(t *testing.T) {

ctx := context.Background()

endDate, err := time.Parse(time.DateOnly, TestEndDate)
endDate, err := time.Parse(openai.DateOnly, TestEndDate)
checks.NoError(t, err)
startDate, err := time.Parse(time.DateOnly, TestStartDate)
startDate, err := time.Parse(openai.DateOnly, TestStartDate)
checks.NoError(t, err)

_, err = client.GetBillingUsage(ctx, startDate, endDate)
Expand All @@ -43,9 +43,9 @@ func TestBillingUsageSessKey(t *testing.T) {
server.RegisterHandler("/dashboard/billing/usage", handleBillingEndpoint)

ctx := context.Background()
endDate, err := time.Parse(time.DateOnly, TestEndDate)
endDate, err := time.Parse(openai.DateOnly, TestEndDate)
checks.NoError(t, err)
startDate, err := time.Parse(time.DateOnly, TestStartDate)
startDate, err := time.Parse(openai.DateOnly, TestStartDate)
checks.NoError(t, err)

resp, err := client.GetBillingUsage(ctx, startDate, endDate)
Expand Down Expand Up @@ -82,7 +82,7 @@ func handleBillingEndpoint(w http.ResponseWriter, r *http.Request) {

dailyCosts := make([]openai.DailyCostResponse, 0)

d, _ := time.Parse(time.DateOnly, TestStartDate)
d, _ := time.Parse(openai.DateOnly, TestStartDate)
d = d.Add(24 * time.Hour)
dailyCosts = append(dailyCosts, openai.DailyCostResponse{
TimestampRaw: float64(d.Unix()),
Expand Down

0 comments on commit fbaa1f1

Please sign in to comment.