Skip to content

Commit

Permalink
Add support for list of billing pending charges route (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
DazWilkin authored Feb 19, 2025
1 parent d756953 commit 6a36887
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
29 changes: 29 additions & 0 deletions billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type BillingService interface {
ListInvoices(ctx context.Context, options *ListOptions) ([]Invoice, *Meta, *http.Response, error)
GetInvoice(ctx context.Context, invoiceID string) (*Invoice, *http.Response, error)
ListInvoiceItems(ctx context.Context, invoiceID int, options *ListOptions) ([]InvoiceItem, *Meta, *http.Response, error)
ListPendingCharges(ctx context.Context, options *ListOptions) ([]InvoiceItem, *http.Response, error)
}

// BillingServiceHandler handles interaction with the billing methods for the Vultr API
Expand Down Expand Up @@ -72,6 +73,10 @@ type invoiceItemsBase struct {
Meta *Meta `json:"meta"`
}

type pendingChargesBase struct {
PendingCharges []InvoiceItem `json:"pending_charges"`
}

// ListHistory retrieves a list of all billing history on the current account
func (b *BillingServiceHandler) ListHistory(ctx context.Context, options *ListOptions) ([]History, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/billing/history"
Expand Down Expand Up @@ -161,3 +166,27 @@ func (b *BillingServiceHandler) ListInvoiceItems(ctx context.Context, invoiceID

return invoice.InvoiceItems, invoice.Meta, resp, nil
}

// ListPendingCharges retrieves a list of all pending charges on the current account
func (b *BillingServiceHandler) ListPendingCharges(ctx context.Context, options *ListOptions) ([]InvoiceItem, *http.Response, error) {
uri := "/v2/billing/pending-charges"
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}

newValues, err := query.Values(options)
if err != nil {
return nil, nil, err
}

req.URL.RawQuery = newValues.Encode()

invoice := new(pendingChargesBase)
resp, err := b.client.DoWithContext(ctx, req, invoice)
if err != nil {
return nil, resp, err
}

return invoice.PendingCharges, resp, nil
}
47 changes: 47 additions & 0 deletions billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,50 @@ func TestBillingServiceHandler_ListInvoiceItems(t *testing.T) {
t.Errorf("Billing.ListInvoiceItems returned %+v, expected %+v", meta, expectedMeta)
}
}

func TestBillingServiceHandler_ListPendingCharges(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/billing/pending-charges", func(w http.ResponseWriter, r *http.Request) {
response := `
{
"pending_charges": [
{
"description": "Load Balancer (my-loadbalancer)",
"start_date": "2020-10-10T01:56:20+00:00",
"end_date": "2020-10-10T01:56:20+00:00",
"units": 720,
"unit_type": "hours",
"unit_price": 0.0149,
"total": 10,
"product": "Load Balancer"
}
]
}
`

fmt.Fprint(w, response)
})
invoices, _, err := client.Billing.ListPendingCharges(ctx, nil)
if err != nil {
t.Errorf("Billing.ListPendingCharges returned error: %v", err)
}

expected := []InvoiceItem{
{
Description: "Load Balancer (my-loadbalancer)",
StartDate: "2020-10-10T01:56:20+00:00",
EndDate: "2020-10-10T01:56:20+00:00",
Units: 720,
UnitType: "hours",
UnitPrice: 0.0149,
Total: 10,
Product: "Load Balancer",
},
}

if !reflect.DeepEqual(invoices, expected) {
t.Errorf("Billing.ListInvoices returned %+v, expected %+v", invoices, expected)
}
}

0 comments on commit 6a36887

Please # to comment.